Code for simple game of snake with four playing modes.

Dependencies:   N5110 PinDetect PowerControl mbed

Files at this revision

API Documentation at this revision

Comitter:
fiifimills
Date:
Mon May 11 01:51:37 2015 +0000
Parent:
11:f421defe6f55
Commit message:
Final Version

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri May 08 00:30:50 2015 +0000
+++ b/main.cpp	Mon May 11 01:51:37 2015 +0000
@@ -49,7 +49,7 @@
     UNKNOWN
 };
 
-DirectionName ga;  // enum variable for current direction of snake's movement
+DirectionName currentDirection;  // enum variable for current direction of snake's movement
 DirectionName previousDirection;  // enum variable for previous direction of snake's movement
 
 // enumerated type for game menu screen
@@ -84,17 +84,17 @@
 
 // flags
 int printFlag = 0;
-int Aflag = 0;  // flag for notification of A button interrupt
-int Bflag = 0;  // flag for notification of B button interrupt
-int collisionFlag = 0;  // flag for notification of self snake collision
-int borderFlag = 0;  // flag for hitting border in insane mode
+int Aflag = 0;          // flag for notification of A button interrupt
+int Bflag = 0;           // flag for notification of B button interrupt
+int collisionFlag = 0;      // flag for notification of self snake collision
+int borderFlag = 0;         // flag for hitting border in insane mode
 
 // global variables
-int score = 0;  // score value
-bool gameOver;  // boolean variable for game over
-int xCentre = 4;  // x coordinate for centre of selection circle
-int yCentre = 11;  // y coordinate for centre of selection circle
-float speed;  // wait time for moveSnake function
+int score = 0;              // score value
+bool gameOver;              // boolean variable for game over
+int xCentre = 4;            // x coordinate for centre of selection circle
+int yCentre = 11;           // y coordinate for centre of selection circle
+float speed;            // wait time for moveSnake function
 
 void pressedA()
 {
@@ -157,7 +157,7 @@
             lcd.drawRect(51,22,5,5,1);  // "food"
             lcd.refresh();
             
-            Sleep();  // when flag is not set, go to sleep & wait for interrupt  <-----
+            Sleep();  // when flag is not set, go to sleep & wait for interrupt  
 
             if (Aflag) { // if currentGameMenu is STARTUP and A is pressed,
                 Aflag = 0;  // reset flag
@@ -270,7 +270,6 @@
                     currentGameMenu = STARTUP;  // go back to startup screen
                 }
 
-                // Sleep();  // ?
             }
         }
 
@@ -318,8 +317,8 @@
 {
     srand(time(NULL));  // use time as seed for rand
 
-    int foodX = rand()%82+1;  // random x coordinate between 1-83
-    int foodY = rand()%46+1;  // random y coordinate between 1-47
+    int foodX = rand()%82+1;  // random x coordinate between 1-82
+    int foodY = rand()%46+1;  // random y coordinate between 1-46
 
     // draw border
     lcd.drawRect(0,0,83,47,0);  // x-origin, y-origin, width, height, fill
@@ -625,21 +624,4 @@
 
 
 
-}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Mon May 11 01:51:37 2015 +0000
@@ -0,0 +1,91 @@
+/**
+@file main.h
+@brief Header file for Snake, containing functions prototypes and global variables.
+@brief Revision 1.0.
+@author Fiifi Mills
+@date   May 2015
+*/
+
+#include "mbed.h"
+#include "N5110.h"
+#include "PinDetect.h"
+#include "PowerControl/PowerControl.h"
+#include "PowerControl/EthernetPowerControl.h"
+#include <vector>
+#include <stdlib.h>     /* srand, rand */
+#include <time.h>
+
+/**
+Enumerated type used for directions read from joystick
+*/
+
+enum DirectionName {
+    UP,
+    DOWN,
+    LEFT,
+    RIGHT,
+    CENTRE,
+    UNKNOWN
+};
+
+DirectionName currentDirection; /*!< DirectionName variable used to refer to the joystick's current direction */
+DirectionName previousDirection; /*!< DirectionName variable used to refer to the snake's previous direction */
+
+/**
+Enumerated type used to display different game menu screens
+*/
+
+enum GameMenu {
+    STARTUP,
+    SELECTSPEED,
+    GAMEPLAY
+};
+
+GameMenu currentGameMenu; /*!< GameMenu variable used to refer to the current game menu being displayed */
+
+/**
+Struct used for creating coordinates
+*/
+
+struct coordinate {
+    int x; 
+    int y; 
+};
+
+
+/** 
+Various flags used in the game for notification of specific events
+*/
+
+int printFlag; /*!< print flag set in ISR */
+int Aflag;  /*!< Aflag set in ISR when button A pressed */
+int Bflag;  /*!< Bflag set in ISR when button B pressed */
+int collisionFlag; /*!< collisionflag set in moveSnake() when snake collides with self */
+int borderFlag; /*!< borderflag set when snake collides with border in insane mode */
+
+/** 
+Global variables
+*/
+
+int score; /*!< The score that the user has accumulated throughout the game */
+int xCentre; /*!< The x coordinate of the centre of the selection circle used in the SELECTSPEED game menu */
+int yCentre; /*!< The y coordinate of the centre of the selection circle used in the SELECTSPEED game menu */
+bool gameOver; /*!< Boolean variable signifying whether or not the game is over */
+float speed; /*!< The wait time used in the moveSnake function */
+
+
+/**
+Implements the actual game's functionality by moving the snake and checking when certain rules have been broken
+*/
+void moveSnake();
+
+/**
+Moves the selection circle in the SELECTSPEED game menu either up or down depending on the direction in which the joystick is moved
+*/
+void speedSelect();
+
+/**
+Vector that contains coordinate structs, represents the snake in game
+*/
+std::vector<coordinate> Snake;
+