submit

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Mon May 06 05:26:28 2019 +0000
Revision:
23:fd0339ec480c
Parent:
22:51fb86b48d9d
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
694617778 0:b67614a0c4cf 1 ///////// pre-processor directives ////////
694617778 0:b67614a0c4cf 2 #include "mbed.h"
694617778 0:b67614a0c4cf 3 #include "Gamepad.h"
694617778 0:b67614a0c4cf 4 #include "N5110.h"
694617778 18:2ca7bd135b68 5 #include "Snake.h"
694617778 1:b49c36604125 6 #include "Engine.h"
694617778 18:2ca7bd135b68 7 #include "Finger.h"
694617778 0:b67614a0c4cf 8
694617778 0:b67614a0c4cf 9 /////////////// objects ///////////////
694617778 0:b67614a0c4cf 10 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
694617778 0:b67614a0c4cf 11 Gamepad pad;
694617778 18:2ca7bd135b68 12 Snake snake;
694617778 1:b49c36604125 13 Engine engine;
694617778 18:2ca7bd135b68 14 Finger finger;
694617778 16:a52b2441c5ae 15
694617778 0:b67614a0c4cf 16 ///////////// prototypes ///////////////
694617778 0:b67614a0c4cf 17 void init();
694617778 0:b67614a0c4cf 18 void run();
694617778 4:323f42022d87 19 void over();
694617778 10:68076e3dcc33 20
694617778 0:b67614a0c4cf 21 ///////////// functions ////////////////
694617778 0:b67614a0c4cf 22 int main()
694617778 0:b67614a0c4cf 23 {
694617778 0:b67614a0c4cf 24 int fps = 8; // frames per second
694617778 20:6ef06ba15666 25 snake.hscore = 0; //initialise the highest record
694617778 11:543c62bed764 26 lcd.init();
694617778 9:18b059e5abb9 27 while(1){
694617778 15:4123abb17291 28 init();
694617778 20:6ef06ba15666 29 pad.tone(900.0,0.5); // Start-up sound
694617778 9:18b059e5abb9 30 engine.welcome(pad,lcd); // show welcome display, waiting for the user to start
694617778 10:68076e3dcc33 31 engine.menu(pad,lcd); // show the select display, waiting for the user to select
694617778 16:a52b2441c5ae 32 // choose game and game loop
694617778 14:e1f7b4be1cf2 33 if(engine.game == 0){
694617778 9:18b059e5abb9 34 while (snake.over == 0) {
694617778 23:fd0339ec480c 35 run(); // run snake game
694617778 10:68076e3dcc33 36 while (pad.check_event(Gamepad::START_PRESSED) == true){
694617778 16:a52b2441c5ae 37 engine.pause(pad,lcd); // pause the game
694617778 13:32772a08841e 38 }
694617778 9:18b059e5abb9 39 wait(engine.p/fps); // and wait for one frame period
694617778 9:18b059e5abb9 40 }
694617778 13:32772a08841e 41 over(); // show gameover display, waiting for the user to restart
694617778 14:e1f7b4be1cf2 42 }else if(engine.game == 1){
694617778 16:a52b2441c5ae 43 finger.run(lcd,pad); // run the finger-geuss game
694617778 14:e1f7b4be1cf2 44 }
694617778 2:934daa65f73d 45 }
694617778 0:b67614a0c4cf 46 }
694617778 0:b67614a0c4cf 47
694617778 0:b67614a0c4cf 48 // this function draws each frame on the LCD
694617778 0:b67614a0c4cf 49 void run()
694617778 0:b67614a0c4cf 50 {
694617778 0:b67614a0c4cf 51 // clear screen, re-draw and refresh
694617778 22:51fb86b48d9d 52 lcd.clear(); // lear the lcd
694617778 22:51fb86b48d9d 53 int length = snake.get_length(); // get length
694617778 22:51fb86b48d9d 54 int direction = engine.get_direction(pad); // get direction
694617778 22:51fb86b48d9d 55 snake.check_eat(pad); // check if eat
694617778 22:51fb86b48d9d 56 snake.update(direction,length); // update the position of snake
694617778 22:51fb86b48d9d 57 snake.draw(lcd,length); // draw the snake
694617778 22:51fb86b48d9d 58 snake.check_over(lcd); // check if the game is over
694617778 22:51fb86b48d9d 59 lcd.refresh(); // refresh the lcd
694617778 0:b67614a0c4cf 60 }
694617778 0:b67614a0c4cf 61
694617778 0:b67614a0c4cf 62 // initialies all classes and libraries
694617778 0:b67614a0c4cf 63 void init()
694617778 0:b67614a0c4cf 64 {
694617778 20:6ef06ba15666 65 // need to initialise Gamepad, engine, sanke and clear the screen
694617778 22:51fb86b48d9d 66 pad.init(); // initialise the Gamepad
694617778 20:6ef06ba15666 67 snake.init(2,3); // set the size = 2 and length = 3
694617778 22:51fb86b48d9d 68 engine.init(); // initialise the engine
694617778 22:51fb86b48d9d 69 finger.init(); // initialise the figner game
694617778 22:51fb86b48d9d 70 lcd.clear(); // initialise the lcd
694617778 0:b67614a0c4cf 71 }
694617778 0:b67614a0c4cf 72
694617778 16:a52b2441c5ae 73 //gameover screen
694617778 4:323f42022d87 74 void over()
694617778 4:323f42022d87 75 {
694617778 16:a52b2441c5ae 76 int s = snake.get_score(); // get the scores to show at the gameover display
694617778 20:6ef06ba15666 77 int hs = snake.hscore; // get the heighest record to show at the gameover display
694617778 22:51fb86b48d9d 78 engine.gameover(pad,lcd,s,hs); // show the gameover display
694617778 1:b49c36604125 79
694617778 4:323f42022d87 80 }