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 MotionSensor
Revision 13:d04a6caba40d, committed 2019-04-24
- Comitter:
- el17sm
- Date:
- Wed Apr 24 02:33:33 2019 +0000
- Parent:
- 12:a1c1991835ca
- Child:
- 14:3361879490b2
- Commit message:
- Added bullets (error)
Changed in this revision
--- a/Entity/Bullets/Bullets.cpp Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Bullets/Bullets.cpp Wed Apr 24 02:33:33 2019 +0000
@@ -1,15 +1,25 @@
-//#include "Bullets.h"
-//
-//Bullets::Bullets(float pos_x, float pos_y){
-// moving = false;
-// face = 0;
-// hp = 0;
-// hitbox.width = 2;
-// hitbox.height = 2;
-// position.x = pos_x;
-// position.y = pos_y;
-// sprite_size.width = 4;
-// sprite_size.height = 2;
-// sprite_size.offset_x = 2;
-// sprite_size.offset_y = 0;
-//}
\ No newline at end of file
+#include "Bullets.h"
+
+Bullets::Bullets(float pos_x, float pos_y, int dir){
+ moving = true;
+ face = 0;
+ hp = 1;
+ hitbox.width = 2;
+ hitbox.height = 2;
+ position.x = pos_x;
+ position.y = pos_y;
+ sprite_size.width = 2;
+ sprite_size.height = 2;
+ sprite_size.offset_x = 0;
+ sprite_size.offset_y = 0;
+ direction = dir;
+}
+
+void Bullets::move(float speed, float unused){
+ position.x += (direction < 2)*speed;
+ position.y += (direction < 2)*speed;
+}
+
+int * Bullets::get_frame(){
+ return (int *) bullets_sprite;
+}
\ No newline at end of file
--- a/Entity/Bullets/Bullets.h Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Bullets/Bullets.h Wed Apr 24 02:33:33 2019 +0000
@@ -1,19 +1,23 @@
-//#ifndef BULLETS_H
-//#define BULLETS_H
-//#include "Entity.h"
-//
-//class Bullets : public Entity {
-//
-// public:
-// // Constructor
-// Bullets(float, float);
-//
-// // Functions
-// virtual void move(float x, float y, int);
-// virtual void chkdmg();
-//
-// private:
-// const float bullet_velocity = 0.5;
-//};
-//
-//#endif
\ No newline at end of file
+#ifndef BULLETS_H
+#define BULLETS_H
+#include "Entity.h"
+
+class Bullets : public Entity {
+
+ public:
+ // Constructor
+ Bullets(float, float, int);
+
+ // Functions
+ virtual void move(float, float);
+ virtual int * get_frame();
+
+ private:
+ // Member Variable
+ int direction;
+};
+
+const int bullets_sprite[2][2] = {{1,1},
+ {1,1}};
+
+#endif
\ No newline at end of file
--- a/Entity/Entity.cpp Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Entity.cpp Wed Apr 24 02:33:33 2019 +0000
@@ -1,8 +1,35 @@
#include "Entity.h"
-// constructors
+// functions
+void Entity::undo_move_x(bool status_x){
+ if (status_x){
+ position.x = prev_pos.x;
+ }
+}
+void Entity::undo_move_y(bool status_y){
+ if (status_y){
+ position.y = prev_pos.y;
+ }
+}
+void Entity::update_prev_pos(){prev_pos = position;};
-void Entity::update_prev_pos(){prev_pos = position;};
+bool Entity::matrix_collision_test(float pos_x, float pos_y, int map_no){
+ for (int j = pos_y; j < (int)pos_y + hitbox.height; j++){
+ for(int i = pos_x; i < (int)pos_x + hitbox.width; i++){
+ if ((j>=48) || (i>=84) || (j<0) || (i<0)) {}
+ else if ((level_map[0][j][i] == 1)) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+
+void Entity::take_damage(int damage){
+ hp -= damage;
+}
+
+// mutators
// accessors
bool Entity::get_moving(){return moving;}
--- a/Entity/Entity.h Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Entity.h Wed Apr 24 02:33:33 2019 +0000
@@ -39,38 +39,16 @@
int face;
public:
- // Functions
- bool matrix_collision_test(float pos_x, float pos_y, int map_no){
- for (int j = pos_y; j < (int)pos_y + hitbox.height; j++){
- for(int i = pos_x; i < (int)pos_x + hitbox.width; i++){
- if ((j>=48) || (i>=84) || (j<0) || (i<0)) {}
- else if ((level_map[0][j][i] == 1)) {
- return true;
- }
- }
- }
- return false;
- }
-
+ // Function
virtual void move(float, float) = 0; // movement control and miscellaneous updates
-
virtual int * get_frame() = 0;
-
- virtual void chkdmg() = 0;
+ bool matrix_collision_test(float, float, int);
+ void undo_move_x(bool);
+ void undo_move_y(bool);
+ void update_prev_pos();
+ void take_damage(int);
- void undo_move_x(bool status_x){
- if (status_x){
- position.x = prev_pos.x;
- }
- }
-
- void undo_move_y(bool status_y){
- if (status_y){
- position.y = prev_pos.y;
- }
- }
-
- void update_prev_pos();
+ // Mutator
// Accessors
bool get_moving();
--- a/Entity/Headless/Headless.cpp Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Headless/Headless.cpp Wed Apr 24 02:33:33 2019 +0000
@@ -5,7 +5,7 @@
Headless::Headless(float pos_x, float pos_y){
moving = true;
face = 0;
- hp = 0;
+ hp = 3;
hitbox.width = 6;
hitbox.height = 5;
position.x = pos_x;
@@ -54,8 +54,4 @@
int * Headless::get_frame(){
return (int *) sprite_headless[face][frame.number];
-}
-
-void Headless::chkdmg(){
-
}
\ No newline at end of file
--- a/Entity/Headless/Headless.h Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Headless/Headless.h Wed Apr 24 02:33:33 2019 +0000
@@ -11,7 +11,6 @@
// Functions
virtual void move(float, float);
virtual int * get_frame();
- virtual void chkdmg();
};
--- a/Entity/Player/Player.cpp Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Player/Player.cpp Wed Apr 24 02:33:33 2019 +0000
@@ -1,10 +1,11 @@
#include "Player.h"
#include "math.h"
+// Constructor
Player::Player(float pos_x, float pos_y){
moving = false;
face = 0;
- hp = 0;
+ hp = 1;
hitbox.width = 6;
hitbox.height = 5;
position.x = pos_x;
@@ -16,8 +17,13 @@
frame.count = 0;
frame.number = 0;
frame.max = 4;
+ for (int i = 0; i < bullets_max; i++){valid_bullets[i] = false;}
}
+// Accessors
+int get_attack(){return 1;};
+
+// Functions
void Player::move(float mapped_x, float mapped_y){
if(!matrix_collision_test(position.x + player_speed*mapped_x, position.y, 0)){
position.x += player_speed*mapped_x;
@@ -58,14 +64,9 @@
return (int *) sprite_player[face][frame.number];
}
-void Player::chkdmg(){
-
-}
-
void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X){
if (button_Y){
face = 0;
-
}
else if (button_B){
face = 1;
@@ -76,4 +77,11 @@
else if (button_X){
face = 3;
}
+ for (int i = 0; i < bullets_max; i++){
+ if (!valid_bullets[i]){
+ bullets_array[i] = new Bullets(position.x, position.y, face);
+ valid_bullets[i] = true;
+ break;
+ }
+ }
}
--- a/Entity/Player/Player.h Tue Apr 23 22:59:12 2019 +0000
+++ b/Entity/Player/Player.h Wed Apr 24 02:33:33 2019 +0000
@@ -1,6 +1,9 @@
#ifndef PLAYER_H
#define PLAYER_H
#include "Entity.h"
+#include "Bullets.h"
+
+const int bullets_max = 20;
class Player : public Entity {
public:
@@ -10,8 +13,14 @@
// Functions
virtual void move(float, float);
virtual int * get_frame();
- virtual void chkdmg();
void buttons(bool, bool, bool, bool);
+
+ // accessors
+ int get_attack();
+
+ // variables
+ Bullets *bullets_array[bullets_max];
+ bool valid_bullets[bullets_max];
};
const float player_speed = 1.2;
--- a/main.cpp Tue Apr 23 22:59:12 2019 +0000
+++ b/main.cpp Wed Apr 24 02:33:33 2019 +0000
@@ -36,16 +36,18 @@
}
// returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the y direction
-bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity){
+bool entity_move_check_y(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){
for (int i = 0; i < no_of_enemies; i++){
- if(i != current_entity){
- if (((array[i]->get_prev_pos_x() <= a->get_prev_pos_x()) && (a->get_prev_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
- ((array[i]->get_prev_pos_x() <= a->get_prev_pos_x() + a->get_hitbox_width() - 1) && (a->get_prev_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
- {
- if (((array[i]->get_prev_pos_y() <= a->get_pos_y()) && (a->get_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
- ((array[i]->get_prev_pos_y() <= a->get_pos_y() + a->get_hitbox_height() - 1) && (a->get_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+ if (valid_enemies[i]){
+ if(i != current_entity){
+ if (((array[i]->get_prev_pos_x() <= a->get_prev_pos_x()) && (a->get_prev_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
+ ((array[i]->get_prev_pos_x() <= a->get_prev_pos_x() + a->get_hitbox_width() - 1) && (a->get_prev_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
{
- return true;
+ if (((array[i]->get_prev_pos_y() <= a->get_pos_y()) && (a->get_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
+ ((array[i]->get_prev_pos_y() <= a->get_pos_y() + a->get_hitbox_height() - 1) && (a->get_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+ {
+ return true;
+ }
}
}
}
@@ -54,17 +56,19 @@
}
// returns true if the hitbox of "entity a" collides with any hitboxes of enttities within "array" as "entity a" moves on the x direction
-bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity){
+bool entity_move_check_x(Entity *a, Entity *array[], int no_of_enemies, int current_entity, bool valid_enemies[]){
for (int i = 0; i < no_of_enemies; i++){
- if(i != current_entity){
- if (((array[i]->get_prev_pos_x() <= a->get_pos_x()) && (a->get_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
- ((array[i]->get_prev_pos_x() <= a->get_pos_x() + a->get_hitbox_width() - 1) && (a->get_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
- {
- if (((array[i]->get_prev_pos_y() <= a->get_prev_pos_y()) && (a->get_prev_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
- ((array[i]->get_prev_pos_y() <= a->get_prev_pos_y() + a->get_hitbox_height() - 1) && (a->get_prev_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+ if (valid_enemies[i]){
+ if(i != current_entity){
+ if (((array[i]->get_prev_pos_x() <= a->get_pos_x()) && (a->get_pos_x() <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)) ||
+ ((array[i]->get_prev_pos_x() <= a->get_pos_x() + a->get_hitbox_width() - 1) && (a->get_pos_x() + a->get_hitbox_width() - 1 <= array[i]->get_prev_pos_x() + array[i]->get_hitbox_width() - 1)))
{
- return true;
- }
+ if (((array[i]->get_prev_pos_y() <= a->get_prev_pos_y()) && (a->get_prev_pos_y() <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)) ||
+ ((array[i]->get_prev_pos_y() <= a->get_prev_pos_y() + a->get_hitbox_height() - 1) && (a->get_prev_pos_y() + a->get_hitbox_height() - 1 <= array[i]->get_prev_pos_y() + array[i]->get_hitbox_height() - 1)))
+ {
+ return true;
+ }
+ }
}
}
}
@@ -79,21 +83,38 @@
while(1){
Player player(39, 27);
int no_of_enemies = 3;
+ bool valid_enemies[no_of_enemies];
+ for (int i = 0; i < no_of_enemies; i++){valid_enemies[i] = false;}
Entity *enemies[no_of_enemies];
- Headless enemy1(20, 20);
- Headless enemy2(20, 30);
- Headless enemy3(60, 30);
- enemies[0] = &enemy1;
- enemies[1] = &enemy2;
- enemies[2] = &enemy3;
+ enemies[0] = new Headless(20, 20);
+ valid_enemies[0] = true;
+ enemies[1] = new Headless(20, 30);
+ valid_enemies[1] = true;
+ enemies[2] = new Headless(60, 30);
+ valid_enemies[2] = true;
while(1){
int pos_x = player.get_pos_x();
int pos_y = player.get_pos_y();
// Damage action
for (int i = 0; i < no_of_enemies; i++){
- if(entity_collision(player, *enemies[i])){
- goto gameover;
+ if (valid_enemies[i]){
+ if(entity_collision(player, *enemies[i])){
+ goto gameover;
+ }
+ }
+ };
+ for (int i = 0; i < bullets_max; i++){
+ if (player.valid_bullets[i]){
+ for (int j = 0; j < no_of_enemies; j++){
+ if (valid_enemies[j]){
+ if(entity_collision(*player.bullets_array[i], *enemies[j])){
+ enemies[j]->take_damage(player.get_attack());
+ player.valid_bullets[i] = false;
+ player.bullets_array[i]->~Bullets;
+ }
+ }
+ };
}
};
@@ -104,12 +125,16 @@
// Enemy Movement
for (int i = 0; i < no_of_enemies; i++){
- enemies[i]->update_prev_pos();
- enemies[i]->move(pos_x, pos_y);
+ if (valid_enemies[i]){
+ enemies[i]->update_prev_pos();
+ enemies[i]->move(pos_x, pos_y);
+ }
};
for (int i = 0; i < no_of_enemies; i++){
- enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i));
- enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i));
+ if (valid_enemies[i]){
+ enemies[i]->undo_move_x(entity_move_check_x(enemies[i], enemies, no_of_enemies, i, valid_enemies));
+ enemies[i]->undo_move_y(entity_move_check_y(enemies[i], enemies, no_of_enemies, i, valid_enemies));
+ }
};
// Entity Collision Detection
@@ -142,13 +167,23 @@
player.get_sprite_width(),
player.get_frame());
for (int i = 0; i < no_of_enemies; i++){
- lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(),
- enemies[i]->get_pos_y()-enemies[i]->get_offset_y(),
- enemies[i]->get_sprite_height(),
- enemies[i]->get_sprite_width(),
- enemies[i]->get_frame());
+ if (valid_enemies[i]){
+ lcd.drawSpriteTransparent(enemies[i]->get_pos_x()-enemies[i]->get_offset_x(),
+ enemies[i]->get_pos_y()-enemies[i]->get_offset_y(),
+ enemies[i]->get_sprite_height(),
+ enemies[i]->get_sprite_width(),
+ enemies[i]->get_frame());
+ }
};
-
+ for (int i = 0; i < bullets_max; i++){
+ if (player.valid_bullets[i]){
+ lcd.drawSpriteTransparent(player.bullets_array[i]->get_pos_x()-player.bullets_array[i]->get_offset_x(),
+ player.bullets_array[i]->get_pos_y()-player.bullets_array[i]->get_offset_y(),
+ player.bullets_array[i]->get_sprite_height(),
+ player.bullets_array[i]->get_sprite_width(),
+ player.bullets_array[i]->get_frame());
+ }
+ };
lcd.refresh();
wait_ms(1000/20); // setting FPS
counter++;