deemo1

Dependencies:   mbed

Committer:
haoyan
Date:
Tue May 12 08:01:52 2020 +0000
Revision:
2:03cd3bb32511
Parent:
1:8c48fb8ca5e0
Child:
3:1db91ad3ab84
dlc1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
haoyan 1:8c48fb8ca5e0 1 #include "StarcraftEngine.h"
haoyan 1:8c48fb8ca5e0 2
haoyan 1:8c48fb8ca5e0 3 StarcraftEngine::StarcraftEngine()
haoyan 1:8c48fb8ca5e0 4 {
haoyan 1:8c48fb8ca5e0 5
haoyan 1:8c48fb8ca5e0 6 }
haoyan 1:8c48fb8ca5e0 7
haoyan 1:8c48fb8ca5e0 8 StarcraftEngine::~StarcraftEngine()
haoyan 1:8c48fb8ca5e0 9 {
haoyan 1:8c48fb8ca5e0 10
haoyan 1:8c48fb8ca5e0 11 }
haoyan 1:8c48fb8ca5e0 12
haoyan 1:8c48fb8ca5e0 13 void StarcraftEngine::init(int Battleship_height, int Battleship_width, int Laser_height, int Laser_width, int Swarm_height, int Swarm_width, int speed)
haoyan 1:8c48fb8ca5e0 14 {
haoyan 1:8c48fb8ca5e0 15 // initialise the game parameters
haoyan 1:8c48fb8ca5e0 16 _Battleship_height = Battleship_height;
haoyan 1:8c48fb8ca5e0 17 _Battleship_width = Battleship_width;
haoyan 1:8c48fb8ca5e0 18 _Laser_height = Laser_height;
haoyan 1:8c48fb8ca5e0 19 _Laser_width = Laser_width;
haoyan 1:8c48fb8ca5e0 20 _Swarm_height = Swarm_height;
haoyan 1:8c48fb8ca5e0 21 _Swarm_width = Swarm_width;
haoyan 1:8c48fb8ca5e0 22 _speed = speed;
haoyan 1:8c48fb8ca5e0 23
haoyan 1:8c48fb8ca5e0 24 // x position on screen - WIDTH is defined in N5110.h
haoyan 1:8c48fb8ca5e0 25 _Battleshipx = GAP;
haoyan 1:8c48fb8ca5e0 26
haoyan 1:8c48fb8ca5e0 27 // Initializing Battleship,Laser and Swarm
haoyan 1:8c48fb8ca5e0 28 _Battleship.init(_Battleshipx, _Battleship_height, _Battleship_width);
haoyan 1:8c48fb8ca5e0 29 _Laser.init(_Laser_height, _Laser_width, _speed);
haoyan 1:8c48fb8ca5e0 30 _Swarm.init(_Swarm_height, _Swarm_width, _speed);
haoyan 1:8c48fb8ca5e0 31
haoyan 1:8c48fb8ca5e0 32 }
haoyan 1:8c48fb8ca5e0 33
haoyan 1:8c48fb8ca5e0 34 void StarcraftEngine::read_input(Gamepad &pad)
haoyan 1:8c48fb8ca5e0 35 {
haoyan 1:8c48fb8ca5e0 36 _d = pad.get_direction();
haoyan 1:8c48fb8ca5e0 37 _mag = pad.get_mag();
haoyan 1:8c48fb8ca5e0 38 }
haoyan 1:8c48fb8ca5e0 39
haoyan 1:8c48fb8ca5e0 40 int StarcraftEngine::find_life()
haoyan 1:8c48fb8ca5e0 41 { int find_life = _Battleship.get_life();
haoyan 1:8c48fb8ca5e0 42 return find_life;
haoyan 1:8c48fb8ca5e0 43 }
haoyan 1:8c48fb8ca5e0 44
haoyan 1:8c48fb8ca5e0 45 int StarcraftEngine::find_score()
haoyan 1:8c48fb8ca5e0 46 { int find_score = _Battleship.get_score();
haoyan 1:8c48fb8ca5e0 47 return find_score;
haoyan 1:8c48fb8ca5e0 48 }
haoyan 1:8c48fb8ca5e0 49
haoyan 1:8c48fb8ca5e0 50 void StarcraftEngine::draw(N5110 &lcd)
haoyan 1:8c48fb8ca5e0 51 {
haoyan 1:8c48fb8ca5e0 52 // draw the elements in the LCD buffer
haoyan 1:8c48fb8ca5e0 53 // pitch
haoyan 1:8c48fb8ca5e0 54 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
haoyan 1:8c48fb8ca5e0 55 //score
haoyan 1:8c48fb8ca5e0 56 print_scores(lcd);
haoyan 1:8c48fb8ca5e0 57 // Battleship
haoyan 1:8c48fb8ca5e0 58 _Battleship.draw(lcd);
haoyan 1:8c48fb8ca5e0 59 // Swarm
haoyan 1:8c48fb8ca5e0 60 _Swarm.draw(lcd);
haoyan 1:8c48fb8ca5e0 61 // Laser
haoyan 1:8c48fb8ca5e0 62 _Laser.draw(lcd);
haoyan 1:8c48fb8ca5e0 63
haoyan 1:8c48fb8ca5e0 64 }
haoyan 1:8c48fb8ca5e0 65
haoyan 1:8c48fb8ca5e0 66 void StarcraftEngine::update(Gamepad &pad)
haoyan 1:8c48fb8ca5e0 67 {
haoyan 1:8c48fb8ca5e0 68 check_goal(pad);
haoyan 1:8c48fb8ca5e0 69
haoyan 1:8c48fb8ca5e0 70 // important to update battleship, laser and swarm before checking collisions so can
haoyan 1:8c48fb8ca5e0 71 // correct for it before updating the display
haoyan 1:8c48fb8ca5e0 72
haoyan 1:8c48fb8ca5e0 73 _Battleship.update(_d,_mag);
haoyan 1:8c48fb8ca5e0 74 _Swarm.update();
haoyan 1:8c48fb8ca5e0 75 _Laser.update();
haoyan 1:8c48fb8ca5e0 76
haoyan 1:8c48fb8ca5e0 77 check_Swarm_collisions(pad);
haoyan 1:8c48fb8ca5e0 78 }
haoyan 1:8c48fb8ca5e0 79
haoyan 1:8c48fb8ca5e0 80 void StarcraftEngine::check_Swarm_collisions(Gamepad &pad)
haoyan 1:8c48fb8ca5e0 81 {
haoyan 1:8c48fb8ca5e0 82 // read current swarm attributes
haoyan 1:8c48fb8ca5e0 83 Vector2D Swarm_pos = _Swarm.get_pos();
haoyan 1:8c48fb8ca5e0 84 Vector2D Swarm_velocity = _Swarm.get_velocity();
haoyan 1:8c48fb8ca5e0 85
haoyan 1:8c48fb8ca5e0 86 // check if swarm hit or over the battleship
haoyan 1:8c48fb8ca5e0 87 if (
haoyan 1:8c48fb8ca5e0 88 (Swarm_pos.y >= HEIGHT - 1)
haoyan 1:8c48fb8ca5e0 89 ) {
haoyan 1:8c48fb8ca5e0 90 _Battleship.minus_life();
haoyan 1:8c48fb8ca5e0 91 pad.tone(800.0,0.1); // Audio feedback
haoyan 1:8c48fb8ca5e0 92 }
haoyan 1:8c48fb8ca5e0 93
haoyan 1:8c48fb8ca5e0 94
haoyan 1:8c48fb8ca5e0 95 // write new attributes
haoyan 1:8c48fb8ca5e0 96 _Swarm.set_velocity(Swarm_velocity);
haoyan 1:8c48fb8ca5e0 97 _Swarm.set_pos(Swarm_pos);
haoyan 1:8c48fb8ca5e0 98 }
haoyan 1:8c48fb8ca5e0 99
haoyan 1:8c48fb8ca5e0 100 void StarcraftEngine::check_goal(Gamepad &pad)
haoyan 1:8c48fb8ca5e0 101 {
haoyan 1:8c48fb8ca5e0 102 Vector2D Swarm_pos = _Swarm.get_pos();
haoyan 1:8c48fb8ca5e0 103 Vector2D Battleship_pos = _Battleship.get_pos();
haoyan 1:8c48fb8ca5e0 104 Vector2D Laser_pos = _Laser.get_pos();
haoyan 1:8c48fb8ca5e0 105
haoyan 1:8c48fb8ca5e0 106 Vector2D Laser_velocity = _Laser.get_velocity();
haoyan 1:8c48fb8ca5e0 107 Vector2D Swarm_velocity = _Swarm.get_velocity();
haoyan 1:8c48fb8ca5e0 108
haoyan 1:8c48fb8ca5e0 109 // Player get score
haoyan 1:8c48fb8ca5e0 110 if ((Laser_pos.x >= Swarm_pos.x)&&
haoyan 1:8c48fb8ca5e0 111 (Laser_pos.x <= Swarm_pos.x + 6)&&
haoyan 1:8c48fb8ca5e0 112 (Laser_pos.y <= Swarm_pos.y + 6)&&
haoyan 1:8c48fb8ca5e0 113 (Laser_pos.y >= Swarm_pos.y))
haoyan 1:8c48fb8ca5e0 114 {
haoyan 1:8c48fb8ca5e0 115 _Battleship.add_score();
haoyan 1:8c48fb8ca5e0 116 Laser_pos.x = Battleship_pos.x + 3;
haoyan 1:8c48fb8ca5e0 117 Laser_pos.y = Battleship_pos.y - 1;
haoyan 1:8c48fb8ca5e0 118 Swarm_pos.x = rand() % 64;
haoyan 1:8c48fb8ca5e0 119 Swarm_pos.y = 2;
haoyan 1:8c48fb8ca5e0 120
haoyan 1:8c48fb8ca5e0 121 pad.tone(800.0,0.25);
haoyan 1:8c48fb8ca5e0 122 wait(0.1);
haoyan 1:8c48fb8ca5e0 123 }
haoyan 1:8c48fb8ca5e0 124
haoyan 2:03cd3bb32511 125 if ((Laser_pos.y < 6)||(Battleship_pos.y - Laser_pos.y >= 20))
haoyan 1:8c48fb8ca5e0 126 {
haoyan 1:8c48fb8ca5e0 127 Laser_pos.x = Battleship_pos.x + 3;
haoyan 1:8c48fb8ca5e0 128 Laser_pos.y = Battleship_pos.y - 1;
haoyan 2:03cd3bb32511 129 }
haoyan 2:03cd3bb32511 130
haoyan 1:8c48fb8ca5e0 131 _Laser.set_velocity(Laser_velocity);
haoyan 1:8c48fb8ca5e0 132 _Swarm.set_velocity(Swarm_velocity);
haoyan 1:8c48fb8ca5e0 133 _Laser.set_pos(Laser_pos);
haoyan 1:8c48fb8ca5e0 134 _Swarm.set_pos(Swarm_pos);
haoyan 1:8c48fb8ca5e0 135 }
haoyan 1:8c48fb8ca5e0 136
haoyan 1:8c48fb8ca5e0 137 void StarcraftEngine::print_scores(N5110 &lcd)
haoyan 1:8c48fb8ca5e0 138 {
haoyan 1:8c48fb8ca5e0 139 // get scores from battleship
haoyan 1:8c48fb8ca5e0 140 int Battleship_score = _Battleship.get_score();
haoyan 1:8c48fb8ca5e0 141 int Battleship_life = _Battleship.get_life();
haoyan 1:8c48fb8ca5e0 142
haoyan 1:8c48fb8ca5e0 143 // print to LCD
haoyan 1:8c48fb8ca5e0 144 char buffer[14];
haoyan 1:8c48fb8ca5e0 145 sprintf(buffer,"%2d",Battleship_score);
haoyan 1:8c48fb8ca5e0 146 lcd.printString(buffer,WIDTH/2 - 20,1); // font is 8 wide, so leave 4 pixel gape from middle assuming two digits
haoyan 1:8c48fb8ca5e0 147 }
haoyan 1:8c48fb8ca5e0 148
haoyan 1:8c48fb8ca5e0 149
haoyan 1:8c48fb8ca5e0 150
haoyan 1:8c48fb8ca5e0 151
haoyan 1:8c48fb8ca5e0 152