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
main.cpp
- Committer:
- Psy1990
- Date:
- 2020-06-05
- Revision:
- 8:32825d724856
- Parent:
- 7:9bd49beccdd1
- Child:
- 9:25597bc0cecc
File content as of revision 8:32825d724856:
/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
2019/20
Name: Simon Thackray Atkinson
Username: el18s2a
Student ID Number: 201255483
Date: 05/03/2020
*/
///////// pre-processor directives ////////
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "Bitmap.h"
#include "SnakeEngine.h"
#include "Snake.h"
#include "Apple.h"
#define SCORE 6
#define APPLE_SIZE 3
#define SNAKE_SPEED 1
/////////////// structs /////////////////
struct UserInput {
Direction d;
float mag;
};
/////////////// objects ///////////////
N5110 lcd;
Gamepad pad;
SnakeEngine snake;
///////////// prototypes ///////////////
void init();
void update_game(UserInput input);
void render();
void welcome();
///////////// functions ////////////////
int main()
{
int fps = 6; // frames per second
init(); // initialise and then display welcome screen...
welcome(); // waiting for the user to start
render(); // first draw the initial frame
wait(1.0f/fps); // and wait for one frame period
// game loop - read input, update the game state and render the display
while (1) {
snake.read_input(pad);
snake.update(pad);
render();
wait(1.0f/fps);
}
}
// initialies all classes and libraries
void init()
{
// need to initialise LCD and Gamepad
lcd.init();
pad.init();
}
// this function draws each frame on the LCD
void render()
{
// clear screen, re-draw and refresh
lcd.clear();
snake.draw(lcd);
lcd.refresh();
}
// simple splash screen displayed on start-up
void welcome() {
pad.led(3,1); // Only Show Green LEDS
pad.led(6,1); //
lcd.clear();
lcd.clear(); // we need to clear the screen first
lcd.printString(" Author ",0,1);
lcd.printString("Simon Atkinson",0,2);
lcd.printString(" 201255483 ",0,3);
lcd.printString(" Uni of Leeds ",0,4);
lcd.refresh(); // need to refresh display after setting pixels or writing strings
wait(1.0); // we don't want this screen on long!
lcd.clear();
lcd.printString(" Welcome to ",0,1);
lcd.printString(" Snake! ",0,2);
lcd.printString(" Press Start ",0,4);
lcd.refresh();
lcd.clear();
while ( pad.start_pressed() == false) {
}
}
/*
bool endGame;
int borderW, borderH, appleX, appleY, snakeX, snakeY, score;
enum eDirection {STOP = 0, LEFT, RIGHT, UP, DOWN};
eDirection dir;
void setup() // Sets up the values for the game
{
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; // 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; // Apple position
appleY = rand() % borderH;
score = 0; // Sets the initial Score to 0
}
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.drawRect(snakeX,snakeY,5,5,FILL_BLACK); // Draws the initial snake head
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;
}
if (pad.Y_pressed()){
dir = LEFT;
}
if (pad.start_pressed()){ // Stops the Movement
dir = STOP;
}
}
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;
}
}
void menu() {
while (1) {
pad.led(3,1); // Only Show Green LEDS
pad.led(6,1); //
// Splash Screen Info
lcd.clear(); // we need to clear the screen first
lcd.printString(" Author ",0,1);
lcd.printString("Simon Atkinson",0,2);
lcd.printString(" 201255483 ",0,3);
lcd.printString(" Uni of Leeds ",0,4);
lcd.refresh(); // need to refresh display after setting pixels or writing strings
wait(1.0); // we don't want this screen on long!
// main menu screen no interaction yet pressing start won't do anything but its a start if you pardon the pun!
lcd.clear();
lcd.printString(" Welcome to ",0,1);
lcd.printString(" Snake! ",0,2);
lcd.printString(" Press Start ",0,4);
lcd.refresh();
wait(45.0);
lcd.clear();
// Easter Egg time!
pad.led(3,0); // Turn Green LEDs Off
pad.led(6,0); //
pad.led(2,1); // Turn on Amber LEDs
pad.led(5,1); //
lcd.printString(" Do you need ",0,1);
lcd.printString(" more time? ",0,2);
lcd.printString(" Grandpa! ",0,3);
lcd.refresh();
wait(5.0);
//returns back to normal welcome screen
lcd.clear();
pad.led(3,1); // Turn Green LEDs On
pad.led(6,1); //
pad.led(2,0); // Turn off Amber LEDs
pad.led(5,0); //
lcd.printString(" Welcome to ",0,1);
lcd.printString(" Snake! ",0,2);
lcd.printString(" Press Start ",0,4);
lcd.refresh();
}
}
void ingame() {
while (!endGame)
{
display();
control();
gameplay();
wait (0.06); // Sets how often the while loop functions can adjust the speed of the game!
}
}
///////////// functions ////////////////
int main()
{
//initialise Display and gamepad
lcd.init(); // Turns the LCD display on
pad.init(); // Turns the gamepad on
pad.leds_off(); // Turns the LEDs OFF
// Main Game Section
setup();
if (pad.start_pressed()){
menu();
}
else {
ingame();
}
} */