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: Joystick N5110 SDFileSystem beep fsmMenu mbed
Fork of SnakeProjectRev1 by
Diff: main.cpp
- Revision:
- 22:195d66c61bf3
- Parent:
- 21:e03461ea23e9
- Child:
- 24:c6b53a526c4e
diff -r e03461ea23e9 -r 195d66c61bf3 main.cpp
--- a/main.cpp Thu May 05 14:19:50 2016 +0000
+++ b/main.cpp Thu May 05 14:55:29 2016 +0000
@@ -1,45 +1,58 @@
/**
@file main.cpp
-
@brief Program implementation of Snake Game
-
*/
-
+/*
+Meurig Phillips
+200870500
+*/
#include "main.h"
-// Serial for debug
-Serial serial(USBTX,USBRX);
-
-FILE *fp; // this is our file pointer
-
-
+FILE *fp; // pointer for SD card
int main()
{
- calibrateJoystick(); // get centred values of joystick
- pollJoystick.attach(&updateJoystick,0.5/10.0); // read joystick 20 times per second
+ /// Setup up interrupt peripherals
+ initInterrupts();
+ /// Get centred values of joystick
+ calibrateJoystick();
+
+ /// Read joystick 20 timer per second
+ pollJoystick.attach(&updateJoystick,0.5/10.0);
+
+ /// Set rand seed
srand(time(NULL));
+ /// Green LED is pull-up, Red is pull-down
greenLed = 1;
redLed = 0;
+
+ /// Init Screen
lcd.init();
+
+ ///Splash Screen
snakeIntro();
- gamePlaying = false;
+
+ /// Menu
+ mainMenu();
+
+}
+
+void initInterrupts()
+{
+ /// ISRs executed on the rise
button.rise(&buttonISR);
gameTicker.attach(&timer_isr,0.1);
RB.mode(PullDown);
LB.mode(PullDown);
RB.rise(&rb_isr);
LB.rise(&lb_isr);
-
-
- mainMenu();
-
}
-void mainGame() {
-
+void mainGame()
+{
+ /// Initialise coordinates depending on game type
if(gameType == classicMode) {
hardWall();
i = 41;
@@ -55,16 +68,17 @@
i = 13;
j = 5;
}
- generateFood();
+ generateFood(); /// Create random food
+ /// Initialise
initSnakeTail();
snakeTailLength = 3;
score = 0;
fruitValue = 10;
- pauseCount = 0; // init everything
+ pauseCount = 0;
while(gamePlaying == true) {
- lcd.setBrightness(1-pot); // turn pot right for brightness
+ lcd.setBrightness(1-pot); /// turn pot right for brightness
if (buttonFlag ==1) {
buttonFlag = 0;
@@ -75,49 +89,48 @@
}
buttonFlag = 0;
- if(game_timer_flag) {
+ if(game_timer_flag) { /// logic implemented & screen updated with every tick
game_timer_flag = 0;
gameLogic();
}
- if (printFlag) { // if flag set, clear flag and print joystick values to serial port
+ if (printFlag) { /// if joystick input is received, update direction
printFlag = 0;
moveSnake();
}
- sleep(); // put the MCU to sleep until an interrupt wakes it up
+ sleep();
}
}
-void mainMenu() {
+void mainMenu()
+{
while(1) {
-
+
+ /// Menu options
lcd.printString("Classic",0,1);
lcd.printString("Infinite",0,3);
lcd.printString("Hard Map",0,5);
+ /// Start game when right button is pressed
if (rb_flag == 1) {
rb_flag = 0;
gamePlaying = true;
mainGame();
}
- // check if flag i.e. interrupt has occured
+ /// If joystcik is moved
if (printFlag ==1) {
- printFlag = 0; // if it has, clear the flag
+ printFlag = 0;
- // swap direction when button has been pressed
- // (could just use ! but want this to be explicit to aid understanding)
+ /// Cycle through menu
if (joystick.direction == CENTRE) {
- serial.printf(" CENTRE\n");
menuDirection = menuSTOP;
}
else if (joystick.direction == UP) {
- serial.printf(" UP\n");
menuDirection = menuUP;
buzzer.beep(2000,0.2);
}
else if (joystick.direction == DOWN) {
- serial.printf(" DOWN\n");
menuDirection = menuDOWN;
buzzer.beep(2000,0.2);
}
@@ -126,8 +139,8 @@
menuFSM();
-
- wait(0.2); // small delay
+ /// Move selector (*) according to fsm state
+ wait(0.2);
if (state ==0) {
lcd.clear();
lcd.printString("*",70,1);
@@ -151,11 +164,11 @@
void generateFood()
{
if (gameType == classicMode || gameType == hardMode) {
- while (randomXoddEven ==0 || randomYoddEven ==0 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on
+ while (randomXoddEven ==0 || randomYoddEven ==0 || lcd.getPixel(randomX,randomY) >= 1) { /// do while x or y is even or pixel is on
- randomX = rand() % 83 + 1; // randomX in the range 1 to 83
- randomY = rand() % 47 + 1; // randomY in the range 1 to 47
- randomXoddEven = randomX%2; // find out whether odd or even
+ randomX = rand() % 83 + 1; /// randomX in the range 1 to 83
+ randomY = rand() % 47 + 1; /// randomY in the range 1 to 47
+ randomXoddEven = randomX%2; /// find out whether odd or even
randomYoddEven = randomY%2;
}
@@ -163,7 +176,7 @@
}
else {
- while (randomXoddEven ==1 || randomYoddEven ==1 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is even or pixel is on
+ while (randomXoddEven ==1 || randomYoddEven ==1 || lcd.getPixel(randomX,randomY) >= 1) { // do while x or y is odd or pixel is on
randomX = rand() % 83 + 1; // randomX in the range 1 to 83
randomY = rand() % 47 + 1; // randomY in the range 1 to 47
@@ -176,12 +189,12 @@
}
-void newFruitXY() // new fruit coordinate values are given before it is passed to the generateFood function
+void newFruitXY() /// new fruit coordinate values are given before it is passed to the generateFood function
{
- randomX = rand() % 83 + 1; // randomX in the range 1 to 81
- randomY = rand() % 47 + 1; // randomY in the range 1 to 47
- randomXoddEven = randomX%2; // find out whether odd or even
+ randomX = rand() % 83 + 1; /// randomX in the range 1 to 81
+ randomY = rand() % 47 + 1; /// randomY in the range 1 to 47
+ randomXoddEven = randomX%2; /// find out whether odd or even
randomYoddEven = randomY%2;
}
@@ -189,23 +202,23 @@
void moveSnake() {
if (joystick.direction == LEFT) {
- if (currentDirection != right) { // change the currentDirection according to joystick input, providing
- currentDirection = left; // it's not the opposite direction to the current direction
+ if (currentDirection != right) { /// change the currentDirection according to joystick input, providing
+ currentDirection = left; /// it's not the opposite direction to the current direction
}
}
else if (joystick.direction == RIGHT) {
- if (currentDirection != left) { // change the currentDirection according to joystick input, providing
- currentDirection = right; // it's not the opposite direction to the current direction
+ if (currentDirection != left) { /// change the currentDirection according to joystick input, providing
+ currentDirection = right; /// it's not the opposite direction to the current direction
}
}
else if (joystick.direction == UP) {
- if (currentDirection != down) { // change the currentDirection according to joystick input, providing
- currentDirection = up; // it's not the opposite direction to the current direction
+ if (currentDirection != down) { /// change the currentDirection according to joystick input, providing
+ currentDirection = up; /// it's not the opposite direction to the current direction
}
}
else if (joystick.direction == DOWN) {
- if (currentDirection != up) { // change the currentDirection according to joystick input, providing
- currentDirection = down; // it's not the opposite direction to the current direction
+ if (currentDirection != up) { /// change the currentDirection according to joystick input, providing
+ currentDirection = down; /// it's not the opposite direction to the current direction
}
}
}
@@ -221,7 +234,7 @@
snakeTailX[0] = i;
snakeTailY[0] = j;
- for (int a=1; a<snakeTailLength; a++) { // loops through snakeTail array and assigns previous seg's coordinates to current one
+ for (int a=1; a<snakeTailLength; a++) { /// loops through snakeTail array and assigns previous seg's coordinates to current one
prev2_i = snakeTailX[a];
prev2_j = snakeTailY[a];
snakeTailX[a] = prev_i;
@@ -231,26 +244,26 @@
}
- if (currentDirection == left) { // shift snake head coordinates according to current direction
- i -= 2; } // move left
+ if (currentDirection == left) { /// shift snake head coordinates according to current direction
+ i -= 2; } /// move left
else if (currentDirection == right) {
- i += 2; } // move right
+ i += 2; } /// move right
else if (currentDirection == up) {
- j -= 2; } // move up
+ j -= 2; } /// move up
else if (currentDirection == down) {
- j += 2; } // move down
+ j += 2; } /// move down
- lcd.drawRect(i,j,1,1,1); // snake head
+ lcd.drawRect(i,j,1,1,1); /// snake head
for (int b=0; b<snakeTailLength; b++) {
lcd.drawRect(snakeTailX[b],snakeTailY[b],1,1,1);
}
lcd.refresh();
- lcd.drawRect(randomX,randomY,1,1,1); // food
+ lcd.drawRect(randomX,randomY,1,1,1); /// food
- if (gameType == classicMode) { // map types
+ if (gameType == classicMode) { /// map types
hardWall();
}
else if (gameType == infiniteMode) {
@@ -260,7 +273,7 @@
specialMap();
}
- if (i == randomX && j == randomY) { // if fruit is eaten
+ if (i == randomX && j == randomY) { /// if fruit is eaten
greenLed = 0;
buzzer.beep(2000,0.2);
scoreCalculation();
@@ -269,7 +282,7 @@
generateFood();
}
- if (snakeTailLength > 4) { // if snake eats itself
+ if (snakeTailLength > 4) { /// if snake eats itself
for (int q=1; q<snakeTailLength; q++) {
if (snakeTailX[q] == i && snakeTailY[q] == j) {
gameOver();
@@ -285,7 +298,7 @@
lcd.drawRect(0,0,83,47,0);
lcd.refresh();
if (i == 0 || i+1 == 0 ||
- i == 83 || i+1 == 83 || // if any of the 4 pixels within the snake head touch the border
+ i == 83 || i+1 == 83 || /// if any of the 4 pixels within the snake head touch the border
j == 0 || j+1 == 0 ||
j == 47 || j+1 == 47)
{
@@ -300,7 +313,7 @@
redLed = 1;
if(score > top_score) {
- ////////////////////// Simple writing example //////////////////////////
+
// open file for writing ('w') - creates file if it doesn't exist and overwrites
// if it does. If you wish to add a score onto a list, then you can
@@ -310,11 +323,11 @@
int top_score = score;
if (fp == NULL) { // if it can't open the file then print error message
- serial.printf("Error! Unable to open file!\n");
+
} else { // opened file so can write
- serial.printf("Writing to file....");
+
fprintf(fp, "%d",top_score); // ensure data type matches
- serial.printf("Done.\n");
+
fclose(fp); // ensure you close the file after writing
}
}
@@ -327,22 +340,21 @@
wait(0.5);
char buffer[14];
- int length = sprintf(buffer,"Score:%i",score); // display score of round
+ int length = sprintf(buffer,"Score:%i",score); /// display score of round
if (length <= 14) { // if string will fit on display
lcd.printString(buffer,0,1); } // display on screen
buzzer.beep(262,2);
wait(0.5);
- ////////////////////// Simple reading example //////////////////////////
// now open file for reading
fp = fopen("/sd/topscore.txt", "r");
// int stored_top_score = -1; // -1 to demonstrate it has changed after reading
if (fp == NULL) { // if it can't open the file then print error message
- serial.printf("Error! Unable to open file!\n");
+
} else { // opened file so can write
fscanf(fp, "%d",&top_score); // ensure data type matches - note address operator (&)
- serial.printf("Read %d from file.\n",top_score);
+
char buffer[14];
int length = sprintf(buffer,"HI Score:%i",top_score); // display score of round
if (length <= 14) { // if string will fit on display
@@ -381,7 +393,7 @@
lcd.drawRect(49,9,1,31,1);
lcd.refresh();
if (i == 0 || i+1 == 0 ||
- i == 83 || i+1 == 83 || // if any of the 4 pixels within the snake head touch the border
+ i == 83 || i+1 == 83 || /// if any of the 4 pixels within the snake head touch the border
j == 0 || j+1 == 0 ||
j == 47 || j+1 == 47)
{
@@ -412,13 +424,10 @@
void scoreCalculation() {
- score += fruitValue; // each time fruit is eaten score is calculated and fruit value will increase by 1
+ score += fruitValue; /// each time fruit is eaten score is calculated and fruit value will increase by 1
fruitValue++;
-
- //serial.printf("score = %i\n",score);
- //serial.printf("fruitValue = %i\n\n",fruitValue);
-
+
}
void initSnakeTail() {
@@ -449,7 +458,7 @@
snakeTailX[2] = prev2_i;
snakeTailY[2] = prev2_j;
- lcd.drawRect(i,j,1,1,1); // snake head
+ lcd.drawRect(i,j,1,1,1); /// snake head
for (int b=0; b<snakeTailLength; b++) {
lcd.drawRect(snakeTailX[b],snakeTailY[b],1,1,1);
}
@@ -467,7 +476,7 @@
for (int q=0; q<WIDTH/2; q++) {
lcd.drawCircle(WIDTH/2,HEIGHT/2,q,2);
wait_ms(2);
- } // x,y,radius,white fill
+ } /// x,y,radius,white fill
lcd.printString("Snake",27,0);
buzzer.beep(2000,0.5);
@@ -492,7 +501,7 @@
pauseCount++;
lcd.clear();
char buffer[14];
- int length = sprintf(buffer,"%i left",3-pauseCount); // number of pauses left
+ int length = sprintf(buffer,"%i left",3-pauseCount); /// number of pauses left
if (length <= 14) { // if string will fit on display
lcd.printString(buffer,24,3); } // display on screen
