Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 7:a3ccabdebe2e
- Parent:
- 6:5d57c758c31d
- Child:
- 8:d4e419dad90f
--- a/main.cpp Wed Apr 03 14:47:21 2019 +0000
+++ b/main.cpp Tue Apr 09 17:09:29 2019 +0000
@@ -12,26 +12,20 @@
#include "Gamepad.h"
#include "N5110.h"
#include "Bitmap.h"
+#include "TankL.h"
+#include "TanksEngine.h"
+#include "Projectile.h"
-N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+#ifdef WITH_TESTING
-const int tank_left[6][10] = {
- { 0,0,0,1,1,1,0,0,0,0 },
- { 0,0,1,1,1,1,1,0,0,0 },
- { 0,0,1,1,1,1,1,1,1,0 },
- { 1,1,1,1,1,1,1,1,1,1 },
- { 1,0,1,0,1,0,1,0,1,0 },
- { 0,1,0,1,0,1,0,1,0,0 },
-};
-const int proj[5][5] = {
- { 0,0,1,0,0 },
- { 0,0,1,0,0 },
- { 1,1,1,1,1 },
- { 0,0,1,0,0 },
- { 0,0,1,0,0 },
-};
-// Spawning tank and projectile at random position
-// with visual feedback to build grid and hitbox system
+#endif
+
+/////////////// objects ///////////////
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+TankL tankl;
+Projectile proj;
+TanksEngine engine;
+
void welcome()
{
lcd.clear();
@@ -46,9 +40,8 @@
int main()
{
- int tank_hb[40];
- int proj_hb[25];
lcd.init();
+ tankl.set_health(1);
// welcome(); // display welcome message
while(1) { // infinite loop
@@ -56,55 +49,29 @@
srand(time(NULL));
int t_pos_x = rand() % (84-1-10); // Tank position
int t_pos_y = rand() % (48-1-6);
+ tankl.set_position(t_pos_x, t_pos_y);
int p_pos_x = rand() % (84-1-5); // projectile position
int p_pos_y = rand() % (48-1-5);
+ proj.set_position(p_pos_x, p_pos_y);
-//Takes the tanks x and y position (of its bottom left pixel of sprite) and
-//determines the area it cover in terms of grid values.
- int i = 0;
- for (int i0 = 0; i0 < 4; i0++) {
+ tankl.generate_hitbox();
+ proj.generate_hitbox();
+ bool hit = engine.collision_pl(tankl, proj);
- for (int i1 = 1; i1 < 11; i1++) {
- tank_hb[i] = (i0 + t_pos_y) * 84 + t_pos_x + i1;
- i++;
- }
- }
-
- i = 0;
-
- for (int i0 = 0; i0 < 5; i0++) {
-
- for (int i1 = 1; i1 < 6; i1++) {
- proj_hb[i] = (i0 + p_pos_y) * 84 + p_pos_x + i1;
- i++;
- }
+ lcd.clear();
+ tankl.draw(lcd);
+ proj.draw(lcd);
+ lcd.refresh();
+ if (hit == true) {
+ wait(1.5);
+ lcd.clear();
+ tankl.lose_health();
+ lcd.printString("HIT",0,1);
+ lcd.refresh();
+ wait(0.5);
}
-
- bool hit = false;
- for (int i0 = 0; i0 < 25; i0++) {
- for (int i1 = 0; i1 < 40; i1++) {
- if (proj_hb[i0] == tank_hb[i1]) {hit = true;}
- }
- if(hit == true) {break;}
- }
-
-// bool hit = false;
- lcd.clear();
- lcd.drawSprite(t_pos_x,42 - t_pos_y,6,10,(int *)tank_left);
- lcd.drawSprite(p_pos_x,43 - p_pos_y,5,5,(int *)proj);
- lcd.refresh();
- wait(0.5);
- if (hit == true){
- lcd.clear();
- lcd.printString("HIT",0,1);
- lcd.refresh();
- wait(2.0);
- }
- }
-}
-/*
- char buffer[14];
- sprintf(buffer,"t_pos_x = %d ",t_pos_x);
- lcd.printString(buffer,0,1);
- */
\ No newline at end of file
+ else {wait_ms(50);}
+ }
+
+}
\ No newline at end of file