test

Dependencies:   mbed FXOS8700CQ

Committer:
Neowless
Date:
Fri May 15 12:32:47 2020 +0000
Revision:
1:48b0bf0bcda8
Parent:
0:03d005063b30
Child:
2:cc9d8ec2e1f4
test;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Neowless 1:48b0bf0bcda8 1 #include "Bitmap.h"
Neowless 1:48b0bf0bcda8 2 #include "N5110.h"
Neowless 1:48b0bf0bcda8 3 #include "Gamepad.h"
Neowless 1:48b0bf0bcda8 4 #include "mbed.h"
Neowless 1:48b0bf0bcda8 5 #include "FXOS8700CQ.h"
Neowless 1:48b0bf0bcda8 6 #include "plane.h"
Neowless 1:48b0bf0bcda8 7 #include "life.h"
Neowless 1:48b0bf0bcda8 8 #include "engine.h"
Neowless 1:48b0bf0bcda8 9 #include "N5110.h"
Neowless 1:48b0bf0bcda8 10 #include "Gamepad.h"
Neowless 1:48b0bf0bcda8 11 #include<iostream>
Neowless 1:48b0bf0bcda8 12
Neowless 1:48b0bf0bcda8 13 using namespace std;
Neowless 1:48b0bf0bcda8 14 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Neowless 1:48b0bf0bcda8 15 FXOS8700CQ device(I2C_SDA,I2C_SCL);
Neowless 1:48b0bf0bcda8 16 Gamepad pad;
Neowless 1:48b0bf0bcda8 17 plane p;
Neowless 1:48b0bf0bcda8 18 life lf;
Neowless 1:48b0bf0bcda8 19 engine eg;
Neowless 1:48b0bf0bcda8 20 bool n = true;
Neowless 1:48b0bf0bcda8 21 bomb bo[20];
Neowless 1:48b0bf0bcda8 22 bullet bu[20];
Neowless 1:48b0bf0bcda8 23 int bunum = 0;
Neowless 1:48b0bf0bcda8 24 int bonum = 0;
Neowless 1:48b0bf0bcda8 25 int score = 0;
Neowless 1:48b0bf0bcda8 26 int m = 0;
Neowless 1:48b0bf0bcda8 27
Neowless 1:48b0bf0bcda8 28 void init();
Neowless 1:48b0bf0bcda8 29 void welcome();
Neowless 1:48b0bf0bcda8 30 void screen();
Neowless 1:48b0bf0bcda8 31 void update();
Neowless 1:48b0bf0bcda8 32 void gameover();
Neowless 1:48b0bf0bcda8 33 void drawBitmap(int x, int y, int width, int height, int *data);
Neowless 1:48b0bf0bcda8 34
Neowless 1:48b0bf0bcda8 35 int main()
Neowless 1:48b0bf0bcda8 36 {
Neowless 1:48b0bf0bcda8 37 while(1){
Neowless 1:48b0bf0bcda8 38 init();
Neowless 1:48b0bf0bcda8 39 welcome();
Neowless 1:48b0bf0bcda8 40 while(n){
Neowless 1:48b0bf0bcda8 41 if(m%8 ==0){
Neowless 1:48b0bf0bcda8 42 bo[bonum%20].y = eg.randombomb();
Neowless 1:48b0bf0bcda8 43 bo[bonum%20].x = 82;
Neowless 1:48b0bf0bcda8 44 bonum = bonum + 1;
Neowless 1:48b0bf0bcda8 45 }
Neowless 1:48b0bf0bcda8 46
Neowless 1:48b0bf0bcda8 47 lcd.clear();
Neowless 1:48b0bf0bcda8 48 screen();
Neowless 1:48b0bf0bcda8 49 update();
Neowless 1:48b0bf0bcda8 50 lcd.refresh();
Neowless 1:48b0bf0bcda8 51 wait(0.02);
Neowless 1:48b0bf0bcda8 52 m = m + 1;
Neowless 1:48b0bf0bcda8 53 }
Neowless 1:48b0bf0bcda8 54 wait(1);
Neowless 1:48b0bf0bcda8 55 }
Neowless 1:48b0bf0bcda8 56 }
Neowless 1:48b0bf0bcda8 57
Neowless 1:48b0bf0bcda8 58 void init(){
Neowless 1:48b0bf0bcda8 59 device.init();
Neowless 1:48b0bf0bcda8 60 lcd.init();
Neowless 1:48b0bf0bcda8 61 pad.init();
Neowless 1:48b0bf0bcda8 62 p.init();
Neowless 1:48b0bf0bcda8 63 lf.init();
Neowless 1:48b0bf0bcda8 64 eg.init();
Neowless 1:48b0bf0bcda8 65 lcd.setContrast(0.5);
Neowless 1:48b0bf0bcda8 66 n = true;
Neowless 1:48b0bf0bcda8 67 bunum = 0;
Neowless 1:48b0bf0bcda8 68 bonum = 0;
Neowless 1:48b0bf0bcda8 69 score = 0;
Neowless 1:48b0bf0bcda8 70 m = 0;
Neowless 1:48b0bf0bcda8 71 for(int i = 0; i < 20; i++){
Neowless 1:48b0bf0bcda8 72 bu[i].x = -1;
Neowless 1:48b0bf0bcda8 73 bu[i].y = -1;
Neowless 1:48b0bf0bcda8 74 bo[i].x = -1;
Neowless 1:48b0bf0bcda8 75 bo[i].y = -1;
Neowless 1:48b0bf0bcda8 76 bo[i].width = 8;
Neowless 1:48b0bf0bcda8 77 bo[i].height = 8;
Neowless 1:48b0bf0bcda8 78 int data[64]= {
Neowless 1:48b0bf0bcda8 79 0,0,0,1,1,0,0,0,
Neowless 1:48b0bf0bcda8 80 0,1,1,0,0,1,1,0,
Neowless 1:48b0bf0bcda8 81 0,1,1,1,1,1,1,0,
Neowless 1:48b0bf0bcda8 82 1,0,1,1,1,1,0,1,
Neowless 1:48b0bf0bcda8 83 1,0,1,1,1,1,0,1,
Neowless 1:48b0bf0bcda8 84 0,1,1,1,1,1,1,0,
Neowless 1:48b0bf0bcda8 85 0,1,1,0,0,1,1,0,
Neowless 1:48b0bf0bcda8 86 0,0,0,1,1,0,0,0
Neowless 1:48b0bf0bcda8 87 };
Neowless 1:48b0bf0bcda8 88 bo[i].data = data;
Neowless 1:48b0bf0bcda8 89 }
Neowless 1:48b0bf0bcda8 90 }
Neowless 1:48b0bf0bcda8 91
Neowless 1:48b0bf0bcda8 92 void welcome(){
Neowless 1:48b0bf0bcda8 93 lcd.clear();
Neowless 1:48b0bf0bcda8 94 lcd.printString("The plane war!",0,1);
Neowless 1:48b0bf0bcda8 95 lcd.printString(" Press Start ",0,4);
Neowless 1:48b0bf0bcda8 96 lcd.refresh();
Neowless 1:48b0bf0bcda8 97 while( pad.check_event(Gamepad::START_PRESSED) == false){
Neowless 1:48b0bf0bcda8 98 pad.leds_on();
Neowless 1:48b0bf0bcda8 99 wait(0.1);
Neowless 1:48b0bf0bcda8 100 pad.leds_off();
Neowless 1:48b0bf0bcda8 101 wait(0.1);
Neowless 1:48b0bf0bcda8 102 }
Neowless 1:48b0bf0bcda8 103 }
Neowless 1:48b0bf0bcda8 104
Neowless 1:48b0bf0bcda8 105 void screen(){
Neowless 1:48b0bf0bcda8 106 lcd.clear();
Neowless 1:48b0bf0bcda8 107 p.display(lcd);
Neowless 1:48b0bf0bcda8 108 lf.display(lcd);
Neowless 1:48b0bf0bcda8 109 for(int i = 0; i <20; i++){
Neowless 1:48b0bf0bcda8 110 if(bo[i].x >=0){
Neowless 1:48b0bf0bcda8 111 drawBitmap(bo[i].x, bo[i].y, bo[i].width, bo[i].height, bo[i].data);}
Neowless 1:48b0bf0bcda8 112 if(bu[i].x >=0){
Neowless 1:48b0bf0bcda8 113 lcd.drawRect(bu[i].x, bu[i].y, 2, 2, FILL_BLACK);}
Neowless 1:48b0bf0bcda8 114 }
Neowless 1:48b0bf0bcda8 115 lcd.refresh();
Neowless 1:48b0bf0bcda8 116 }
Neowless 1:48b0bf0bcda8 117
Neowless 1:48b0bf0bcda8 118 void update(){
Neowless 1:48b0bf0bcda8 119 p.update(pad, device);
Neowless 1:48b0bf0bcda8 120 for(int i = 0; i<20; i++){
Neowless 1:48b0bf0bcda8 121 for(int j = 0; j < 20; j++){
Neowless 1:48b0bf0bcda8 122 events e = eg.evet_check(p, bo[i],pad,bu[j]);
Neowless 1:48b0bf0bcda8 123 if(e == DEAD){
Neowless 1:48b0bf0bcda8 124 lf.update();
Neowless 1:48b0bf0bcda8 125 bo[i].x = -1;
Neowless 1:48b0bf0bcda8 126 pad.tone(1000,0.2);
Neowless 1:48b0bf0bcda8 127 if(lf.liferest()==0){
Neowless 1:48b0bf0bcda8 128 gameover();
Neowless 1:48b0bf0bcda8 129 i = 20;
Neowless 1:48b0bf0bcda8 130 j = 20;
Neowless 1:48b0bf0bcda8 131 pad.tone(1000,0.5);
Neowless 1:48b0bf0bcda8 132 break;
Neowless 1:48b0bf0bcda8 133 }
Neowless 1:48b0bf0bcda8 134 }else if(e == FIRE){
Neowless 1:48b0bf0bcda8 135 eg.fire(p.getxy().x+7, p.getxy().y+3, bu[bunum%20]);
Neowless 1:48b0bf0bcda8 136 bunum = bunum + 1;
Neowless 1:48b0bf0bcda8 137 }else if(e == ELMININATE){
Neowless 1:48b0bf0bcda8 138 bo[i].x = -1;
Neowless 1:48b0bf0bcda8 139 bu[j].x = -1;
Neowless 1:48b0bf0bcda8 140 score = score + 5;
Neowless 1:48b0bf0bcda8 141 cout <<score<<endl;
Neowless 1:48b0bf0bcda8 142 }
Neowless 1:48b0bf0bcda8 143 }
Neowless 1:48b0bf0bcda8 144 }
Neowless 1:48b0bf0bcda8 145 for(int i = 0; i<20; i++){
Neowless 1:48b0bf0bcda8 146 if((bo[i].x >=0) && (bo[i].x<=84)){
Neowless 1:48b0bf0bcda8 147 bo[i].x = bo[i].x - 1;
Neowless 1:48b0bf0bcda8 148 }
Neowless 1:48b0bf0bcda8 149 if(bu[i].x >=0){
Neowless 1:48b0bf0bcda8 150 bu[i].y = bu[i].y + 1;
Neowless 1:48b0bf0bcda8 151 }
Neowless 1:48b0bf0bcda8 152 }
Neowless 1:48b0bf0bcda8 153
Neowless 1:48b0bf0bcda8 154 }
Neowless 1:48b0bf0bcda8 155
Neowless 1:48b0bf0bcda8 156 void gameover(){
Neowless 1:48b0bf0bcda8 157 cout <<"game over"<< endl;
Neowless 1:48b0bf0bcda8 158 lcd.clear();
Neowless 1:48b0bf0bcda8 159 lcd.printString("GAMEOVER ",0,1);
Neowless 1:48b0bf0bcda8 160 lcd.printString("SCORE ",0,2);
Neowless 1:48b0bf0bcda8 161 char buffer1[14];
Neowless 1:48b0bf0bcda8 162 sprintf(buffer1,"%2d",score);
Neowless 1:48b0bf0bcda8 163 lcd.printString(buffer1,0,3);
Neowless 1:48b0bf0bcda8 164 lcd.printString(" Press A ",0,4);
Neowless 1:48b0bf0bcda8 165 lcd.refresh();
Neowless 1:48b0bf0bcda8 166 while ( pad.check_event(Gamepad::A_PRESSED) == false) {
Neowless 1:48b0bf0bcda8 167 pad.leds_on();
Neowless 1:48b0bf0bcda8 168 wait(0.1);
Neowless 1:48b0bf0bcda8 169 pad.leds_off();
Neowless 1:48b0bf0bcda8 170 wait(0.1);
Neowless 1:48b0bf0bcda8 171 }
Neowless 1:48b0bf0bcda8 172 n = false;
Neowless 1:48b0bf0bcda8 173 }
Neowless 1:48b0bf0bcda8 174
Neowless 1:48b0bf0bcda8 175 void drawBitmap(int x, int y, int width, int height, int *data){
Neowless 1:48b0bf0bcda8 176 Bitmap p(data, height, width);
Neowless 1:48b0bf0bcda8 177 p.render(lcd, x, y);
Neowless 1:48b0bf0bcda8 178 }
Neowless 1:48b0bf0bcda8 179