Wang Lin 201090174
Dependencies: mbed Gamepad N5110 FXOS8700Q
main.cpp@12:121ba031343a, 2019-05-03 (annotated)
- Committer:
- a1115921303
- Date:
- Fri May 03 17:12:37 2019 +0000
- Revision:
- 12:121ba031343a
- Parent:
- 10:167d0aa1c9b3
- Child:
- 13:febf9fbb502f
Implement the basic movement of Snake and it could eat the food and become longer (finished 2 weeks ago. forget to commit)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a1115921303 | 12:121ba031343a | 1 | #include "main.h" |
a1115921303 | 12:121ba031343a | 2 | #define SNAKE_LENGTH 100 |
a1115921303 | 12:121ba031343a | 3 | #define SNAKE_SIZE 2 |
eencae | 0:e1442f3aa3c7 | 4 | void init(); |
a1115921303 | 12:121ba031343a | 5 | void welcome(); |
eencae | 0:e1442f3aa3c7 | 6 | void render(); |
a1115921303 | 12:121ba031343a | 7 | void initsnake(); |
a1115921303 | 12:121ba031343a | 8 | |
eencae | 0:e1442f3aa3c7 | 9 | int main() |
eencae | 0:e1442f3aa3c7 | 10 | { |
a1115921303 | 12:121ba031343a | 11 | |
eencae | 6:d9d05b321b4d | 12 | init(); // initialise and then display welcome screen... |
eencae | 6:d9d05b321b4d | 13 | welcome(); // waiting for the user to start |
a1115921303 | 12:121ba031343a | 14 | // first draw the initial frame |
a1115921303 | 12:121ba031343a | 15 | initsnake();//intilize the variables |
a1115921303 | 12:121ba031343a | 16 | initSnake(); |
a1115921303 | 12:121ba031343a | 17 | SpwanFood(); |
a1115921303 | 12:121ba031343a | 18 | |
a1115921303 | 12:121ba031343a | 19 | while(1) { |
a1115921303 | 12:121ba031343a | 20 | lcd.clear(); |
a1115921303 | 12:121ba031343a | 21 | render(); |
a1115921303 | 12:121ba031343a | 22 | drawFood(); |
a1115921303 | 12:121ba031343a | 23 | DrawSnake(); |
a1115921303 | 12:121ba031343a | 24 | MoveSnake(); |
a1115921303 | 12:121ba031343a | 25 | EatFood(); |
a1115921303 | 12:121ba031343a | 26 | lcd.refresh(); |
a1115921303 | 12:121ba031343a | 27 | Speed(); |
eencae | 1:25a839625a1e | 28 | |
a1115921303 | 12:121ba031343a | 29 | |
eencae | 0:e1442f3aa3c7 | 30 | } |
eencae | 0:e1442f3aa3c7 | 31 | } |
a1115921303 | 12:121ba031343a | 32 | |
eencae | 0:e1442f3aa3c7 | 33 | void init() |
eencae | 0:e1442f3aa3c7 | 34 | { |
eencae | 3:910d7e87f367 | 35 | // need to initialise LCD and Gamepad |
eencae | 0:e1442f3aa3c7 | 36 | lcd.init(); |
eencae | 0:e1442f3aa3c7 | 37 | pad.init(); |
eencae | 3:910d7e87f367 | 38 | |
eencae | 0:e1442f3aa3c7 | 39 | } |
a1115921303 | 12:121ba031343a | 40 | |
eencae | 3:910d7e87f367 | 41 | void welcome() { |
a1115921303 | 12:121ba031343a | 42 | |
a1115921303 | 12:121ba031343a | 43 | lcd.clear(); |
a1115921303 | 12:121ba031343a | 44 | lcd.printString(" SNAKE! ",0,1); |
a1115921303 | 12:121ba031343a | 45 | lcd.printString(" Press Start ",0,2); |
eencae | 3:910d7e87f367 | 46 | |
eencae | 3:910d7e87f367 | 47 | lcd.refresh(); |
eencae | 3:910d7e87f367 | 48 | |
eencae | 3:910d7e87f367 | 49 | // wait flashing LEDs until start button is pressed |
eencae | 3:910d7e87f367 | 50 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
eencae | 3:910d7e87f367 | 51 | pad.leds_on(); |
eencae | 3:910d7e87f367 | 52 | wait(0.1); |
eencae | 3:910d7e87f367 | 53 | pad.leds_off(); |
a1115921303 | 12:121ba031343a | 54 | wait(waittime); |
a1115921303 | 12:121ba031343a | 55 | lcd.printString("LETS START!",0,2); |
eencae | 3:910d7e87f367 | 56 | } |
a1115921303 | 12:121ba031343a | 57 | } |
eencae | 3:910d7e87f367 | 58 | |
a1115921303 | 12:121ba031343a | 59 | void render() { |
a1115921303 | 12:121ba031343a | 60 | |
a1115921303 | 12:121ba031343a | 61 | lcd.drawRect(0,0,83,47,FILL_TRANSPARENT); |
a1115921303 | 12:121ba031343a | 62 | |
a1115921303 | 12:121ba031343a | 63 | } |
a1115921303 | 12:121ba031343a | 64 | |
a1115921303 | 12:121ba031343a | 65 | void initsnake() { |
a1115921303 | 12:121ba031343a | 66 | SNAKE.snakezb[0].x = 30; |
a1115921303 | 12:121ba031343a | 67 | SNAKE.snakezb[0].y = 20; |
a1115921303 | 12:121ba031343a | 68 | SNAKE.n = 3; //inital length of snake |
a1115921303 | 12:121ba031343a | 69 | Dir = 3; |
a1115921303 | 12:121ba031343a | 70 | |
a1115921303 | 12:121ba031343a | 71 | |
a1115921303 | 12:121ba031343a | 72 | } |
a1115921303 | 12:121ba031343a | 73 |