Li Ruofan 201199450
Dependencies: mbed
Snake/Snake.cpp@1:bd7c99a5bd10, 2020-05-24 (annotated)
- Committer:
- DannyLee
- Date:
- Sun May 24 08:16:35 2020 +0000
- Revision:
- 1:bd7c99a5bd10
- Parent:
- 0:80a59a49d504
Li Ruofan 201199450
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DannyLee | 0:80a59a49d504 | 1 | #include "Snake.h" |
DannyLee | 0:80a59a49d504 | 2 | |
DannyLee | 0:80a59a49d504 | 3 | Snake::Snake(){} |
DannyLee | 0:80a59a49d504 | 4 | Snake::~Snake(){} |
DannyLee | 0:80a59a49d504 | 5 | |
DannyLee | 0:80a59a49d504 | 6 | pos apple; |
DannyLee | 0:80a59a49d504 | 7 | Body greedy; |
DannyLee | 0:80a59a49d504 | 8 | |
DannyLee | 0:80a59a49d504 | 9 | void Snake::drawApple(N5110 &lcd){ |
DannyLee | 0:80a59a49d504 | 10 | |
DannyLee | 0:80a59a49d504 | 11 | lcd.setPixel(apple.x,apple.y); |
DannyLee | 0:80a59a49d504 | 12 | |
DannyLee | 0:80a59a49d504 | 13 | }//end drawing |
DannyLee | 0:80a59a49d504 | 14 | void Snake::initApple(){ |
DannyLee | 0:80a59a49d504 | 15 | srand(time(NULL)); |
DannyLee | 0:80a59a49d504 | 16 | apple.x = (rand()%49)+6 ; |
DannyLee | 0:80a59a49d504 | 17 | apple.y = (rand()%25)+9; |
DannyLee | 0:80a59a49d504 | 18 | }// end initializing |
DannyLee | 0:80a59a49d504 | 19 | |
DannyLee | 0:80a59a49d504 | 20 | void Snake::produceApple(){ |
DannyLee | 0:80a59a49d504 | 21 | |
DannyLee | 0:80a59a49d504 | 22 | srand(time(NULL)); |
DannyLee | 0:80a59a49d504 | 23 | apple.x=(rand()%49)+6; |
DannyLee | 0:80a59a49d504 | 24 | apple.y=(rand()%25)+9; //set apple's position |
DannyLee | 0:80a59a49d504 | 25 | |
DannyLee | 0:80a59a49d504 | 26 | } |
DannyLee | 0:80a59a49d504 | 27 | |
DannyLee | 0:80a59a49d504 | 28 | pos Snake::getPos(){ |
DannyLee | 1:bd7c99a5bd10 | 29 | return apple; |
DannyLee | 0:80a59a49d504 | 30 | } //end producing |
DannyLee | 0:80a59a49d504 | 31 | |
DannyLee | 1:bd7c99a5bd10 | 32 | void Snake::gameover(N5110 &lcd){ |
DannyLee | 0:80a59a49d504 | 33 | |
DannyLee | 1:bd7c99a5bd10 | 34 | Life--; |
DannyLee | 0:80a59a49d504 | 35 | for (;Life==0;){ |
DannyLee | 0:80a59a49d504 | 36 | |
DannyLee | 0:80a59a49d504 | 37 | lcd.clear(); |
DannyLee | 1:bd7c99a5bd10 | 38 | lcd.printString(" You dead ",0,1); |
DannyLee | 1:bd7c99a5bd10 | 39 | lcd.printString(" Score: ",0,3); |
DannyLee | 0:80a59a49d504 | 40 | |
DannyLee | 0:80a59a49d504 | 41 | char buffer[2]; |
DannyLee | 0:80a59a49d504 | 42 | sprintf(buffer,"%2d",L-6); |
DannyLee | 1:bd7c99a5bd10 | 43 | lcd.printString(buffer,60,3); |
DannyLee | 0:80a59a49d504 | 44 | |
DannyLee | 1:bd7c99a5bd10 | 45 | lcd.printString(" Reset the pad",0,4); |
DannyLee | 1:bd7c99a5bd10 | 46 | lcd.printString(" to restart ",0,5); |
DannyLee | 0:80a59a49d504 | 47 | lcd.refresh(); |
DannyLee | 1:bd7c99a5bd10 | 48 | } |
DannyLee | 1:bd7c99a5bd10 | 49 | init(beginX,beginY,beginL,Life); |
DannyLee | 1:bd7c99a5bd10 | 50 | |
DannyLee | 1:bd7c99a5bd10 | 51 | } //Reset the pad to restart the game. |
DannyLee | 0:80a59a49d504 | 52 | |
DannyLee | 1:bd7c99a5bd10 | 53 | |
DannyLee | 0:80a59a49d504 | 54 | void Snake::WallMeet(N5110 &lcd){ |
DannyLee | 0:80a59a49d504 | 55 | |
DannyLee | 0:80a59a49d504 | 56 | if (greedy.bodyX[L]==84 || greedy.bodyX[L]==0 //snake is also called greedy in this game |
DannyLee | 0:80a59a49d504 | 57 | || greedy.bodyY[L]==0 || greedy.bodyY[L]==48 ) //if greedy touch the wall |
DannyLee | 0:80a59a49d504 | 58 | { |
DannyLee | 1:bd7c99a5bd10 | 59 | gameover(lcd); |
DannyLee | 0:80a59a49d504 | 60 | } |
DannyLee | 0:80a59a49d504 | 61 | |
DannyLee | 0:80a59a49d504 | 62 | }//end WallMeet |
DannyLee | 0:80a59a49d504 | 63 | |
DannyLee | 0:80a59a49d504 | 64 | void Snake::checkBodyMeet(N5110 &lcd){ |
DannyLee | 0:80a59a49d504 | 65 | int i = 0; |
DannyLee | 0:80a59a49d504 | 66 | while (i < L - 1){ |
DannyLee | 0:80a59a49d504 | 67 | if (greedy.bodyY[L-1]==greedy.bodyY[i] && greedy.bodyX[L-1]==greedy.bodyX[i] ) |
DannyLee | 0:80a59a49d504 | 68 | { |
DannyLee | 1:bd7c99a5bd10 | 69 | gameover(lcd); |
DannyLee | 0:80a59a49d504 | 70 | } |
DannyLee | 0:80a59a49d504 | 71 | i++; |
DannyLee | 0:80a59a49d504 | 72 | } |
DannyLee | 0:80a59a49d504 | 73 | } |
DannyLee | 0:80a59a49d504 | 74 | |
DannyLee | 0:80a59a49d504 | 75 | void Snake::produceBody(){ |
DannyLee | 0:80a59a49d504 | 76 | int i = 0; |
DannyLee | 0:80a59a49d504 | 77 | while (i < L){ |
DannyLee | 0:80a59a49d504 | 78 | greedy.bodyX[i]=beginX+i; |
DannyLee | 0:80a59a49d504 | 79 | greedy.bodyY[i]=beginY; |
DannyLee | 0:80a59a49d504 | 80 | greedy._Joystick[i]=1; |
DannyLee | 0:80a59a49d504 | 81 | i++; |
DannyLee | 0:80a59a49d504 | 82 | } |
DannyLee | 0:80a59a49d504 | 83 | greedy.bodyX[L] = greedy.bodyX[L-1]; |
DannyLee | 0:80a59a49d504 | 84 | greedy.bodyY[L] = greedy.bodyY[L-1]; |
DannyLee | 0:80a59a49d504 | 85 | greedy.bodyX[L+1]= greedy.bodyX[L] + 1; |
DannyLee | 0:80a59a49d504 | 86 | greedy.bodyY[L+1]= greedy.bodyY[L] + 1; |
DannyLee | 0:80a59a49d504 | 87 | }//end of produceBody |
DannyLee | 0:80a59a49d504 | 88 | |
DannyLee | 0:80a59a49d504 | 89 | void Snake::init(int _x, int _y, int _L, int _life){//initalize variables |
DannyLee | 0:80a59a49d504 | 90 | |
DannyLee | 0:80a59a49d504 | 91 | initApple(); //initialize apple |
DannyLee | 0:80a59a49d504 | 92 | beginX= _x; |
DannyLee | 0:80a59a49d504 | 93 | beginY= _y; |
DannyLee | 0:80a59a49d504 | 94 | beginL= _L; |
DannyLee | 0:80a59a49d504 | 95 | Life= _life; |
DannyLee | 0:80a59a49d504 | 96 | L = _L; //save variables for further coding |
DannyLee | 0:80a59a49d504 | 97 | initApple(); |
DannyLee | 0:80a59a49d504 | 98 | produceBody(); |
DannyLee | 0:80a59a49d504 | 99 | printf("xog is %d ", greedy.bodyX[L-1]); |
DannyLee | 0:80a59a49d504 | 100 | }//end of init |
DannyLee | 0:80a59a49d504 | 101 | |
DannyLee | 0:80a59a49d504 | 102 | void Snake::draw(N5110 &lcd){ |
DannyLee | 1:bd7c99a5bd10 | 103 | WallMeet(lcd); |
DannyLee | 1:bd7c99a5bd10 | 104 | checkBodyMeet(lcd); //checke if greedy touch the wall |
DannyLee | 0:80a59a49d504 | 105 | if (Life != 0 ){ |
DannyLee | 0:80a59a49d504 | 106 | lcd.clear(); |
DannyLee | 0:80a59a49d504 | 107 | wait(0.15-L*0.001); |
DannyLee | 1:bd7c99a5bd10 | 108 | drawApple(lcd);//produce apple |
DannyLee | 1:bd7c99a5bd10 | 109 | lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);//create battlefield |
DannyLee | 0:80a59a49d504 | 110 | int i = 0; |
DannyLee | 0:80a59a49d504 | 111 | while (i < L){ |
DannyLee | 0:80a59a49d504 | 112 | lcd.setPixel(greedy.bodyX[i],greedy.bodyY[i]); |
DannyLee | 0:80a59a49d504 | 113 | i++; |
DannyLee | 0:80a59a49d504 | 114 | } |
DannyLee | 0:80a59a49d504 | 115 | lcd.refresh(); |
DannyLee | 0:80a59a49d504 | 116 | } |
DannyLee | 0:80a59a49d504 | 117 | |
DannyLee | 0:80a59a49d504 | 118 | }//end of drawing |
DannyLee | 0:80a59a49d504 | 119 | |
DannyLee | 0:80a59a49d504 | 120 | //set the joysitick |
DannyLee | 0:80a59a49d504 | 121 | void Snake::press(Gamepad &pad){ |
DannyLee | 0:80a59a49d504 | 122 | if( pad.check_event(Gamepad:: A_PRESSED) == true |
DannyLee | 0:80a59a49d504 | 123 | && greedy._Joystick[L-1]!=2) { greedy._Joystick[L-1] = 4 ;} |
DannyLee | 0:80a59a49d504 | 124 | if( pad.check_event(Gamepad:: B_PRESSED) == true |
DannyLee | 0:80a59a49d504 | 125 | && greedy._Joystick[L-1]!=3) { greedy._Joystick[L-1] = 1 ;} |
DannyLee | 0:80a59a49d504 | 126 | if( pad.check_event(Gamepad:: X_PRESSED) == true |
DannyLee | 0:80a59a49d504 | 127 | && greedy._Joystick[L-1]!=1) { greedy._Joystick[L-1] = 3;} |
DannyLee | 0:80a59a49d504 | 128 | if( pad.check_event(Gamepad:: Y_PRESSED) == true |
DannyLee | 0:80a59a49d504 | 129 | && greedy._Joystick[L-1]!=4) { greedy._Joystick[L-1] = 2 ;} |
DannyLee | 0:80a59a49d504 | 130 | } |
DannyLee | 0:80a59a49d504 | 131 | |
DannyLee | 0:80a59a49d504 | 132 | void Snake::changeDir(Gamepad &pad){ |
DannyLee | 0:80a59a49d504 | 133 | |
DannyLee | 0:80a59a49d504 | 134 | switch(greedy._Joystick[L-1]){ |
DannyLee | 0:80a59a49d504 | 135 | case 1: greedy.bodyX[L]++; |
DannyLee | 0:80a59a49d504 | 136 | greedy.bodyX[L+1] = greedy.bodyX[L]+1; break; |
DannyLee | 0:80a59a49d504 | 137 | case 2: greedy.bodyY[L]--; |
DannyLee | 0:80a59a49d504 | 138 | greedy.bodyY[L+1] = greedy.bodyY[L]-1; break; |
DannyLee | 0:80a59a49d504 | 139 | case 3: greedy.bodyX[L]--; |
DannyLee | 0:80a59a49d504 | 140 | greedy.bodyX[L+1] = greedy.bodyX[L]-1; break; |
DannyLee | 0:80a59a49d504 | 141 | case 4: greedy.bodyY[L]++; |
DannyLee | 0:80a59a49d504 | 142 | greedy.bodyY[L+1] = greedy.bodyY[L]+1; break; |
DannyLee | 0:80a59a49d504 | 143 | } //change direction |
DannyLee | 0:80a59a49d504 | 144 | |
DannyLee | 0:80a59a49d504 | 145 | int i = 0; //reset body |
DannyLee | 0:80a59a49d504 | 146 | while (i < L){ |
DannyLee | 0:80a59a49d504 | 147 | greedy.bodyX[i]=greedy.bodyX[i+1]; |
DannyLee | 0:80a59a49d504 | 148 | greedy.bodyY[i]=greedy.bodyY[i+1]; |
DannyLee | 0:80a59a49d504 | 149 | i++; |
DannyLee | 0:80a59a49d504 | 150 | } |
DannyLee | 0:80a59a49d504 | 151 | |
DannyLee | 0:80a59a49d504 | 152 | //moving with buttons |
DannyLee | 0:80a59a49d504 | 153 | press(pad); |
DannyLee | 0:80a59a49d504 | 154 | d = pad.get_direction(); |
DannyLee | 0:80a59a49d504 | 155 | if (d==N && greedy._Joystick[L-1]!=4 ){ greedy._Joystick[L-1] = 2; } |
DannyLee | 0:80a59a49d504 | 156 | if (d==E && greedy._Joystick[L-1]!=3 ){ greedy._Joystick[L-1] = 1; } |
DannyLee | 0:80a59a49d504 | 157 | if (d==W && greedy._Joystick[L-1]!=1 ){ greedy._Joystick[L-1] = 3; } |
DannyLee | 0:80a59a49d504 | 158 | if (d==S && greedy._Joystick[L-1]!=2 ){ greedy._Joystick[L-1] = 4; } |
DannyLee | 0:80a59a49d504 | 159 | |
DannyLee | 0:80a59a49d504 | 160 | } // end changeDir |
DannyLee | 0:80a59a49d504 | 161 | |
DannyLee | 0:80a59a49d504 | 162 | void Snake::update(Gamepad &pad){ |
DannyLee | 0:80a59a49d504 | 163 | |
DannyLee | 0:80a59a49d504 | 164 | if (Life!=0){ |
DannyLee | 0:80a59a49d504 | 165 | |
DannyLee | 0:80a59a49d504 | 166 | printf("x+1 is %d", greedy.bodyX[L+1]); |
DannyLee | 0:80a59a49d504 | 167 | printf("y+1 is %d", greedy.bodyY[L+1]); |
DannyLee | 0:80a59a49d504 | 168 | printf("dir+1 is %d", greedy._Joystick[L+1]); |
DannyLee | 0:80a59a49d504 | 169 | printf("length is %d", L); |
DannyLee | 0:80a59a49d504 | 170 | |
DannyLee | 0:80a59a49d504 | 171 | changeDir(pad); |
DannyLee | 0:80a59a49d504 | 172 | |
DannyLee | 0:80a59a49d504 | 173 | printf("updated "); |
DannyLee | 0:80a59a49d504 | 174 | |
DannyLee | 1:bd7c99a5bd10 | 175 | |
DannyLee | 0:80a59a49d504 | 176 | if(eatApple()){ |
DannyLee | 0:80a59a49d504 | 177 | greedy.bodyX[L] = greedy.bodyX[L+1]; |
DannyLee | 0:80a59a49d504 | 178 | greedy.bodyY[L] = greedy.bodyY[L+1]; |
DannyLee | 0:80a59a49d504 | 179 | greedy._Joystick[L] =greedy._Joystick[L-1]; |
DannyLee | 0:80a59a49d504 | 180 | L++; |
DannyLee | 0:80a59a49d504 | 181 | produceApple(); //new apple |
DannyLee | 0:80a59a49d504 | 182 | pad.tone(1000.0,0.2); |
DannyLee | 0:80a59a49d504 | 183 | } |
DannyLee | 0:80a59a49d504 | 184 | } |
DannyLee | 0:80a59a49d504 | 185 | } |
DannyLee | 0:80a59a49d504 | 186 | |
DannyLee | 0:80a59a49d504 | 187 | bool Snake:: eatApple(){ |
DannyLee | 0:80a59a49d504 | 188 | pos applePos = getPos(); //get apple's position |
DannyLee | 0:80a59a49d504 | 189 | return greedy.bodyX[L-1] == applePos.x && greedy.bodyY[L-1]== applePos.y; |
DannyLee | 0:80a59a49d504 | 190 | } |