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 <stdio.h>
a1115921303 12:121ba031343a 2 #include <time.h>
a1115921303 12:121ba031343a 3 #include "mbed.h"
a1115921303 12:121ba031343a 4 #include "Gamepad.h"
a1115921303 12:121ba031343a 5 #include "N5110.h"
a1115921303 12:121ba031343a 6 #define length 100
a1115921303 12:121ba031343a 7 #define SNAKE_SIZE 2
a1115921303 13:febf9fbb502f 8
a1115921303 13:febf9fbb502f 9 /////////////// objects ///////////////
a1115921303 12:121ba031343a 10 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
a1115921303 12:121ba031343a 11 Gamepad pad;
a1115921303 12:121ba031343a 12
a1115921303 12:121ba031343a 13 //////////STRUCTURE///////////////
a1115921303 12:121ba031343a 14 struct Coor {
a1115921303 12:121ba031343a 15 int x;
a1115921303 12:121ba031343a 16 int y;
a1115921303 13:febf9fbb502f 17 }COOR;//define the variable of snake coordinate
a1115921303 12:121ba031343a 18
a1115921303 12:121ba031343a 19 struct Snake {
a1115921303 13:febf9fbb502f 20 Coor snakezb[length]; //the max length of snake, every section length of snake
a1115921303 13:febf9fbb502f 21 int n; //number of snake points
a1115921303 12:121ba031343a 22 }SNAKE;
a1115921303 12:121ba031343a 23 struct Food{
a1115921303 13:febf9fbb502f 24 Coor fc;//define the variable of food coordinate
a1115921303 12:121ba031343a 25 }food;
a1115921303 12:121ba031343a 26
a1115921303 13:febf9fbb502f 27 /** main Class
a1115921303 13:febf9fbb502f 28 @author WangLin, University of Leeds
a1115921303 13:febf9fbb502f 29 @brief all the functions of Snake game
a1115921303 13:febf9fbb502f 30 @date May 2019
a1115921303 13:febf9fbb502f 31 */
a1115921303 13:febf9fbb502f 32
a1115921303 13:febf9fbb502f 33 /**define the body coordinate of snake*/
a1115921303 13:febf9fbb502f 34 void defineSnake();
a1115921303 13:febf9fbb502f 35 /**the music module*/
a1115921303 13:febf9fbb502f 36 void sound();
a1115921303 13:febf9fbb502f 37 /**draw snake
a1115921303 13:febf9fbb502f 38 @param the coor(N5110&lcd)
a1115921303 13:febf9fbb502f 39 */
a1115921303 13:febf9fbb502f 40 void DrawSnake();
a1115921303 13:febf9fbb502f 41 /**make snake to move to different direction
a1115921303 13:febf9fbb502f 42 @param direction(int Dir)
a1115921303 13:febf9fbb502f 43 */
a1115921303 13:febf9fbb502f 44 void MoveSnake();
a1115921303 13:febf9fbb502f 45 /** generate a new food when the previous food has been eated
a1115921303 13:febf9fbb502f 46 */
a1115921303 13:febf9fbb502f 47 void RespawnFood();
a1115921303 13:febf9fbb502f 48 /**when snake dead print gameover and guide to restart
a1115921303 13:febf9fbb502f 49 */
a1115921303 13:febf9fbb502f 50 void DeadSnake();
a1115921303 13:febf9fbb502f 51 float waittime;
a1115921303 13:febf9fbb502f 52 int Score ;
a1115921303 13:febf9fbb502f 53 int live; // Snake's live, if live=0, break the loop.
a1115921303 13:febf9fbb502f 54 int Dir;
a1115921303 13:febf9fbb502f 55 Direction d;
a1115921303 12:121ba031343a 56
a1115921303 12:121ba031343a 57
a1115921303 12:121ba031343a 58
a1115921303 13:febf9fbb502f 59 // define the coordinate of the body of snake
a1115921303 13:febf9fbb502f 60 void defineSnake(){
a1115921303 12:121ba031343a 61 for(int i = 1; i < SNAKE.n; i++){
a1115921303 12:121ba031343a 62 SNAKE.snakezb[i].x = SNAKE.snakezb[i-1].x;
a1115921303 13:febf9fbb502f 63 SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y-1;//draw snake from top to bottom
a1115921303 12:121ba031343a 64 }
a1115921303 12:121ba031343a 65 }
a1115921303 12:121ba031343a 66
a1115921303 12:121ba031343a 67
a1115921303 13:febf9fbb502f 68 //draw a snake by through the whole array
a1115921303 12:121ba031343a 69 void DrawSnake()
a1115921303 12:121ba031343a 70 {
a1115921303 12:121ba031343a 71
a1115921303 12:121ba031343a 72 for(int i = 0; i < SNAKE.n; i++){
a1115921303 12:121ba031343a 73 lcd.drawRect(SNAKE.snakezb[i].x,SNAKE.snakezb[i].y,1,1,FILL_BLACK);
a1115921303 12:121ba031343a 74 }
a1115921303 12:121ba031343a 75
a1115921303 12:121ba031343a 76
a1115921303 12:121ba031343a 77 }
a1115921303 12:121ba031343a 78
a1115921303 13:febf9fbb502f 79 //implement the movement function
a1115921303 12:121ba031343a 80 void MoveSnake()
a1115921303 12:121ba031343a 81 {
a1115921303 13:febf9fbb502f 82 d = pad.get_direction();//get the direction from joystick
a1115921303 13:febf9fbb502f 83 //get the direction from the button
a1115921303 13:febf9fbb502f 84 //prepare for move
a1115921303 12:121ba031343a 85 if (pad.check_event(Gamepad::Y_PRESSED) || d == N){
a1115921303 12:121ba031343a 86 Dir = 1;//up
a1115921303 12:121ba031343a 87 }
a1115921303 12:121ba031343a 88 else if (pad.check_event(Gamepad::A_PRESSED) || d == S){
a1115921303 12:121ba031343a 89 Dir = 2;//down
a1115921303 12:121ba031343a 90 }
a1115921303 12:121ba031343a 91 else if (pad.check_event(Gamepad::X_PRESSED) || d == W){
a1115921303 12:121ba031343a 92 Dir = 3;//left
a1115921303 12:121ba031343a 93 }
a1115921303 12:121ba031343a 94 else if (pad.check_event(Gamepad::B_PRESSED) || d == E){
a1115921303 12:121ba031343a 95 Dir = 4;//right
a1115921303 12:121ba031343a 96 }
a1115921303 13:febf9fbb502f 97 // the main statement to run the snake
a1115921303 13:febf9fbb502f 98 //to let the coordinate of the snake equals to the previous section
a1115921303 13:febf9fbb502f 99 //so the move statement only need to change the first section snake's coordinate
a1115921303 12:121ba031343a 100 for(int i = SNAKE.n; i >0; i--){
a1115921303 12:121ba031343a 101 SNAKE.snakezb[i].x = SNAKE.snakezb[i-1].x;
a1115921303 12:121ba031343a 102 SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y;
a1115921303 12:121ba031343a 103 }
a1115921303 13:febf9fbb502f 104 //let snake move for different direction
a1115921303 13:febf9fbb502f 105 if (Dir == 1 && Dir != 2){
a1115921303 12:121ba031343a 106 SNAKE.snakezb[0].x = SNAKE.snakezb[0].x;
a1115921303 12:121ba031343a 107 SNAKE.snakezb[0].y = SNAKE.snakezb[0].y-1;
a1115921303 13:febf9fbb502f 108 }//move up
a1115921303 13:febf9fbb502f 109 else if (Dir == 2 && Dir != 1){
a1115921303 12:121ba031343a 110 SNAKE.snakezb[0].x = SNAKE.snakezb[0].x;
a1115921303 12:121ba031343a 111 SNAKE.snakezb[0].y = SNAKE.snakezb[0].y+1;
a1115921303 13:febf9fbb502f 112 }//move down
a1115921303 13:febf9fbb502f 113 else if (Dir == 3 && Dir != 4){
a1115921303 12:121ba031343a 114 SNAKE.snakezb[0].x = SNAKE.snakezb[0].x-1;
a1115921303 12:121ba031343a 115 SNAKE.snakezb[0].y = SNAKE.snakezb[0].y;
a1115921303 13:febf9fbb502f 116 }//move left
a1115921303 13:febf9fbb502f 117 else if (Dir == 4 && Dir != 3){
a1115921303 12:121ba031343a 118 SNAKE.snakezb[0].x = SNAKE.snakezb[0].x+1;
a1115921303 12:121ba031343a 119 SNAKE.snakezb[0].y = SNAKE.snakezb[0].y;
a1115921303 13:febf9fbb502f 120 }//move right
a1115921303 12:121ba031343a 121
a1115921303 12:121ba031343a 122 }
a1115921303 12:121ba031343a 123
a1115921303 13:febf9fbb502f 124 //generate a radom food
a1115921303 12:121ba031343a 125 void SpwanFood(){
a1115921303 12:121ba031343a 126 srand(unsigned (time(NULL)));
a1115921303 13:febf9fbb502f 127 food.fc.x = rand() % 80; // get a radom number for the coordinate of food.
a1115921303 12:121ba031343a 128 food.fc.y = rand() % 40;
a1115921303 12:121ba031343a 129
a1115921303 13:febf9fbb502f 130 }
a1115921303 13:febf9fbb502f 131 //print the food
a1115921303 13:febf9fbb502f 132 void drawFood(){
a1115921303 13:febf9fbb502f 133 lcd.drawRect(food.fc.x,food.fc.y,1,1,FILL_BLACK);
a1115921303 13:febf9fbb502f 134 }
a1115921303 13:febf9fbb502f 135 //juge if snake eat food, length increased, score +1 , play sound effect.
a1115921303 13:febf9fbb502f 136 void EatFood(){
a1115921303 13:febf9fbb502f 137 //juge if snake eat food
a1115921303 13:febf9fbb502f 138 if(SNAKE.snakezb[0].x == food.fc.x && SNAKE.snakezb[0].y == food.fc.y ){
a1115921303 13:febf9fbb502f 139 SNAKE.n = SNAKE.n+5;
a1115921303 13:febf9fbb502f 140 SpwanFood();//generate a new food after the previous food had been eated
a1115921303 13:febf9fbb502f 141 pad.tone(650,0.5);// sound effect
a1115921303 13:febf9fbb502f 142 Score = Score +1;// score+1
a1115921303 13:febf9fbb502f 143 char buffer[1];
a1115921303 13:febf9fbb502f 144 sprintf(buffer,"%d",Score);
a1115921303 13:febf9fbb502f 145 lcd.printString(buffer,70,1);//print score
a1115921303 13:febf9fbb502f 146 }
a1115921303 13:febf9fbb502f 147 }
a1115921303 13:febf9fbb502f 148 //juge if snake hit the map border or itself, if hitted then break loop, jump to the DeadSnake mode.
a1115921303 13:febf9fbb502f 149 void Break(){
a1115921303 13:febf9fbb502f 150 //check if snake hit the map border
a1115921303 13:febf9fbb502f 151 if(SNAKE.snakezb[0].x<=0 || SNAKE.snakezb[0].x >= 83 || SNAKE.snakezb[0].y <= 0 || SNAKE.snakezb[0].y >= 47)
a1115921303 13:febf9fbb502f 152 {
a1115921303 13:febf9fbb502f 153 live = 0;
a1115921303 13:febf9fbb502f 154
a1115921303 13:febf9fbb502f 155 }
a1115921303 13:febf9fbb502f 156 //check if the snake hit itself
a1115921303 13:febf9fbb502f 157 for(int i = SNAKE.n-2; i > 0; i--){
a1115921303 13:febf9fbb502f 158 if( SNAKE.snakezb[0].x == SNAKE.snakezb[i].x && SNAKE.snakezb[0].y == SNAKE.snakezb[i].y){
a1115921303 13:febf9fbb502f 159 live = 0;
a1115921303 13:febf9fbb502f 160
a1115921303 13:febf9fbb502f 161 }
a1115921303 13:febf9fbb502f 162 }
a1115921303 13:febf9fbb502f 163 }
a1115921303 13:febf9fbb502f 164
a1115921303 13:febf9fbb502f 165 //if snake dead,print gameover page and a passway to resart
a1115921303 13:febf9fbb502f 166 void DeadSnake(){
a1115921303 13:febf9fbb502f 167 lcd.clear();
a1115921303 13:febf9fbb502f 168 lcd.printString("GAME OVER",0,1 );
a1115921303 13:febf9fbb502f 169 lcd.printString("YOUR SCORE:",0,2);
a1115921303 13:febf9fbb502f 170 lcd.printString("Press Back",1,4);
a1115921303 13:febf9fbb502f 171 lcd.printString("TO Resart",1,5);
a1115921303 13:febf9fbb502f 172 char buffer[1];
a1115921303 13:febf9fbb502f 173 sprintf(buffer,"%d",Score);
a1115921303 13:febf9fbb502f 174 lcd.printString(buffer,65,2);
a1115921303 13:febf9fbb502f 175 lcd.refresh();
a1115921303 13:febf9fbb502f 176 sound();
a1115921303 12:121ba031343a 177
a1115921303 12:121ba031343a 178
a1115921303 12:121ba031343a 179 }
a1115921303 13:febf9fbb502f 180 // the sound moudle
a1115921303 13:febf9fbb502f 181 void sound(){
a1115921303 13:febf9fbb502f 182 pad.tone(440.0,0.25);
a1115921303 13:febf9fbb502f 183 wait(0.27);
a1115921303 13:febf9fbb502f 184 pad.tone(659,0.25);
a1115921303 13:febf9fbb502f 185 wait(0.27);
a1115921303 13:febf9fbb502f 186 pad.tone(659,0.25);
a1115921303 13:febf9fbb502f 187 wait(0.27);
a1115921303 13:febf9fbb502f 188 pad.tone(440.0,0.25);// 及你太美
a1115921303 13:febf9fbb502f 189 wait(0.85);
a1115921303 13:febf9fbb502f 190 pad.tone(440.0,0.25);
a1115921303 13:febf9fbb502f 191 wait(0.2);
a1115921303 13:febf9fbb502f 192 pad.tone(440.0,0.25);// baby
a1115921303 13:febf9fbb502f 193 wait(0.85);
a1115921303 13:febf9fbb502f 194 pad.tone(440.0,0.25);
a1115921303 13:febf9fbb502f 195 wait(0.27);
a1115921303 13:febf9fbb502f 196 pad.tone(659,0.25);
a1115921303 13:febf9fbb502f 197 wait(0.27);
a1115921303 13:febf9fbb502f 198 pad.tone(659,0.25);
a1115921303 13:febf9fbb502f 199 wait(0.27);
a1115921303 13:febf9fbb502f 200 pad.tone(440.0,0.25);
a1115921303 13:febf9fbb502f 201 wait(0.85);
a1115921303 13:febf9fbb502f 202 pad.tone(440.0,0.25);
a1115921303 13:febf9fbb502f 203 wait(0.2);
a1115921303 13:febf9fbb502f 204 pad.tone(440.0,0.25);
a1115921303 13:febf9fbb502f 205 wait(0.8);
a1115921303 13:febf9fbb502f 206 }