Yunting Zou 201199716

Dependencies:   mbed MotionSensor

Committer:
zhouyun123
Date:
Thu May 14 17:18:58 2020 +0000
Revision:
3:4bd22344278d
Parent:
2:a2f88b2d5da4
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zhouyun123 0:047e14f53977 1 /*
zhouyun123 0:047e14f53977 2 ELEC 2645 Embedded System Project
zhouyun123 0:047e14f53977 3 School of Electronic & Electrical Engineering
zhouyun123 0:047e14f53977 4 University of Leeds
zhouyun123 0:047e14f53977 5
zhouyun123 0:047e14f53977 6 Name:Zou yunting
zhouyun123 0:047e14f53977 7 Username:el17y2z
zhouyun123 0:047e14f53977 8 Student ID Number:201199716
zhouyun123 1:9a8033d80067 9 Date:15th May 2020
zhouyun123 0:047e14f53977 10 */
zhouyun123 0:047e14f53977 11
zhouyun123 0:047e14f53977 12
zhouyun123 0:047e14f53977 13 #include "viper.h"
zhouyun123 0:047e14f53977 14 #include "story.h"
zhouyun123 0:047e14f53977 15
zhouyun123 1:9a8033d80067 16 void init(); // initialise and show the welcome screen
zhouyun123 1:9a8033d80067 17 void begin(); // the welcome screen
zhouyun123 1:9a8033d80067 18 void render(); // Draw the ouline of the area
zhouyun123 1:9a8033d80067 19 void showviper(); //Draw the snake
zhouyun123 1:9a8033d80067 20 void speed(); //select the speed of the snake's movment
zhouyun123 3:4bd22344278d 21 void showtarget(); //draw the target food
zhouyun123 1:9a8033d80067 22 void Dead(); //set the failure condition
zhouyun123 1:9a8033d80067 23 void music(); //set the starting music
zhouyun123 1:9a8033d80067 24 void lose(); //set the losing music
zhouyun123 1:9a8033d80067 25 int sp = 0 ; //variable of speed level
zhouyun123 0:047e14f53977 26
zhouyun123 0:047e14f53977 27
zhouyun123 0:047e14f53977 28
zhouyun123 0:047e14f53977 29 int main()
zhouyun123 0:047e14f53977 30 {
zhouyun123 1:9a8033d80067 31 // the main function of the whole project
zhouyun123 0:047e14f53977 32 while(1){
zhouyun123 1:9a8033d80067 33 init(); // initialise and show the welcome screen
zhouyun123 0:047e14f53977 34 begin();
zhouyun123 1:9a8033d80067 35 music(); // show the starting screen
zhouyun123 0:047e14f53977 36 firststory();
zhouyun123 0:047e14f53977 37 graph();
zhouyun123 1:9a8033d80067 38 speed(); // set the speed of snake's movement
zhouyun123 0:047e14f53977 39 showviper();//@intilize the snake's location
zhouyun123 1:9a8033d80067 40 viper();//set the parameter of snake
zhouyun123 1:9a8033d80067 41 goal();//generate the first target food
zhouyun123 0:047e14f53977 42
zhouyun123 0:047e14f53977 43 //@ the loop of game, if snake is dead jump to the gameover page
zhouyun123 0:047e14f53977 44 while(HP == 1 ) {
zhouyun123 0:047e14f53977 45 lcd.clear();
zhouyun123 0:047e14f53977 46 render();
zhouyun123 0:047e14f53977 47 viper();
zhouyun123 3:4bd22344278d 48 showtarget();
zhouyun123 0:047e14f53977 49 Movement();
zhouyun123 0:047e14f53977 50 gettarget();
zhouyun123 0:047e14f53977 51 Dead();
zhouyun123 0:047e14f53977 52 lcd.refresh();
zhouyun123 0:047e14f53977 53 wait(wtime/2);
zhouyun123 0:047e14f53977 54 }
zhouyun123 1:9a8033d80067 55 //turn to the game over page , press the back button to restart.
zhouyun123 0:047e14f53977 56 while(pad.check_event(Gamepad::BACK_PRESSED) == false){
zhouyun123 0:047e14f53977 57 gameover();
zhouyun123 0:047e14f53977 58 lcd.refresh();
zhouyun123 0:047e14f53977 59 }
zhouyun123 0:047e14f53977 60 }
zhouyun123 0:047e14f53977 61 }
zhouyun123 0:047e14f53977 62
zhouyun123 0:047e14f53977 63
zhouyun123 0:047e14f53977 64 void init() //1
zhouyun123 0:047e14f53977 65 {
zhouyun123 1:9a8033d80067 66 // initialise LCD and Gamepad
zhouyun123 0:047e14f53977 67 lcd.init();
zhouyun123 0:047e14f53977 68 pad.init();
zhouyun123 0:047e14f53977 69
zhouyun123 0:047e14f53977 70 }
zhouyun123 0:047e14f53977 71
zhouyun123 2:a2f88b2d5da4 72
zhouyun123 1:9a8033d80067 73 //the welcome screen
zhouyun123 0:047e14f53977 74 void begin() { //2
zhouyun123 0:047e14f53977 75
zhouyun123 0:047e14f53977 76 lcd.clear();
zhouyun123 0:047e14f53977 77 lcd.printString("Gluttonous!",0,0);
zhouyun123 0:047e14f53977 78 lcd.printString("snake!!!!",0,1);
zhouyun123 0:047e14f53977 79 lcd.printString(" Zou Yunting",0,2);
zhouyun123 0:047e14f53977 80 lcd.printString(" ID:201199716 ",0,3);
zhouyun123 0:047e14f53977 81 lcd.printString(" welcome! ",0,4);
zhouyun123 0:047e14f53977 82 lcd.printString("press A button",0,5);
zhouyun123 0:047e14f53977 83 lcd.refresh();
zhouyun123 0:047e14f53977 84
zhouyun123 0:047e14f53977 85 // Press the A button to turn to the next screen
zhouyun123 0:047e14f53977 86 while ( pad.check_event(Gamepad::A_PRESSED) == false) {
zhouyun123 0:047e14f53977 87 lcd.refresh();
zhouyun123 0:047e14f53977 88 }
zhouyun123 0:047e14f53977 89 }
zhouyun123 0:047e14f53977 90
zhouyun123 2:a2f88b2d5da4 91
zhouyun123 1:9a8033d80067 92 //Outline the entire interface
zhouyun123 0:047e14f53977 93 void render() { //3
zhouyun123 0:047e14f53977 94
zhouyun123 0:047e14f53977 95 lcd.drawRect(0,0,80,40,FILL_TRANSPARENT);
zhouyun123 0:047e14f53977 96
zhouyun123 0:047e14f53977 97 }
zhouyun123 0:047e14f53977 98
zhouyun123 0:047e14f53977 99
zhouyun123 1:9a8033d80067 100 //initialise the parameters of snake
zhouyun123 0:047e14f53977 101 void showviper() { //4
zhouyun123 0:047e14f53977 102 SER.head[0].x = 20;
zhouyun123 0:047e14f53977 103 SER.head[0].y = 30;
zhouyun123 0:047e14f53977 104 SER.n = 5; //inital length of snake
zhouyun123 0:047e14f53977 105 HP = 1;
zhouyun123 0:047e14f53977 106 score = 0;
zhouyun123 0:047e14f53977 107 }
zhouyun123 0:047e14f53977 108
zhouyun123 0:047e14f53977 109
zhouyun123 1:9a8033d80067 110 // select the speed of snale's movement by the A and B buttoms
zhouyun123 0:047e14f53977 111 void speed(){ //5
zhouyun123 0:047e14f53977 112 while(pad.check_event(Gamepad::START_PRESSED) == false){
zhouyun123 0:047e14f53977 113 lcd.clear();
zhouyun123 0:047e14f53977 114 lcd.printString("CHOOSE",25,0);
zhouyun123 0:047e14f53977 115 lcd.printString("difficulty",15,1);
zhouyun123 0:047e14f53977 116 lcd.printString("press A or B",0,2);
zhouyun123 0:047e14f53977 117 lcd.printString("Press START",13,4);
zhouyun123 0:047e14f53977 118 lcd.printString("to Start",18,5);
zhouyun123 0:047e14f53977 119 if(sp == 1){
zhouyun123 0:047e14f53977 120 wtime = 0.3;
zhouyun123 0:047e14f53977 121 }
zhouyun123 0:047e14f53977 122 else if (sp == 2){
zhouyun123 0:047e14f53977 123 wtime = 0.5;
zhouyun123 0:047e14f53977 124 }
zhouyun123 0:047e14f53977 125 //@press A or B button to select the speed
zhouyun123 0:047e14f53977 126 if(pad.check_event(Gamepad::A_PRESSED) ){
zhouyun123 0:047e14f53977 127 sp = 1;
zhouyun123 0:047e14f53977 128 }
zhouyun123 0:047e14f53977 129 else if(pad.check_event(Gamepad::B_PRESSED) ){
zhouyun123 0:047e14f53977 130 sp = 2;
zhouyun123 0:047e14f53977 131 }
zhouyun123 0:047e14f53977 132 char buffer[1];
zhouyun123 0:047e14f53977 133 sprintf(buffer,"%d",sp);
zhouyun123 0:047e14f53977 134 lcd.printString(buffer,40,3);
zhouyun123 0:047e14f53977 135 lcd.refresh();
zhouyun123 0:047e14f53977 136 }
zhouyun123 0:047e14f53977 137 }
zhouyun123 0:047e14f53977 138
zhouyun123 0:047e14f53977 139
zhouyun123 1:9a8033d80067 140 //print the food 5
zhouyun123 3:4bd22344278d 141 void showtarget(){ //6
zhouyun123 0:047e14f53977 142 lcd.drawRect(target.var.x,target.var.y,1,1,FILL_TRANSPARENT);
zhouyun123 0:047e14f53977 143 }
zhouyun123 0:047e14f53977 144
zhouyun123 0:047e14f53977 145
zhouyun123 1:9a8033d80067 146 //if the viper rush out of the border of the screen, game over. 7
zhouyun123 0:047e14f53977 147 void Dead(){
zhouyun123 1:9a8033d80067 148 //judge if snake rush out of the border
zhouyun123 0:047e14f53977 149 if(SER.head[0].x<=0 || SER.head[0].x >= 80 || SER.head[0].y <= 0 || SER.head[0].y >= 40)
zhouyun123 0:047e14f53977 150 {
zhouyun123 0:047e14f53977 151 HP = 0;
zhouyun123 0:047e14f53977 152 }
zhouyun123 0:047e14f53977 153 }
zhouyun123 0:047e14f53977 154
zhouyun123 0:047e14f53977 155
zhouyun123 1:9a8033d80067 156 //if snake dead,print gameover screen and prepare to resart 8
zhouyun123 0:047e14f53977 157 void gameover(){
zhouyun123 0:047e14f53977 158 lcd.clear();
zhouyun123 0:047e14f53977 159 lcd.printString("failure!",0,1 );
zhouyun123 0:047e14f53977 160 lcd.printString("last score:",0,2);
zhouyun123 0:047e14f53977 161 lcd.printString("Press Back",1,4);
zhouyun123 0:047e14f53977 162 lcd.printString("to restart",1,5);
zhouyun123 0:047e14f53977 163 char buffer[1];
zhouyun123 0:047e14f53977 164 sprintf(buffer,"%d",score);
zhouyun123 0:047e14f53977 165 lcd.printString(buffer,65,2);
zhouyun123 0:047e14f53977 166 lcd.refresh();
zhouyun123 0:047e14f53977 167 lose();
zhouyun123 0:047e14f53977 168 }
zhouyun123 0:047e14f53977 169
zhouyun123 0:047e14f53977 170