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:
- 6:8e2fca142827
- Parent:
- 5:e63e1024294e
- Child:
- 7:9bd49beccdd1
--- a/main.cpp Thu Jun 04 17:23:49 2020 +0000
+++ b/main.cpp Thu Jun 04 19:13:21 2020 +0000
@@ -14,6 +14,7 @@
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
+#include "Bitmap.h"
#include <ctime>
@@ -24,64 +25,109 @@
Gamepad pad;
-
-void welcome();
-
-bool gameOver;
-int borderW, borderH, appleX, appleY, snakeX, snakeY, score;
+bool endGame;
+int borderW, borderH, appleX, appleY, snakeX, snakeY, score;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
-char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+
-void setup()
+void setup() // Sets up the values for the game
{
- srand(time(0)); //Seed the generator, give it a starting value
- gameOver = false;
+ srand(time(0)); // Seed the generator, give it a starting value
+ endGame = false; // Sets the game as not finished
dir = STOP; // When the game starts the snake isn't moving
- borderW = WIDTH; //Fence width and height
- borderH = HEIGHT-8;
+
+ borderW = WIDTH; // Border width and height
+ borderH = HEIGHT-8; // Uses - 8 so we can get a line of text for the score
+
snakeX = borderW/2; // Snake Starts at Center
snakeY = borderH/2+8;
- appleX = rand() % borderW; // Fruit position
+
+ appleX = rand() % borderW; // Apple position
appleY = rand() % borderH;
- score = 0;
+
+ score = 0; // Sets the initial Score to 0
+
}
-void draw() {
- lcd.clear();
-
-
-
- lcd.drawRect(0,8,borderW,borderH,FILL_TRANSPARENT); // Border
- int ScoreLength = sprintf(buffer,"Score:%2d",score); // Score
+void display() // Deals with the fuctions of the display
+ {
+ lcd.clear();
+ lcd.drawRect(0,8,borderW,borderH,FILL_TRANSPARENT); // Draws a border showing the game area
+
+ char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+ int ScoreLength = sprintf(buffer,"Score:%2d",score); // Shows the player the score
if (ScoreLength <= 14)
- lcd.printString(buffer,WIDTH/2-25,0); // centers the score
+ lcd.printString(buffer,WIDTH/2-25,0); // centers the score
- lcd.drawCircle(snakeX,snakeY,2,FILL_BLACK); // snakehead
+
+ lcd.drawRect(snakeX,snakeY,5,5,FILL_BLACK); // Draws the initial snake head
- lcd.drawRect(appleX, appleY, 3, 3, FILL_BLACK); // Apple
+ lcd.drawCircle(appleX,appleY,2,FILL_BLACK); // Draws a circle representing an apple
lcd.refresh();
}
+void control() // Reads the controls
+ {
+ if (pad.A_pressed()){
+ dir = RIGHT;
+ }
+ if (pad.B_pressed()){
+ dir = DOWN;
+ }
+ if (pad.X_pressed()){
+ dir = UP;
+ }
+ if (pad.Y_pressed()){
+ dir = LEFT;
+ }
+
+}
+
+void gameplay() {
+
+ // Change the direction the snake goes dependant on the input
+
+ if (dir == UP) {
+ --snakeY;
+ }
+ if (dir == DOWN) {
+ ++snakeY;
+ }
+ if (dir == LEFT) {
+ --snakeX;
+ }
+ if (dir == RIGHT) {
+ ++snakeX;
+ }
+
+
+
+}
///////////// functions ////////////////
int main()
{
//initialise Display and gamepad
-lcd.init();
-pad.leds_off();
+ lcd.init(); // Turns the LCD display on
+ pad.init(); // Turns the gamepad on
+ pad.leds_off(); // Turns the LEDs OFF
+
+// Main Game Section
setup();
-while (!gameOver)
+while (!endGame)
{
- draw();
-
+ display();
+ control();
+ gameplay();
+ wait (0.06); // Sets how often the while loop functions can adjust the speed of the game!
}