Wang Lin 201090174

Dependencies:   mbed Gamepad N5110 FXOS8700Q

Files at this revision

API Documentation at this revision

Comitter:
a1115921303
Date:
Mon May 06 06:05:10 2019 +0000
Parent:
12:121ba031343a
Commit message:
Final

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
tests.h Show diff for this revision Revisions of this file
--- a/main.cpp	Fri May 03 17:12:37 2019 +0000
+++ b/main.cpp	Mon May 06 06:05:10 2019 +0000
@@ -5,31 +5,51 @@
 void welcome();
 void render();
 void initsnake();
+void speed();
+int  dif = 1;
+
+/*
+ELEC 2645 Embedded System Project
+School of Electronic & Electrical Engineering
+University of Leeds
+
+Name:Wang Lin
+Username:wang lin
+Student ID Number:201090174
+Date:May 2019
+*/
  
 int main()
 {
- 
+   // the main loop of the whole project
+    while(1){
     init();     // initialise and then display welcome screen...
     welcome();  // waiting for the user to start
-  // first draw the initial frame
+    speed(); // the page of choose difficulty
     initsnake();//intilize the variables
- initSnake();
- SpwanFood();
- 
-  while(1) {
+    defineSnake();//define the body of snake 
+    SpwanFood();//generate the firstfood
+    
+   // the loop of game, if snake is dead jump to the gameover page
+  while(live == 1 ) {
    lcd.clear();
     render();
-   drawFood();
-   DrawSnake();
+    DrawSnake();
+    drawFood();
     MoveSnake();
     EatFood();
+    Break();
     lcd.refresh();
-    Speed();
-    
+    wait(waittime/2);
+    }
+     //game over page , if peass back, jump this loop and restart
+     while(pad.check_event(Gamepad::BACK_PRESSED) == false){
+        DeadSnake();
+        lcd.refresh();
+        }
     
-    }
+   }
 }
- 
 void init()
 {
     // need to initialise LCD and Gamepad 
@@ -41,33 +61,77 @@
 void welcome() {
         
     lcd.clear();    
-    lcd.printString(" SNAKE!  ",0,1);  
-    lcd.printString(" Press Start ",0,2);
-    
+    lcd.printString("WELCOME!",19,0);
+    lcd.printString(" SNAKE!  ",20,1);  
+    lcd.printString(" Press A  ",16,2);
+    lcd.printString("to choose ",18,3);
+    lcd.printString("difficulty",17,4);
     lcd.refresh();
      
-    // wait flashing LEDs until start button is pressed 
-    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
-        pad.leds_on();
-        wait(0.1);
-        pad.leds_off();
-        wait(waittime);
-        lcd.printString("LETS START!",0,2);
+    // wait flashing LEDs until A button is pressed 
+    while ( pad.check_event(Gamepad::A_PRESSED) == false) {
+       lcd.refresh();
     }
 }
  
 void render() {
    
-    lcd.drawRect(0,0,83,47,FILL_TRANSPARENT); 
+    lcd.drawRect(0,0,83,47,FILL_TRANSPARENT); //daaw the map
 
 }
- 
+//initialise the varaiables of snake
 void initsnake() {
     SNAKE.snakezb[0].x = 30;
     SNAKE.snakezb[0].y = 20;
     SNAKE.n = 3;  //inital length of snake
     Dir = 3;
-  
+    live = 1;
+    Score = 0;
+   
     
 }
+// the difficulty choose module throuth joystick or direction button
+void speed(){
+    while(pad.check_event(Gamepad::START_PRESSED) == false){
+    lcd.clear();
+    lcd.printString("CHOOSE",25,0);
+    lcd.printString("difficulty",15,1);
+    lcd.printString("Press START",13,4);
+    lcd.printString("to Start",18,5);//user interface guide to choose difficulty
+    if(dif == 1){
+        waittime = 1;
+        }
+    else if (dif == 2){
+        waittime = 0.3;
+        }
+    else if (dif == 3){
+        waittime = 0.2;
+        }
+    else if (dif == 4){
+        waittime = 0.1;
+        }
+    else if (dif == 5){
+        waittime = 0.05;
+        }
+    if(dif > 5){
+        dif = 5;
+        }
+    else if(dif < 1){
+        dif = 1;
+        }
+    d = pad.get_direction();
+    //if difficulty is larger or less than the limitation , make back to the limitation 
+    if(pad.check_event(Gamepad::Y_PRESSED) || d == N){
+        dif = dif + 1;
+        }
+     if(pad.check_event(Gamepad::A_PRESSED) || d == S){
+        dif = dif - 1;
+        }
+    char buffer[1];
+    sprintf(buffer,"%d",dif);
+    lcd.printString(buffer,40,2);
+    lcd.refresh();
+        }
+    
+    }
     
\ No newline at end of file
--- a/main.h	Fri May 03 17:12:37 2019 +0000
+++ b/main.h	Mon May 06 06:05:10 2019 +0000
@@ -1,12 +1,12 @@
 #include <stdio.h>
-#include <stdlib.h>
 #include <time.h>
 #include "mbed.h"
 #include "Gamepad.h"
 #include "N5110.h"
 #define length 100
 #define SNAKE_SIZE 2 
- 
+
+/////////////// objects /////////////// 
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad pad;
  
@@ -14,53 +14,58 @@
 struct Coor {
     int x;
     int y;    
-}COOR;
+}COOR;//define the variable of snake coordinate
  
 struct Snake {
-    Coor snakezb[length]; //蛇每一届的坐标 蛇最大的长度
-    int n;   //number of snake points 当前节数        
+    Coor snakezb[length]; //the max length of snake, every section length of snake
+    int n;   //number of snake points   
 }SNAKE;
 struct Food{
-   Coor fc;
+   Coor fc;//define the variable of food coordinate
 }food;
  
