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
main.cpp@20:980b37fde361, 2018-05-08 (annotated)
- Committer:
- weiway
- Date:
- Tue May 08 13:41:08 2018 +0000
- Revision:
- 20:980b37fde361
- Parent:
- 18:e58a1f8e72ad
adding doxygen documentation , comments and references
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| weiway | 0:d557f30e3a95 | 1 | /* |
| weiway | 0:d557f30e3a95 | 2 | ELEC2645 Embedded Systems Project |
| weiway | 0:d557f30e3a95 | 3 | School of Electronic & Electrical Engineering |
| weiway | 0:d557f30e3a95 | 4 | University of Leeds |
| weiway | 0:d557f30e3a95 | 5 | Name: Wei Wei |
| weiway | 0:d557f30e3a95 | 6 | Username: el15ww |
| weiway | 0:d557f30e3a95 | 7 | Student ID Number: 200975452 |
| weiway | 0:d557f30e3a95 | 8 | Date: |
| weiway | 9:f82627d119f6 | 9 | */ |
| weiway | 20:980b37fde361 | 10 | ///////////pre-processor directives//////////// |
| weiway | 9:f82627d119f6 | 11 | #include "mbed.h" |
| weiway | 9:f82627d119f6 | 12 | #include "Gamepad.h" |
| weiway | 9:f82627d119f6 | 13 | #include "N5110.h" |
| weiway | 15:47ea86f1ed70 | 14 | #include "snake_engine.h" |
| weiway | 13:4026781772cb | 15 | #include "snake.h" |
| weiway | 20:980b37fde361 | 16 | /////////////objects/////////////// |
| weiway | 9:f82627d119f6 | 17 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| weiway | 15:47ea86f1ed70 | 18 | |
| weiway | 9:f82627d119f6 | 19 | Gamepad pad; |
| weiway | 13:4026781772cb | 20 | snake_engine engine; |
| weiway | 20:980b37fde361 | 21 | ////////////prototypes////////////// |
| weiway | 15:47ea86f1ed70 | 22 | void init(); |
| weiway | 15:47ea86f1ed70 | 23 | void render(); |
| weiway | 15:47ea86f1ed70 | 24 | void welcome(); |
| weiway | 15:47ea86f1ed70 | 25 | void reset(); |
| weiway | 15:47ea86f1ed70 | 26 | |
| weiway | 15:47ea86f1ed70 | 27 | |
| weiway | 9:f82627d119f6 | 28 | |
| weiway | 20:980b37fde361 | 29 | /////////structure///////// |
| weiway | 9:f82627d119f6 | 30 | struct UserInput { |
| weiway | 9:f82627d119f6 | 31 | Direction d; |
| weiway | 9:f82627d119f6 | 32 | float mag; |
| weiway | 9:f82627d119f6 | 33 | }; |
| weiway | 20:980b37fde361 | 34 | ////////////functions////////// |
| weiway | 13:4026781772cb | 35 | int main() |
| weiway | 13:4026781772cb | 36 | { |
| weiway | 13:4026781772cb | 37 | init(); |
| weiway | 13:4026781772cb | 38 | welcome(); |
| weiway | 13:4026781772cb | 39 | while (1) { |
| weiway | 20:980b37fde361 | 40 | if ((engine.clision == 1)) { //wall collision check. if collision happens == 1. gameover. initialise |
| weiway | 18:e58a1f8e72ad | 41 | init(); |
| weiway | 20:980b37fde361 | 42 | welcome();//back to welcompage |
| weiway | 20:980b37fde361 | 43 | render(); //first draw initial frame |
| weiway | 20:980b37fde361 | 44 | wait(0.03f);//frame period |
| weiway | 20:980b37fde361 | 45 | engine.clision = 0;// no collision when it backs to welcomepage |
| weiway | 18:e58a1f8e72ad | 46 | } |
| weiway | 13:4026781772cb | 47 | |
| weiway | 20:980b37fde361 | 48 | engine.read_input(pad);//engine recive command from gamepad |
| weiway | 20:980b37fde361 | 49 | engine.update(pad, lcd);//update all the functions |
| weiway | 20:980b37fde361 | 50 | render();//draw the frame |
| weiway | 13:4026781772cb | 51 | wait(0.05f); |
| weiway | 20:980b37fde361 | 52 | reset();//initialise the reset page. |
| weiway | 13:4026781772cb | 53 | } |
| weiway | 15:47ea86f1ed70 | 54 | |
| weiway | 13:4026781772cb | 55 | } |
| weiway | 20:980b37fde361 | 56 | void init()//initialise the gamepad , lcd and engine |
| weiway | 13:4026781772cb | 57 | { |
| weiway | 13:4026781772cb | 58 | lcd.init(); |
| weiway | 13:4026781772cb | 59 | pad.init(); |
| weiway | 13:4026781772cb | 60 | engine.init(); |
| weiway | 13:4026781772cb | 61 | } |
| weiway | 13:4026781772cb | 62 | |
| weiway | 13:4026781772cb | 63 | void render() |
| weiway | 20:980b37fde361 | 64 | { //clear screen , redraw the frame and game |
| weiway | 13:4026781772cb | 65 | lcd.clear(); |
| weiway | 13:4026781772cb | 66 | engine.draw(lcd); |
| weiway | 13:4026781772cb | 67 | lcd.refresh(); |
| weiway | 13:4026781772cb | 68 | } |
| weiway | 13:4026781772cb | 69 | void welcome() |
| weiway | 20:980b37fde361 | 70 | {//welcome page. |
| weiway | 15:47ea86f1ed70 | 71 | lcd.printString(" Simple ",0,1); |
| weiway | 15:47ea86f1ed70 | 72 | lcd.printString("Snake Game",0,2); |
| weiway | 15:47ea86f1ed70 | 73 | lcd.printString(" WEI WEI ",0,3); |
| weiway | 15:47ea86f1ed70 | 74 | lcd.printString("200975452",0,4); |
| weiway | 15:47ea86f1ed70 | 75 | lcd.printString(" PRESS START ",0,5); |
| weiway | 15:47ea86f1ed70 | 76 | lcd.refresh(); |
| weiway | 15:47ea86f1ed70 | 77 | lcd.clear(); |
| weiway | 15:47ea86f1ed70 | 78 | wait(0.5); |
| weiway | 20:980b37fde361 | 79 | while ( pad.check_event(Gamepad::START_PRESSED) == false) {//wait until the start button pressed |
| weiway | 13:4026781772cb | 80 | } |
| weiway | 13:4026781772cb | 81 | |
| weiway | 13:4026781772cb | 82 | } |
| weiway | 13:4026781772cb | 83 | |
| weiway | 13:4026781772cb | 84 | void reset() |
| weiway | 20:980b37fde361 | 85 | {//game reset. |
| weiway | 20:980b37fde361 | 86 | if ( pad.check_event(Gamepad::BACK_PRESSED) == true) {//if the back button pressed. returns to welcomepage. |
| weiway | 13:4026781772cb | 87 | lcd.clear(); |
| weiway | 20:980b37fde361 | 88 | lcd.refresh();//lcd clear screen and refresh |
| weiway | 13:4026781772cb | 89 | |
| weiway | 13:4026781772cb | 90 | init(); |
| weiway | 13:4026781772cb | 91 | welcome(); |
| weiway | 13:4026781772cb | 92 | } |
| weiway | 13:4026781772cb | 93 | } |
| weiway | 9:f82627d119f6 | 94 | |
| weiway | 9:f82627d119f6 | 95 | |
| weiway | 18:e58a1f8e72ad | 96 | |
| weiway | 9:f82627d119f6 | 97 | |
| weiway | 18:e58a1f8e72ad | 98 |