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:
- 5:e63e1024294e
- Parent:
- 4:07aada4c8421
- Child:
- 6:8e2fca142827
diff -r 07aada4c8421 -r e63e1024294e main.cpp
--- a/main.cpp Thu Jun 04 16:14:12 2020 +0000
+++ b/main.cpp Thu Jun 04 17:23:49 2020 +0000
@@ -14,14 +14,11 @@
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
+#include <ctime>
-/////////////// structs /////////////////
-struct UserInput {
- Direction d;
- float mag;
-};
+
/////////////// objects ///////////////
N5110 lcd;
Gamepad pad;
@@ -31,7 +28,7 @@
void welcome();
bool gameOver;
-int borderW, borderH, appleX, appleY, score;
+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)
@@ -40,25 +37,37 @@
void setup()
{
+ srand(time(0)); //Seed the generator, give it a starting value
gameOver = false;
- dir = STOP;
+ dir = STOP; // When the game starts the snake isn't moving
borderW = WIDTH; //Fence width and height
- borderH = HEIGHT-8;
- appleX = rand() % borderW;
- appleY = rand() % borderH;
+ borderH = HEIGHT-8;
+ snakeX = borderW/2; // Snake Starts at Center
+ snakeY = borderH/2+8;
+ appleX = rand() % borderW; // Fruit position
+ appleY = rand() % borderH;
score = 0;
}
void draw() {
lcd.clear();
+
+
lcd.drawRect(0,8,borderW,borderH,FILL_TRANSPARENT); // Border
- int length = sprintf(buffer,"Score:%2d",score); // Score
- if (length <= 14)
+ int ScoreLength = sprintf(buffer,"Score:%2d",score); // Score
+ if (ScoreLength <= 14)
lcd.printString(buffer,WIDTH/2-25,0); // centers the score
+
+ lcd.drawCircle(snakeX,snakeY,2,FILL_BLACK); // snakehead
+
+
+ lcd.drawRect(appleX, appleY, 3, 3, FILL_BLACK); // Apple
+
lcd.refresh();
- }
+
+ }
///////////// functions ////////////////