+/** main Class
+@author WangLin, University of Leeds
+@brief all the functions of Snake game
+@date May 2019
+*/
+
+/**define the body coordinate of snake*/ 
+void defineSnake();
+/**the music module*/
+void sound();
+/**draw snake
+@param the coor(N5110&lcd)
+ */
+void DrawSnake();
+/**make snake to move to different direction
+@param direction(int Dir)
+ */
+void MoveSnake();
+/** generate a new food when the previous food has been eated
+*/
+void RespawnFood();
+/**when snake dead print gameover and guide to restart
+*/
+void DeadSnake();
+float waittime;
+int Score ;
+int live; // Snake's live, if live=0, break the loop.
+int Dir;
+Direction d;
 
  
  
- 
- 
- 
- 
- 
- 
-
-void DrawSnake();
-//void DirSnake();
-void MoveSnake();
-int Dir;
-void RespawnFood();
-//void EatFood();
-//void BreakSnake();
-//void OverSnake();
-//void Speed();
-int waittime;
-Direction d;
- 
- 
-
- 
-
- 
- 
- 
- void initSnake(){
+ // define the coordinate of the body of snake
+ void defineSnake(){
      for(int i = 1; i < SNAKE.n; i++){
          SNAKE.snakezb[i].x = SNAKE.snakezb[i-1].x;
-         SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y-1;
+         SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y-1;//draw snake from top to bottom
          }
      }
 
  
+//draw a snake by through the whole array
 void DrawSnake()
 {
         
@@ -71,9 +76,12 @@
     
 }
  
+//implement the movement function
 void MoveSnake()
 {
-    d = pad.get_direction();
+    d = pad.get_direction();//get the direction from joystick
+    //get the direction from the button 
+    //prepare for move
     if (pad.check_event(Gamepad::Y_PRESSED) || d == N){
         Dir = 1;//up
         }
@@ -86,68 +94,113 @@
     else if (pad.check_event(Gamepad::B_PRESSED) || d == E){
         Dir = 4;//right
         }
-        
+// the main  statement to run the snake
+//to let the coordinate of the snake equals to the previous section
+//so the move statement only need to change the first section snake's coordinate
     for(int i = SNAKE.n; i >0; i--){
         SNAKE.snakezb[i].x = SNAKE.snakezb[i-1].x;
         SNAKE.snakezb[i].y = SNAKE.snakezb[i-1].y;
         }
-    if (Dir == 1){
+    //let snake move for different direction
+    if (Dir == 1 && Dir != 2){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y-1;
-        }
-    else if (Dir == 2){
+        }//move up
+    else if (Dir == 2 && Dir != 1){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y+1;
-        }
-    else if (Dir == 3){
+        }//move down
+    else if (Dir == 3 && Dir != 4){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x-1;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y;
-        }
-    else if (Dir == 4){
+        }//move left
+    else if (Dir == 4 && Dir != 3){
         SNAKE.snakezb[0].x = SNAKE.snakezb[0].x+1;
         SNAKE.snakezb[0].y = SNAKE.snakezb[0].y;
-        }
+        }//move right
     
  }
 
+//generate a radom food
 void SpwanFood(){
     srand(unsigned (time(NULL)));
-    food.fc.x = rand() % 80;
+    food.fc.x = rand() % 80; // get a radom number for the coordinate of food.
     food.fc.y = rand() % 40;
     
-    
+    }
+//print the food
+void drawFood(){
+    lcd.drawRect(food.fc.x,food.fc.y,1,1,FILL_BLACK);
+    }
+//juge if snake eat food, length increased, score +1 , play sound effect.
+void EatFood(){
+//juge if snake eat food
+    if(SNAKE.snakezb[0].x == food.fc.x && SNAKE.snakezb[0].y == food.fc.y ){
+        SNAKE.n = SNAKE.n+5;
+       SpwanFood();//generate a new food after the previous food had been eated
+       pad.tone(650,0.5);// sound effect
+       Score = Score +1;// score+1
+       char buffer[1];
+       sprintf(buffer,"%d",Score);
+       lcd.printString(buffer,70,1);//print score
+        }
+    }
+//juge if snake hit the map border or itself, if hitted then break loop, jump to the DeadSnake mode.
+void Break(){
+    //check if snake hit the map border
+    if(SNAKE.snakezb[0].x<=0 || SNAKE.snakezb[0].x >= 83 || SNAKE.snakezb[0].y <= 0  || SNAKE.snakezb[0].y >= 47)
+    {
+        live = 0;
+        
+        }
+       //check if the snake hit itself 
+        for(int i = SNAKE.n-2; i > 0; i--){
+           if( SNAKE.snakezb[0].x == SNAKE.snakezb[i].x && SNAKE.snakezb[0].y == SNAKE.snakezb[i].y){
+            live =  0;
+            
+            }
+            }
+    }
+
+//if snake dead,print gameover page and a passway to resart
+void DeadSnake(){
+    lcd.clear();
+    lcd.printString("GAME  OVER",0,1 );
+    lcd.printString("YOUR SCORE:",0,2);
+    lcd.printString("Press Back",1,4);
+    lcd.printString("TO Resart",1,5);
+    char buffer[1];
+    sprintf(buffer,"%d",Score);
+    lcd.printString(buffer,65,2);
+    lcd.refresh();
+    sound();
     
     
     }
-void drawFood(){
-    lcd.drawRect(food.fc.x,food.fc.y,1,1,FILL_BLACK);
-    }
-void EatFood(){
-
-    if(SNAKE.snakezb[0].x == food.fc.x && SNAKE.snakezb[0].y == food.fc.y ){
-        SNAKE.n = SNAKE.n+1;
-       SpwanFood();
-        }
-    }
-void DeadSnake(){
-    
-    }
-
- 
-void Speed()
-{
-    if(pad.check_event(Gamepad::L_PRESSED))
-    {
-      waittime = 0.05;
-      wait(waittime);  
-      }
-    else if(pad.check_event(Gamepad::R_PRESSED))
-    {
-      waittime = 0.5;
-      wait(waittime);  
-      }
-    else
-    {
-      wait(0.3);
-      }    
-}
\ No newline at end of file
+// the sound moudle
+void sound(){
+    pad.tone(440.0,0.25);
+       wait(0.27);
+       pad.tone(659,0.25);
+        wait(0.27);
+       pad.tone(659,0.25);
+       wait(0.27);
+       pad.tone(440.0,0.25);// 及你太美
+       wait(0.85);
+       pad.tone(440.0,0.25);
+       wait(0.2);
+       pad.tone(440.0,0.25);// baby
+       wait(0.85);
+       pad.tone(440.0,0.25);
+       wait(0.27);
+       pad.tone(659,0.25);
+        wait(0.27);
+       pad.tone(659,0.25);
+       wait(0.27);
+       pad.tone(440.0,0.25);
+       wait(0.85);
+       pad.tone(440.0,0.25);
+       wait(0.2);
+       pad.tone(440.0,0.25);
+       wait(0.8);
+    }
\ No newline at end of file
--- a/tests.h	Fri May 03 17:12:37 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,43 +0,0 @@
-#ifndef TESTS_H
-#define TESTS_H
-
-#include "Ball-test.h"
-
-/**
- * @brief Run all the tests for this program
- *
- * @returns The number of tests that failed
- */
-int run_all_tests()
-{
-    int n_tests_failed = 0; // A log of the number of tests that have failed
-
-    // Run the Ball_test_movement test
-    printf("Testing Ball_test_movement...\n");
-    bool this_test_passed = Ball_test_movement();
-
-    // Print out the result of this test
-    if (this_test_passed) {
-        printf("...Passed!\n");
-    }
-    else {
-        printf("...FAILED!\n");
-        ++n_tests_failed; // Increment number of failures
-    }
-    
-    // Repeat the above for each testing function...
-    // ...
-    // ...
-
-    // Finish by printing a summary of the tests
-    if (n_tests_failed > 0) {
-        printf("%d tests FAILED!\n", n_tests_failed);
-    }
-    else {
-        printf("All tests passed!\n");
-    }
-
-    return n_tests_failed;
-}
-
-#endif
\ No newline at end of file