bird

Dependencies:   mbed

Committer:
1013373474
Date:
Sun May 05 19:00:12 2019 +0000
Revision:
4:cd9a60a75717
Parent:
3:d9c2fc1d5984
gjhg

Who changed what in which revision?

UserRevisionLine numberNew contents of line
1013373474 0:3887ebee1b37 1 #include "mbed.h"
1013373474 0:3887ebee1b37 2 #include "N5110.h"
1013373474 0:3887ebee1b37 3 #include "Gamepad.h"
1013373474 0:3887ebee1b37 4
1013373474 2:fb9c05867677 5 /**
1013373474 2:fb9c05867677 6 *@brief Data struct includes the bird position.
1013373474 2:fb9c05867677 7 */
1013373474 0:3887ebee1b37 8
1013373474 0:3887ebee1b37 9
1013373474 0:3887ebee1b37 10 struct Data{
1013373474 0:3887ebee1b37 11 int y;
1013373474 0:3887ebee1b37 12 };
1013373474 2:fb9c05867677 13
1013373474 2:fb9c05867677 14
1013373474 3:d9c2fc1d5984 15 /** bird Class
1013373474 2:fb9c05867677 16 *@brief Data used to make the bird
1013373474 4:cd9a60a75717 17 *@author Zhou Yongzhi
1013373474 4:cd9a60a75717 18 *@date May 2019
1013373474 3:d9c2fc1d5984 19
1013373474 3:d9c2fc1d5984 20 @code
1013373474 3:d9c2fc1d5984 21
1013373474 3:d9c2fc1d5984 22
1013373474 3:d9c2fc1d5984 23 #include "structure.h"
1013373474 3:d9c2fc1d5984 24 #include "structure_2.h"
1013373474 3:d9c2fc1d5984 25
1013373474 3:d9c2fc1d5984 26 void init();
1013373474 3:d9c2fc1d5984 27 void welcome();
1013373474 3:d9c2fc1d5984 28 void render();
1013373474 3:d9c2fc1d5984 29 void render1();
1013373474 3:d9c2fc1d5984 30 void check();
1013373474 3:d9c2fc1d5984 31 void check1();
1013373474 3:d9c2fc1d5984 32 void death();
1013373474 3:d9c2fc1d5984 33
1013373474 3:d9c2fc1d5984 34
1013373474 3:d9c2fc1d5984 35
1013373474 3:d9c2fc1d5984 36 //pin
1013373474 3:d9c2fc1d5984 37 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
1013373474 3:d9c2fc1d5984 38 Gamepad pad;
1013373474 3:d9c2fc1d5984 39 bird bird;
1013373474 3:d9c2fc1d5984 40 structure structure;
1013373474 3:d9c2fc1d5984 41 structure_2 structure_2;
1013373474 3:d9c2fc1d5984 42
1013373474 3:d9c2fc1d5984 43
1013373474 3:d9c2fc1d5984 44
1013373474 3:d9c2fc1d5984 45 int main()
1013373474 3:d9c2fc1d5984 46 {
1013373474 3:d9c2fc1d5984 47 while (1){
1013373474 3:d9c2fc1d5984 48 init();//初始化
1013373474 3:d9c2fc1d5984 49 lcd.drawRect(0,0,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 50 lcd.drawRect(0,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 51 lcd.drawRect(0,47,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 52 lcd.drawRect(83,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 53 welcome();
1013373474 3:d9c2fc1d5984 54
1013373474 3:d9c2fc1d5984 55
1013373474 3:d9c2fc1d5984 56 while (pad.check_event(Gamepad::BACK_PRESSED) == false){
1013373474 3:d9c2fc1d5984 57 lcd.clear();
1013373474 3:d9c2fc1d5984 58 lcd.printString(" A NORMAL ",1,1);
1013373474 3:d9c2fc1d5984 59 lcd.printString(" B HARD ",1,2);
1013373474 3:d9c2fc1d5984 60 lcd.printString(" Y MESSAGE ",1,3);
1013373474 3:d9c2fc1d5984 61 lcd.drawRect(0,0,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 62 lcd.drawRect(0,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 63 lcd.drawRect(0,47,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 64 lcd.drawRect(83,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 65 lcd.refresh();
1013373474 3:d9c2fc1d5984 66
1013373474 3:d9c2fc1d5984 67 wait(0.5);
1013373474 3:d9c2fc1d5984 68
1013373474 3:d9c2fc1d5984 69 if (pad.check_event(Gamepad::A_PRESSED) == true) {
1013373474 3:d9c2fc1d5984 70 while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
1013373474 3:d9c2fc1d5984 71 render();
1013373474 3:d9c2fc1d5984 72 wait(0.5);
1013373474 3:d9c2fc1d5984 73 check();
1013373474 3:d9c2fc1d5984 74 }//BACK
1013373474 3:d9c2fc1d5984 75 }//A
1013373474 3:d9c2fc1d5984 76
1013373474 3:d9c2fc1d5984 77
1013373474 3:d9c2fc1d5984 78 if (pad.check_event(Gamepad::B_PRESSED) == true) {
1013373474 3:d9c2fc1d5984 79 while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
1013373474 3:d9c2fc1d5984 80 render1();
1013373474 3:d9c2fc1d5984 81 wait(0.5);
1013373474 3:d9c2fc1d5984 82 check1();
1013373474 3:d9c2fc1d5984 83 }//BACK
1013373474 3:d9c2fc1d5984 84 }//B
1013373474 3:d9c2fc1d5984 85
1013373474 3:d9c2fc1d5984 86 if (pad.check_event(Gamepad::Y_PRESSED) == true) {
1013373474 3:d9c2fc1d5984 87 while (pad.check_event(Gamepad::BACK_PRESSED) == false) {
1013373474 3:d9c2fc1d5984 88 lcd.clear();
1013373474 3:d9c2fc1d5984 89 lcd.printString(" Programme by:",0,1);
1013373474 3:d9c2fc1d5984 90 lcd.printString(" Zhou Yongzhi",0,2);
1013373474 3:d9c2fc1d5984 91 lcd.printString(" 201090418 ",0,3);
1013373474 3:d9c2fc1d5984 92 lcd.printString(" 2016110306 ",0,4);
1013373474 3:d9c2fc1d5984 93 lcd.refresh();
1013373474 3:d9c2fc1d5984 94 }//BACK
1013373474 3:d9c2fc1d5984 95 }//C
1013373474 3:d9c2fc1d5984 96
1013373474 3:d9c2fc1d5984 97
1013373474 3:d9c2fc1d5984 98 }
1013373474 3:d9c2fc1d5984 99 }
1013373474 3:d9c2fc1d5984 100 }
1013373474 3:d9c2fc1d5984 101
1013373474 3:d9c2fc1d5984 102
1013373474 3:d9c2fc1d5984 103 void init()
1013373474 3:d9c2fc1d5984 104 {
1013373474 3:d9c2fc1d5984 105 lcd.init();
1013373474 3:d9c2fc1d5984 106 pad.init();
1013373474 3:d9c2fc1d5984 107 bird.init();
1013373474 3:d9c2fc1d5984 108 structure.init();
1013373474 3:d9c2fc1d5984 109 structure_2.init();
1013373474 3:d9c2fc1d5984 110 }
1013373474 3:d9c2fc1d5984 111
1013373474 3:d9c2fc1d5984 112
1013373474 3:d9c2fc1d5984 113 void render()//A nomal
1013373474 3:d9c2fc1d5984 114 { // clear screen, re-draw and refresh
1013373474 3:d9c2fc1d5984 115 lcd.clear();
1013373474 3:d9c2fc1d5984 116 Data1 data1 = structure.get_data1();
1013373474 3:d9c2fc1d5984 117 lcd.drawRect(0,0,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 118 lcd.drawRect(0,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 119 lcd.drawRect(0,47,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 120 lcd.drawRect(83,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 121 bird.draw(lcd);
1013373474 3:d9c2fc1d5984 122 structure.draw(lcd);
1013373474 3:d9c2fc1d5984 123 char buffer1[14];
1013373474 3:d9c2fc1d5984 124 sprintf(buffer1,"%2d",data1.t);
1013373474 3:d9c2fc1d5984 125 lcd.printString(buffer1,1,1);
1013373474 3:d9c2fc1d5984 126 lcd.drawRect(1,1,15,15,FILL_TRANSPARENT);
1013373474 3:d9c2fc1d5984 127 bird.check(pad,lcd);
1013373474 3:d9c2fc1d5984 128 lcd.refresh();
1013373474 3:d9c2fc1d5984 129 }
1013373474 3:d9c2fc1d5984 130
1013373474 3:d9c2fc1d5984 131
1013373474 3:d9c2fc1d5984 132 void render1()//A nomal
1013373474 3:d9c2fc1d5984 133 { // clear screen, re-draw and refresh
1013373474 3:d9c2fc1d5984 134 lcd.clear();
1013373474 3:d9c2fc1d5984 135 Data2 data2 = structure_2.get_data2();
1013373474 3:d9c2fc1d5984 136 lcd.drawRect(0,0,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 137 lcd.drawRect(0,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 138 lcd.drawRect(0,47,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 139 lcd.drawRect(83,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 140 bird.draw(lcd);
1013373474 3:d9c2fc1d5984 141 structure_2.draw(lcd);
1013373474 3:d9c2fc1d5984 142 char buffer1[14];
1013373474 3:d9c2fc1d5984 143 sprintf(buffer1,"%2d",data2.tt);
1013373474 3:d9c2fc1d5984 144 lcd.printString(buffer1,1,1);
1013373474 3:d9c2fc1d5984 145 lcd.drawRect(1,1,15,15,FILL_TRANSPARENT);
1013373474 3:d9c2fc1d5984 146 bird.check(pad,lcd);
1013373474 3:d9c2fc1d5984 147 lcd.refresh();
1013373474 3:d9c2fc1d5984 148 }
1013373474 3:d9c2fc1d5984 149
1013373474 3:d9c2fc1d5984 150
1013373474 3:d9c2fc1d5984 151 void welcome() {
1013373474 3:d9c2fc1d5984 152 lcd.printString(" Flappy Bird ",1,1);
1013373474 3:d9c2fc1d5984 153 lcd.printString(" Press Start ",1,4);
1013373474 3:d9c2fc1d5984 154 lcd.refresh();
1013373474 3:d9c2fc1d5984 155 // wait flashing LEDs until start button is pressed LED灯闪到按Press
1013373474 3:d9c2fc1d5984 156 while (pad.check_event(Gamepad::START_PRESSED) == false) {
1013373474 3:d9c2fc1d5984 157 pad.leds_on();
1013373474 3:d9c2fc1d5984 158 wait(0.1);
1013373474 3:d9c2fc1d5984 159 pad.leds_off();
1013373474 3:d9c2fc1d5984 160 wait(0.1);
1013373474 3:d9c2fc1d5984 161 }
1013373474 3:d9c2fc1d5984 162 }
1013373474 3:d9c2fc1d5984 163
1013373474 3:d9c2fc1d5984 164
1013373474 3:d9c2fc1d5984 165 void check(){
1013373474 3:d9c2fc1d5984 166 Data data = bird.get_data();
1013373474 3:d9c2fc1d5984 167 Data1 data1 = structure.get_data1();
1013373474 3:d9c2fc1d5984 168 if ( data.y >= 48 ){
1013373474 3:d9c2fc1d5984 169 death();
1013373474 3:d9c2fc1d5984 170 }else if(data1.m >= 25 && data1.m <= 32 ){ if(data.y <= data1.n || data.y >= data1.n + 15){
1013373474 3:d9c2fc1d5984 171 death(); }}
1013373474 3:d9c2fc1d5984 172 else if(data1.p >= 25 && data1.p <= 32){ if(data.y <= data1.q || data.y >= data1.q + 15){
1013373474 3:d9c2fc1d5984 173 death(); }}
1013373474 3:d9c2fc1d5984 174 else if(data1.e >= 25 && data1.e <= 32){ if(data.y <= data1.o || data.y >= data1.o + 15){
1013373474 3:d9c2fc1d5984 175 death(); }}
1013373474 3:d9c2fc1d5984 176
1013373474 3:d9c2fc1d5984 177 }
1013373474 3:d9c2fc1d5984 178
1013373474 3:d9c2fc1d5984 179
1013373474 3:d9c2fc1d5984 180
1013373474 3:d9c2fc1d5984 181 void check1(){
1013373474 3:d9c2fc1d5984 182 Data data = bird.get_data();
1013373474 3:d9c2fc1d5984 183 Data2 data2 = structure_2.get_data2();
1013373474 3:d9c2fc1d5984 184 if ( data.y >= 48 ){
1013373474 3:d9c2fc1d5984 185 death();
1013373474 3:d9c2fc1d5984 186 }else if(data2.mm >= 25 && data2.mm <= 32 ){ if(data.y <= data2.nn || data.y >= data2.nn + 15){
1013373474 3:d9c2fc1d5984 187 death(); }}
1013373474 3:d9c2fc1d5984 188 else if(data2.pp >= 25 && data2.pp <= 32){ if(data.y <= data2.qq || data.y >= data2.qq + 15){
1013373474 3:d9c2fc1d5984 189 death(); }}
1013373474 3:d9c2fc1d5984 190 else if(data2.ee >= 25 && data2.ee <= 32){ if(data.y <= data2.oo || data.y >= data2.oo + 15){
1013373474 3:d9c2fc1d5984 191 death(); }}
1013373474 3:d9c2fc1d5984 192
1013373474 3:d9c2fc1d5984 193 }
1013373474 3:d9c2fc1d5984 194
1013373474 3:d9c2fc1d5984 195 void death(){
1013373474 3:d9c2fc1d5984 196 int fps = 2;
1013373474 3:d9c2fc1d5984 197 init();
1013373474 3:d9c2fc1d5984 198 while (pad.check_event(Gamepad::START_PRESSED) == false) {
1013373474 3:d9c2fc1d5984 199 lcd.clear();
1013373474 3:d9c2fc1d5984 200 lcd.drawRect(0,0,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 201 lcd.drawRect(0,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 202 lcd.drawRect(0,47,84,1,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 203 lcd.drawRect(83,1,1,48,FILL_BLACK);//该公式前两个点是坐标,后两个点是涂抹区域的长宽,先长后宽,
1013373474 3:d9c2fc1d5984 204 lcd.printString(" Dead ",1,1);
1013373474 3:d9c2fc1d5984 205 lcd.printString(" Press start",1,3);
1013373474 3:d9c2fc1d5984 206 lcd.printString("to play again",1,4);
1013373474 3:d9c2fc1d5984 207 lcd.refresh();
1013373474 3:d9c2fc1d5984 208 wait(0.3);
1013373474 3:d9c2fc1d5984 209
1013373474 3:d9c2fc1d5984 210 }//while(1)
1013373474 3:d9c2fc1d5984 211
1013373474 3:d9c2fc1d5984 212 }//death
1013373474 3:d9c2fc1d5984 213
1013373474 3:d9c2fc1d5984 214 @endcode
1013373474 2:fb9c05867677 215 */
1013373474 0:3887ebee1b37 216 class bird
1013373474 0:3887ebee1b37 217 {
1013373474 0:3887ebee1b37 218
1013373474 0:3887ebee1b37 219
1013373474 0:3887ebee1b37 220 public:
1013373474 2:fb9c05867677 221 /**
1013373474 2:fb9c05867677 222 *@brief initialise the parameters
1013373474 2:fb9c05867677 223 *@param size,speed
1013373474 2:fb9c05867677 224 */
1013373474 0:3887ebee1b37 225 void init(int size,int speed);
1013373474 2:fb9c05867677 226 /**
1013373474 2:fb9c05867677 227 *@brief initialise the parameters
1013373474 2:fb9c05867677 228 *@param lcd
1013373474 2:fb9c05867677 229 *@details The function in N5110 is used
1013373474 2:fb9c05867677 230 */
1013373474 0:3887ebee1b37 231 void draw(N5110 &lcd);
1013373474 2:fb9c05867677 232 /**
1013373474 2:fb9c05867677 233 *@brief initialise the parameters
1013373474 2:fb9c05867677 234 *@param lcd,pad
1013373474 2:fb9c05867677 235 *@details The function in N5110 and Gamepad is used
1013373474 2:fb9c05867677 236 */
1013373474 1:8672e52115e3 237 void check(Gamepad &pad, N5110 &lcd);
1013373474 2:fb9c05867677 238 /**
1013373474 2:fb9c05867677 239 *@brief initialise the parameters
1013373474 2:fb9c05867677 240 */
1013373474 0:3887ebee1b37 241 void init();
1013373474 2:fb9c05867677 242 /**
1013373474 2:fb9c05867677 243 *@brief The data which are sent to main.cpp
1013373474 2:fb9c05867677 244 */
1013373474 0:3887ebee1b37 245 Data get_data();
1013373474 2:fb9c05867677 246
1013373474 0:3887ebee1b37 247
1013373474 0:3887ebee1b37 248 private:
1013373474 0:3887ebee1b37 249 int _y;
1013373474 0:3887ebee1b37 250 Data _data;
1013373474 0:3887ebee1b37 251 };