Wang Lin 201090174

Dependencies:   mbed Gamepad N5110 FXOS8700Q

Committer:
a1115921303
Date:
Mon May 06 06:05:10 2019 +0000
Revision:
13:febf9fbb502f
Parent:
12:121ba031343a
Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
a1115921303 12:121ba031343a 1 #include "main.h"
a1115921303 12:121ba031343a 2 #define SNAKE_LENGTH 100
a1115921303 12:121ba031343a 3 #define SNAKE_SIZE 2
eencae 0:e1442f3aa3c7 4 void init();
a1115921303 12:121ba031343a 5 void welcome();
eencae 0:e1442f3aa3c7 6 void render();
a1115921303 12:121ba031343a 7 void initsnake();
a1115921303 13:febf9fbb502f 8 void speed();
a1115921303 13:febf9fbb502f 9 int dif = 1;
a1115921303 13:febf9fbb502f 10
a1115921303 13:febf9fbb502f 11 /*
a1115921303 13:febf9fbb502f 12 ELEC 2645 Embedded System Project
a1115921303 13:febf9fbb502f 13 School of Electronic & Electrical Engineering
a1115921303 13:febf9fbb502f 14 University of Leeds
a1115921303 13:febf9fbb502f 15
a1115921303 13:febf9fbb502f 16 Name:Wang Lin
a1115921303 13:febf9fbb502f 17 Username:wang lin
a1115921303 13:febf9fbb502f 18 Student ID Number:201090174
a1115921303 13:febf9fbb502f 19 Date:May 2019
a1115921303 13:febf9fbb502f 20 */
a1115921303 12:121ba031343a 21
eencae 0:e1442f3aa3c7 22 int main()
eencae 0:e1442f3aa3c7 23 {
a1115921303 13:febf9fbb502f 24 // the main loop of the whole project
a1115921303 13:febf9fbb502f 25 while(1){
eencae 6:d9d05b321b4d 26 init(); // initialise and then display welcome screen...
eencae 6:d9d05b321b4d 27 welcome(); // waiting for the user to start
a1115921303 13:febf9fbb502f 28 speed(); // the page of choose difficulty
a1115921303 12:121ba031343a 29 initsnake();//intilize the variables
a1115921303 13:febf9fbb502f 30 defineSnake();//define the body of snake
a1115921303 13:febf9fbb502f 31 SpwanFood();//generate the firstfood
a1115921303 13:febf9fbb502f 32
a1115921303 13:febf9fbb502f 33 // the loop of game, if snake is dead jump to the gameover page
a1115921303 13:febf9fbb502f 34 while(live == 1 ) {
a1115921303 12:121ba031343a 35 lcd.clear();
a1115921303 12:121ba031343a 36 render();
a1115921303 13:febf9fbb502f 37 DrawSnake();
a1115921303 13:febf9fbb502f 38 drawFood();
a1115921303 12:121ba031343a 39 MoveSnake();
a1115921303 12:121ba031343a 40 EatFood();
a1115921303 13:febf9fbb502f 41 Break();
a1115921303 12:121ba031343a 42 lcd.refresh();
a1115921303 13:febf9fbb502f 43 wait(waittime/2);
a1115921303 13:febf9fbb502f 44 }
a1115921303 13:febf9fbb502f 45 //game over page , if peass back, jump this loop and restart
a1115921303 13:febf9fbb502f 46 while(pad.check_event(Gamepad::BACK_PRESSED) == false){
a1115921303 13:febf9fbb502f 47 DeadSnake();
a1115921303 13:febf9fbb502f 48 lcd.refresh();
a1115921303 13:febf9fbb502f 49 }
a1115921303 12:121ba031343a 50
a1115921303 13:febf9fbb502f 51 }
eencae 0:e1442f3aa3c7 52 }
eencae 0:e1442f3aa3c7 53 void init()
eencae 0:e1442f3aa3c7 54 {
eencae 3:910d7e87f367 55 // need to initialise LCD and Gamepad
eencae 0:e1442f3aa3c7 56 lcd.init();
eencae 0:e1442f3aa3c7 57 pad.init();
eencae 3:910d7e87f367 58
eencae 0:e1442f3aa3c7 59 }
a1115921303 12:121ba031343a 60
eencae 3:910d7e87f367 61 void welcome() {
a1115921303 12:121ba031343a 62
a1115921303 12:121ba031343a 63 lcd.clear();
a1115921303 13:febf9fbb502f 64 lcd.printString("WELCOME!",19,0);
a1115921303 13:febf9fbb502f 65 lcd.printString(" SNAKE! ",20,1);
a1115921303 13:febf9fbb502f 66 lcd.printString(" Press A ",16,2);
a1115921303 13:febf9fbb502f 67 lcd.printString("to choose ",18,3);
a1115921303 13:febf9fbb502f 68 lcd.printString("difficulty",17,4);
eencae 3:910d7e87f367 69 lcd.refresh();
eencae 3:910d7e87f367 70
a1115921303 13:febf9fbb502f 71 // wait flashing LEDs until A button is pressed
a1115921303 13:febf9fbb502f 72 while ( pad.check_event(Gamepad::A_PRESSED) == false) {
a1115921303 13:febf9fbb502f 73 lcd.refresh();
eencae 3:910d7e87f367 74 }
a1115921303 12:121ba031343a 75 }
eencae 3:910d7e87f367 76
a1115921303 12:121ba031343a 77 void render() {
a1115921303 12:121ba031343a 78
a1115921303 13:febf9fbb502f 79 lcd.drawRect(0,0,83,47,FILL_TRANSPARENT); //daaw the map
a1115921303 12:121ba031343a 80
a1115921303 12:121ba031343a 81 }
a1115921303 13:febf9fbb502f 82 //initialise the varaiables of snake
a1115921303 12:121ba031343a 83 void initsnake() {
a1115921303 12:121ba031343a 84 SNAKE.snakezb[0].x = 30;
a1115921303 12:121ba031343a 85 SNAKE.snakezb[0].y = 20;
a1115921303 12:121ba031343a 86 SNAKE.n = 3; //inital length of snake
a1115921303 12:121ba031343a 87 Dir = 3;
a1115921303 13:febf9fbb502f 88 live = 1;
a1115921303 13:febf9fbb502f 89 Score = 0;
a1115921303 13:febf9fbb502f 90
a1115921303 12:121ba031343a 91
a1115921303 12:121ba031343a 92 }
a1115921303 13:febf9fbb502f 93 // the difficulty choose module throuth joystick or direction button
a1115921303 13:febf9fbb502f 94 void speed(){
a1115921303 13:febf9fbb502f 95 while(pad.check_event(Gamepad::START_PRESSED) == false){
a1115921303 13:febf9fbb502f 96 lcd.clear();
a1115921303 13:febf9fbb502f 97 lcd.printString("CHOOSE",25,0);
a1115921303 13:febf9fbb502f 98 lcd.printString("difficulty",15,1);
a1115921303 13:febf9fbb502f 99 lcd.printString("Press START",13,4);
a1115921303 13:febf9fbb502f 100 lcd.printString("to Start",18,5);//user interface guide to choose difficulty
a1115921303 13:febf9fbb502f 101 if(dif == 1){
a1115921303 13:febf9fbb502f 102 waittime = 1;
a1115921303 13:febf9fbb502f 103 }
a1115921303 13:febf9fbb502f 104 else if (dif == 2){
a1115921303 13:febf9fbb502f 105 waittime = 0.3;
a1115921303 13:febf9fbb502f 106 }
a1115921303 13:febf9fbb502f 107 else if (dif == 3){
a1115921303 13:febf9fbb502f 108 waittime = 0.2;
a1115921303 13:febf9fbb502f 109 }
a1115921303 13:febf9fbb502f 110 else if (dif == 4){
a1115921303 13:febf9fbb502f 111 waittime = 0.1;
a1115921303 13:febf9fbb502f 112 }
a1115921303 13:febf9fbb502f 113 else if (dif == 5){
a1115921303 13:febf9fbb502f 114 waittime = 0.05;
a1115921303 13:febf9fbb502f 115 }
a1115921303 13:febf9fbb502f 116 if(dif > 5){
a1115921303 13:febf9fbb502f 117 dif = 5;
a1115921303 13:febf9fbb502f 118 }
a1115921303 13:febf9fbb502f 119 else if(dif < 1){
a1115921303 13:febf9fbb502f 120 dif = 1;
a1115921303 13:febf9fbb502f 121 }
a1115921303 13:febf9fbb502f 122 d = pad.get_direction();
a1115921303 13:febf9fbb502f 123 //if difficulty is larger or less than the limitation , make back to the limitation
a1115921303 13:febf9fbb502f 124 if(pad.check_event(Gamepad::Y_PRESSED) || d == N){
a1115921303 13:febf9fbb502f 125 dif = dif + 1;
a1115921303 13:febf9fbb502f 126 }
a1115921303 13:febf9fbb502f 127 if(pad.check_event(Gamepad::A_PRESSED) || d == S){
a1115921303 13:febf9fbb502f 128 dif = dif - 1;
a1115921303 13:febf9fbb502f 129 }
a1115921303 13:febf9fbb502f 130 char buffer[1];
a1115921303 13:febf9fbb502f 131 sprintf(buffer,"%d",dif);
a1115921303 13:febf9fbb502f 132 lcd.printString(buffer,40,2);
a1115921303 13:febf9fbb502f 133 lcd.refresh();
a1115921303 13:febf9fbb502f 134 }
a1115921303 13:febf9fbb502f 135
a1115921303 13:febf9fbb502f 136 }
a1115921303 12:121ba031343a 137