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
Diff: main.cpp
- Revision:
- 20:980b37fde361
- Parent:
- 18:e58a1f8e72ad
--- a/main.cpp Tue May 08 12:51:40 2018 +0000
+++ b/main.cpp Tue May 08 13:41:08 2018 +0000
@@ -7,17 +7,18 @@
Student ID Number: 200975452
Date:
*/
-
+///////////pre-processor directives////////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "snake_engine.h"
#include "snake.h"
-
+/////////////objects///////////////
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad pad;
snake_engine engine;
+////////////prototypes//////////////
void init();
void render();
void welcome();
@@ -25,34 +26,34 @@
-
+/////////structure/////////
struct UserInput {
Direction d;
float mag;
};
-
+////////////functions//////////
int main()
{
init();
welcome();
while (1) {
- if ((engine.clision == 1)) {
+ if ((engine.clision == 1)) { //wall collision check. if collision happens == 1. gameover. initialise
init();
- welcome();
- render();
- wait(0.03f);
- engine.clision = 0;
+ welcome();//back to welcompage
+ render(); //first draw initial frame
+ wait(0.03f);//frame period
+ engine.clision = 0;// no collision when it backs to welcomepage
}
- engine.read_input(pad);
- engine.update(pad, lcd);
- render();
+ engine.read_input(pad);//engine recive command from gamepad
+ engine.update(pad, lcd);//update all the functions
+ render();//draw the frame
wait(0.05f);
- reset();
+ reset();//initialise the reset page.
}
}
-void init()
+void init()//initialise the gamepad , lcd and engine
{
lcd.init();
pad.init();
@@ -60,13 +61,13 @@
}
void render()
-{
+{ //clear screen , redraw the frame and game
lcd.clear();
engine.draw(lcd);
lcd.refresh();
}
void welcome()
-{
+{//welcome page.
lcd.printString(" Simple ",0,1);
lcd.printString("Snake Game",0,2);
lcd.printString(" WEI WEI ",0,3);
@@ -75,16 +76,16 @@
lcd.refresh();
lcd.clear();
wait(0.5);
- while ( pad.check_event(Gamepad::START_PRESSED) == false) {
+ while ( pad.check_event(Gamepad::START_PRESSED) == false) {//wait until the start button pressed
}
}
void reset()
-{
- if ( pad.check_event(Gamepad::BACK_PRESSED) == true) {
+{//game reset.
+ if ( pad.check_event(Gamepad::BACK_PRESSED) == true) {//if the back button pressed. returns to welcomepage.
lcd.clear();
- lcd.refresh();
+ lcd.refresh();//lcd clear screen and refresh
init();
welcome();