Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed Gamepad N5110
Diff: snake/snake.cpp
- Revision:
- 15:4123abb17291
- Parent:
- 11:543c62bed764
--- a/snake/snake.cpp Mon Apr 22 07:22:56 2019 +0000
+++ b/snake/snake.cpp Mon Apr 22 09:59:39 2019 +0000
@@ -19,13 +19,13 @@
_x[i] = 0;
_y[i] = 0;
}
- _x[0] = WIDTH/2 - _size/2;
- _y[0] = HEIGHT/2 - _size/2;
+ _x[0] = WIDTH/2 - _size;
+ _y[0] = HEIGHT/2 - _size;
- _x[1] = _x[0] -size;
+ _x[1] = _x[0] - _size;
_y[1] = _y[0];
- _x[2] = _x[0] -2*size;
+ _x[2] = _x[0] -2*_size;
_y[2] = _y[0];
ball( _length);
@@ -67,14 +67,14 @@
}
// draw ball in screen buffer.
- lcd.drawCircle(ball_x,ball_y,1,FILL_BLACK);
+ lcd.drawRect(ball_x,ball_y,2,2,FILL_BLACK);
}
void snake::ball(int length)
{
srand(time(NULL));
- ball_x = 3 + rand() % (WIDTH - 6); // randomise initial position of ball.
- ball_y = 3 + rand() % (HEIGHT - 6);
+ ball_x = (3 + rand() % (37))*2; // randomise initial position of ball.
+ ball_y = (3 + rand() % (19))*2;
int check = 0;
do{
check = 0;
@@ -84,15 +84,15 @@
}
}
//srand(time(NULL));
- ball_x = 3 + rand() % (WIDTH - 6); // randomise initial position of ball.
- ball_y = 3 + rand() % (HEIGHT - 6);
+ ball_x = (3 + rand() % (37))*2; // randomise initial position of ball.
+ ball_y = (3 + rand() % (19))*2;
} while(check == 1);
}
void snake::check_eat(Gamepad &pad)
{
//
- if (ball_x + _size >= _x[0] && ball_x - _size <= _x[0] && ball_y + _size >= _y[0] && ball_y - _size <= _y[0]) {
+ if (ball_x == _x[0] && ball_y == _y[0]) {
_length++;
_score++;
ball(_length);