201199550 Li Boyuan PlaneWar Game on K64f
Dependencies: mbed Gamepad N5110
Revision 0:36c99c50e688, committed 2020-05-14
- Comitter:
- LBY
- Date:
- Thu May 14 10:14:48 2020 +0000
- Commit message:
- From:201199550 Li Boyuan; Game name: PlaneWar
Changed in this revision
diff -r 000000000000 -r 36c99c50e688 Gamepad.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Gamepad.lib Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/eencae/code/Gamepad/#c0959710291b
diff -r 000000000000 -r 36c99c50e688 N5110.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/N5110.lib Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/eencae/code/N5110/#32f9ffeafb6d
diff -r 000000000000 -r 36c99c50e688 engine.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/engine.cpp Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,37 @@ +#include "engine.h" + + +void engine::init(){ +} + +void engine::fire(int x,int y, bullet &b){ + b.x = x; + b.y = y; +} +events engine::evet_check(plane &p, bomb &b, Gamepad &pad, bullet &bu){ + xy pxy = p.getxy(); + int width = p.getwidth(); + int height = p.getheight(); + if(b.x>-1){ + if(((pxy.x - b.width < b.x)&&(pxy.x+width>b.x)) &&((pxy.y-b.height<b.y)&&(pxy.y+height>b.y))){ + return DEAD; + } + } + if(pad.check_event(Gamepad::B_PRESSED) == true){ + return FIRE; + } + if(bu.x>-1&&b.x>-1){ + if(((bu.x - b.width < b.x)&&(bu.x+2>b.x)) &&((bu.y-b.height<b.y)&&(bu.y+2>b.y))){ + return ELMININATE; + } + } + return NONE; + } + + +int engine::randombomb(){ + int a = 5; + int b = 35; + int y = (rand() % (b-a+1))+ a; + return y; +} \ No newline at end of file
diff -r 000000000000 -r 36c99c50e688 engine.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/engine.h Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,36 @@ +#ifndef ENGINE_H +#define ENGINE_H + +#include "plane.h" +#include "life.h" +#include "Bitmap.h" +#include "N5110.h" +#include "Gamepad.h" + + +struct bomb{ + int x; + int y; + int width; + int height; + int *data; +}; +struct bullet{ + int x; + int y; +}; + +enum events{ + DEAD, + FIRE, + ELMININATE, + NONE, +}; +class engine{ + public: + void init(); + events evet_check(plane &p, bomb &b, Gamepad &pad, bullet &bu); + void fire(int x,int y, bullet &b); + int randombomb(); +}; +#endif \ No newline at end of file
diff -r 000000000000 -r 36c99c50e688 life.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/life.cpp Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,35 @@ +#include "life.h" + +void life::init(){ + _x = 0; + _y = 0; + _num = 4; + _width = 8; + _height = 7; + int d[56] = { + 0,1,1,0,0,1,1,0, + 1,1,1,1,1,1,1,1, + 1,1,0,0,1,1,1,1, + 1,1,0,1,1,1,1,1, + 0,1,1,1,1,1,1,0, + 0,0,1,1,1,1,0,0, + 0,0,0,1,1,0,0,0 + }; + for(int i = 0; i < 56; i++){ + data[i] = d[i]; + } +} +void life::update(){ + _num = _num - 1; +} +void life::display(N5110 &lcd){ + for(int i = 0; i < _num; i++){ + int x = _x + _width*i; + unsigned int width = _width; + unsigned int height = _height; + Bitmap p(data, height, width); + p.render(lcd, x, _y); + } +} + +int life::liferest(){return _num;} \ No newline at end of file
diff -r 000000000000 -r 36c99c50e688 life.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/life.h Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,23 @@ +#ifndef LIFE_H +#define LIFE_H + + +#include "Bitmap.h" +#include "N5110.h" +#include "Gamepad.h" +class life{ + public: + void init(); + void update(); + void display(N5110 &lcd); + int liferest(); + + private: + int _x; + int _y; + int _num; + int _width; + int _height; + int data[56]; +}; +#endif \ No newline at end of file
diff -r 000000000000 -r 36c99c50e688 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,174 @@ +#include "Bitmap.h" +#include "N5110.h" +#include "Gamepad.h" + +#include "plane.h" +#include "life.h" +#include "engine.h" +#include "N5110.h" +#include "Gamepad.h" +#include<iostream> + +using namespace std; +N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); +Gamepad pad; +plane p; +life lf; +engine eg; +bool n = true; +bomb bo[20]; +bullet bu[20]; +int bunum = 0; +int bonum = 0; +int score = 0; +int m = 0; + +void init(); +void welcome(); +void screen(); +void update(); +void gameover(); +void drawBitmap(int x, int y, int width, int height, int *data); + +int main() +{ + while(1){ + init(); + welcome(); + while(n){ + if(m%8 ==0){ + bo[bonum%20].y = eg.randombomb(); + bo[bonum%20].x = 82; + bonum = bonum + 1; + } + lcd.clear(); + screen(); + update(); + lcd.refresh(); + wait(0.2); + m = m + 1; + } + wait(1); + } +} + +void init(){ + lcd.init(); + pad.init(); + p.init(); + lf.init(); + eg.init(); + lcd.setContrast(0.4); + n = true; + bunum = 0; + bonum = 0; + score = 0; + m = 0; + for(int i = 0; i < 20; i++){ + bu[i].x = -1; + bu[i].y = -1; + bo[i].x = -1; + bo[i].y = -1; + bo[i].width = 8; + bo[i].height = 8; + int data[64]= { + 0,0,0,1,1,0,0,0, + 0,1,1,0,0,1,1,0, + 0,1,1,1,1,1,1,0, + 1,0,1,1,1,1,0,1, + 1,0,1,1,1,1,0,1, + 0,1,1,1,1,1,1,0, + 0,1,1,0,0,1,1,0, + 0,0,0,1,1,0,0,0 + }; + bo[i].data = data; + } +} + +void welcome(){ + lcd.clear(); + lcd.printString("The plane war!",0,1); + lcd.printString(" Press Start ",0,4); + lcd.refresh(); + while( pad.check_event(Gamepad::START_PRESSED) == false){ + pad.leds_on(); + wait(0.1); + pad.leds_off(); + wait(0.1); + } +} + +void screen(){ + lcd.clear(); + p.display(lcd); + lf.display(lcd); + for(int i = 0; i <20; i++){ + if(bo[i].x >=0){ + drawBitmap(bo[i].x, bo[i].y, bo[i].width, bo[i].height, bo[i].data);} + if(bu[i].x >=0){ + lcd.drawRect(bu[i].x, bu[i].y, 2, 2, FILL_BLACK);} + } + lcd.refresh(); +} + +void update(){ + p.update(pad); + for(int i = 0; i<20; i++){ + for(int j = 0; j < 20; j++){ + events e = eg.evet_check(p, bo[i],pad,bu[j]); + if(e == DEAD){ + lf.update(); + bo[i].x = -1; + pad.tone(1000,0.2); + if(lf.liferest()==0){ + gameover(); + i = 20; + j = 20; + pad.tone(1000,0.5); + break; + } + }else if(e == FIRE){ + eg.fire(p.getxy().x+7, p.getxy().y+3, bu[bunum%20]); + bunum = bunum + 1; + }else if(e == ELMININATE){ + bo[i].x = -1; + bu[j].x = -1; + score = score + 5; + cout <<score<<endl; + } + } + } + for(int i = 0; i<20; i++){ + if((bo[i].x >=0) && (bo[i].x<=84)){ + bo[i].x = bo[i].x - 1; + } + if((bu[i].x >=0) && (bu[i].x<=84)){ + bu[i].x = bu[i].x + 1; + } + } + +} + +void gameover(){ + cout <<"game over"<< endl; + lcd.clear(); + lcd.printString("GAMEOVER ",0,1); + lcd.printString("SCORE ",0,2); + char buffer1[14]; + sprintf(buffer1,"%2d",score); + lcd.printString(buffer1,0,3); + lcd.printString(" Press A ",0,4); + lcd.refresh(); + while ( pad.check_event(Gamepad::A_PRESSED) == false) { + pad.leds_on(); + wait(0.1); + pad.leds_off(); + wait(0.1); + } + n = false; +} + +void drawBitmap(int x, int y, int width, int height, int *data){ + Bitmap p(data, height, width); + p.render(lcd, x, y); +} \ No newline at end of file
diff -r 000000000000 -r 36c99c50e688 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/e7ca05fa8600 \ No newline at end of file
diff -r 000000000000 -r 36c99c50e688 plane.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plane.cpp Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,45 @@ +#include "plane.h" + +void plane::init(){ + _width = 7; + _height = 8; + int d[56] = { + 0,0,0,0,1,0,0, + 0,0,0,0,1,1,0, + 1,0,0,0,1,1,0, + 1,1,1,1,1,1,1, + 1,1,1,1,1,1,1, + 1,0,0,0,1,1,0, + 0,0,0,0,1,1,0, + 0,0,0,0,1,0,0 + }; + for(int i = 0; i<56;i++){ + data[i] = d[i]; + } + _xy.x = 0; + _xy.y = 25; +} + +void plane::update(Gamepad &pad){ + if(pad.get_direction() == N){ + _xy.y = _xy.y - 1; + }else if(pad.get_direction() == S){ + _xy.y = _xy.y + 1; + }else if(pad.get_direction() == W){ + _xy.x = _xy.x - 1; + }else if(pad.get_direction() == E){ + _xy.x = _xy.x + 1; + } +} + +void plane::display(N5110 &lcd){ + unsigned int width = _width; + unsigned int height = _height; + Bitmap p(data, height, width); + p.render(lcd, _xy.x, _xy.y); +} + +xy plane::getxy(){return _xy;} + +int plane::getwidth(){return _width;} +int plane::getheight(){return _height;} \ No newline at end of file
diff -r 000000000000 -r 36c99c50e688 plane.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plane.h Thu May 14 10:14:48 2020 +0000 @@ -0,0 +1,29 @@ +#ifndef PLANE_H +#define PLANE_H + +#include "Bitmap.h" +#include "N5110.h" +#include "Gamepad.h" + + +struct xy{ + int x; + int y; +}; + +class plane{ + public: + void init(); + void update(Gamepad &pad); + void display(N5110 &lcd); + xy getxy(); + int getwidth(); + int getheight(); + + private: + int _height; + int _width; + int data[56]; + xy _xy; +}; +#endif \ No newline at end of file