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 21:be18f33da757, committed 2019-04-25
- Comitter:
- el17sm
- Date:
- Thu Apr 25 05:27:43 2019 +0000
- Parent:
- 19:bfe410c82b45
- Commit message:
- Snake half done no clear screen error
Changed in this revision
--- a/Entity/Bullets/Bullets.cpp Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Bullets/Bullets.cpp Thu Apr 25 05:27:43 2019 +0000
@@ -1,6 +1,7 @@
#include "Bullets.h"
-Bullets::Bullets(float pos_x, float pos_y, int dir){
+Bullets::Bullets(float pos_x, float pos_y, int dir)
+{
moving = true;
face = 0;
hp = 1;
@@ -15,30 +16,30 @@
direction = dir;
}
-void Bullets::move(float speed, float unused){
- if (direction == 0){
+void Bullets::move(float speed, float unused)
+{
+ if (direction == 0) {
position.y -= speed;
- }
- else if (direction == 1){
+ } else if (direction == 1) {
position.x += speed;
- }
- else if (direction == 2){
+ } else if (direction == 2) {
position.y += speed;
- }
- else if (direction == 3){
+ } else if (direction == 3) {
position.x -= speed;
}
}
-int * Bullets::get_frame(){
+int * Bullets::get_frame()
+{
return (int *) bullets_sprite;
}
-bool Bullets::out_of_bounds_check(){
- if (matrix_collision_test(position.x, position.y, 0)){
+bool Bullets::out_of_bounds_check()
+{
+ if (matrix_collision_test(position.x, position.y, 0)) {
return true;
}
- if ((0 > position.x) || (position.x > 84) || (0 > position.y) || (position.y > 48)){
+ if ((0 > position.x) || (position.x > 84) || (0 > position.y) || (position.y > 48)) {
return true;
}
return false;
--- a/Entity/Bullets/Bullets.h Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Bullets/Bullets.h Thu Apr 25 05:27:43 2019 +0000
@@ -2,23 +2,25 @@
#define BULLETS_H
#include "Entity.h"
-class Bullets : public Entity {
-
- public:
+class Bullets : public Entity
+{
+
+public:
// Constructor
Bullets(float, float, int);
-
+
// Functions
virtual void move(float, float);
virtual int * get_frame();
bool out_of_bounds_check();
-
- private:
+
+private:
// Member Variable
int direction;
};
const int bullets_sprite[2][2] = {{1,1},
- {1,1}};
+ {1,1}
+};
#endif
\ No newline at end of file
--- a/Entity/Entity.cpp Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Entity.cpp Thu Apr 25 05:27:43 2019 +0000
@@ -1,21 +1,27 @@
#include "Entity.h"
// functions
-void Entity::undo_move_x(bool status_x){
- if (status_x){
+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){
+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++){
+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;
@@ -25,12 +31,14 @@
return false;
}
-void Entity::take_damage(int damage){
+void Entity::take_damage(int damage)
+{
hp -= damage;
}
-bool Entity::death_check(){
- if (hp <= 0){
+bool Entity::death_check()
+{
+ if (hp <= 0) {
return true;
}
return false;
@@ -39,15 +47,51 @@
// mutators
// accessors
-bool Entity::get_moving(){return moving;}
-int Entity::get_hitbox_width(){return hitbox.width;}
-int Entity::get_hitbox_height(){return hitbox.height;}
-int Entity::get_face(){return face;}
-int Entity::get_sprite_width(){return sprite_size.width;}
-int Entity::get_sprite_height(){return sprite_size.height;}
-int Entity::get_offset_x(){return sprite_size.offset_x;}
-int Entity::get_offset_y(){return sprite_size.offset_y;}
-int Entity::get_pos_x(){return position.x;}
-int Entity::get_pos_y(){return position.y;}
-int Entity::get_prev_pos_x(){return prev_pos.x;}
-int Entity::get_prev_pos_y(){return prev_pos.y;}
\ No newline at end of file
+bool Entity::get_moving()
+{
+ return moving;
+}
+int Entity::get_hitbox_width()
+{
+ return hitbox.width;
+}
+int Entity::get_hitbox_height()
+{
+ return hitbox.height;
+}
+int Entity::get_face()
+{
+ return face;
+}
+int Entity::get_sprite_width()
+{
+ return sprite_size.width;
+}
+int Entity::get_sprite_height()
+{
+ return sprite_size.height;
+}
+int Entity::get_offset_x()
+{
+ return sprite_size.offset_x;
+}
+int Entity::get_offset_y()
+{
+ return sprite_size.offset_y;
+}
+int Entity::get_pos_x()
+{
+ return position.x;
+}
+int Entity::get_pos_y()
+{
+ return position.y;
+}
+int Entity::get_prev_pos_x()
+{
+ return prev_pos.x;
+}
+int Entity::get_prev_pos_y()
+{
+ return prev_pos.y;
+}
\ No newline at end of file
--- a/Entity/Entity.h Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Entity.h Thu Apr 25 05:27:43 2019 +0000
@@ -4,67 +4,67 @@
#include "math.h"
class Entity
-{
- protected:
- bool moving;
- struct Hitbox {
- int width;
- int height;
- };
- Hitbox hitbox;
- struct SpriteSize {
- int width;
- int height;
- // Top-left corner of sprite is offset_x
- // to the right of top-left corner of hitbox
- int offset_x;
- // Top-left corner of sprite is offset_y
- // above of top-left corner of hitbox
- int offset_y;
- };
- SpriteSize sprite_size;
- struct Position {
- float x;
- float y;
- };
- Position position;
- Position prev_pos;
- struct FrameCount {
- int count;
- int number;
- int max;
- };
- FrameCount frame;
- int hp;
- int face;
- float velocity;
-
- public:
- // Function
- virtual void move(float, float) = 0; // movement control and miscellaneous updates
- virtual int * get_frame() = 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);
- bool death_check();
-
- // Mutator
-
- // Accessors
- bool get_moving();
- int get_hitbox_width();
- int get_hitbox_height();
- int get_face();
- int get_sprite_width();
- int get_sprite_height();
- int get_offset_x();
- int get_offset_y();
- int get_pos_x();
- int get_pos_y();
- int get_prev_pos_x();
- int get_prev_pos_y();
+{
+protected:
+ bool moving;
+ struct Hitbox {
+ int width;
+ int height;
+ };
+ Hitbox hitbox;
+ struct SpriteSize {
+ int width;
+ int height;
+ // Top-left corner of sprite is offset_x
+ // to the right of top-left corner of hitbox
+ int offset_x;
+ // Top-left corner of sprite is offset_y
+ // above of top-left corner of hitbox
+ int offset_y;
+ };
+ SpriteSize sprite_size;
+ struct Position {
+ float x;
+ float y;
+ };
+ Position position;
+ Position prev_pos;
+ struct FrameCount {
+ int count;
+ int number;
+ int max;
+ };
+ FrameCount frame;
+ int hp;
+ int face;
+ float velocity;
+
+public:
+ // Function
+ virtual void move(float, float) = 0; // movement control and miscellaneous updates
+ virtual int * get_frame() = 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);
+ bool death_check();
+
+ // Mutator
+
+ // Accessors
+ bool get_moving();
+ int get_hitbox_width();
+ int get_hitbox_height();
+ int get_face(); // not used yet
+ int get_sprite_width();
+ int get_sprite_height();
+ int get_offset_x();
+ int get_offset_y();
+ int get_pos_x();
+ int get_pos_y();
+ int get_prev_pos_x();
+ int get_prev_pos_y();
};
--- a/Entity/Headless/Headless.cpp Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Headless/Headless.cpp Thu Apr 25 05:27:43 2019 +0000
@@ -2,7 +2,8 @@
#include "math.h"
#include <complex>
-Headless::Headless(float pos_x, float pos_y){
+Headless::Headless(float pos_x, float pos_y)
+{
moving = true;
face = 0;
hp = 4;
@@ -20,36 +21,34 @@
velocity = 0.5;
}
-void Headless::move(float player_x, float player_y){
+void Headless::move(float player_x, float player_y)
+{
std::complex<double> pos_diff(player_x - position.x, player_y - position.y); // defining difference in position as a vector
position.x += velocity * pos_diff.real() / std::abs(pos_diff);
position.y += velocity * pos_diff.imag() / std::abs(pos_diff);
- if (pos_diff.imag() / std::abs(pos_diff) < 0 && abs(pos_diff.imag() / std::abs(pos_diff)) > abs(pos_diff.real() / std::abs(pos_diff))){
+ if (pos_diff.imag() / std::abs(pos_diff) < 0 && abs(pos_diff.imag() / std::abs(pos_diff)) > abs(pos_diff.real() / std::abs(pos_diff))) {
face = 2;
- }
- else if (pos_diff.imag() / std::abs(pos_diff) > 0 && abs(pos_diff.imag() / std::abs(pos_diff)) > abs(pos_diff.real() / std::abs(pos_diff))){
+ } else if (pos_diff.imag() / std::abs(pos_diff) > 0 && abs(pos_diff.imag() / std::abs(pos_diff)) > abs(pos_diff.real() / std::abs(pos_diff))) {
face = 0;
- }
- else if (pos_diff.real() / std::abs(pos_diff) > 0 && abs(pos_diff.real() / std::abs(pos_diff)) > abs(pos_diff.imag() / std::abs(pos_diff))){
+ } else if (pos_diff.real() / std::abs(pos_diff) > 0 && abs(pos_diff.real() / std::abs(pos_diff)) > abs(pos_diff.imag() / std::abs(pos_diff))) {
face = 1;
- }
- else if (pos_diff.real() / std::abs(pos_diff) < 0 && abs(pos_diff.real() / std::abs(pos_diff)) > abs(pos_diff.imag() / std::abs(pos_diff))){
+ } else if (pos_diff.real() / std::abs(pos_diff) < 0 && abs(pos_diff.real() / std::abs(pos_diff)) > abs(pos_diff.imag() / std::abs(pos_diff))) {
face = 3;
}
-
+
undo_move_x(matrix_collision_test(position.x, prev_pos.y, 0));
undo_move_y(matrix_collision_test(prev_pos.x, position.y, 0));
-
- if (frame.number < frame.max){
+
+ if (frame.number < frame.max) {
frame.count++;
- }
- else {
+ } else {
frame.count = 0;
}
frame.number = (frame.count/5) % frame.max;
}
-int * Headless::get_frame(){
+int * Headless::get_frame()
+{
return (int *) sprite_headless[face][frame.number];
}
\ No newline at end of file
--- a/Entity/Headless/Headless.h Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Headless/Headless.h Thu Apr 25 05:27:43 2019 +0000
@@ -2,20 +2,22 @@
#define HEADLESS_H
#include "Entity.h"
-class Headless : public Entity {
-
- public:
+class Headless : public Entity
+{
+
+public:
// Constructor
Headless(float, float);
-
+
// Functions
virtual void move(float, float);
virtual int * get_frame();
-
+
};
const int sprite_headless[4][4][9][6] = { // Player [Face][SpriteAnimationFrame][Size_Y][Size_X]
- { // Up
+ {
+ // Up
{
{0,0,0,0,0,0,},
{1,0,0,0,0,0,},
@@ -61,7 +63,8 @@
{0,1,0,0,0,0,}
}
},
- { // Right
+ {
+ // Right
{
{0,0,0,0,0,0,},
{0,0,1,0,0,0,},
@@ -107,7 +110,8 @@
{0,1,0,0,0,1,}
}
},
- { // Down
+ {
+ // Down
{
{0,0,0,0,0,0,},
{1,0,0,0,0,0,},
@@ -153,7 +157,8 @@
{0,1,0,0,0,0,}
}
},
- { // Left
+ {
+ // Left
{
{0,0,0,0,0,0,},
{0,0,0,1,0,0,},
--- a/Entity/Player/Player.cpp Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Player/Player.cpp Thu Apr 25 05:27:43 2019 +0000
@@ -2,7 +2,8 @@
#include "math.h"
// Constructor
-Player::Player(float pos_x, float pos_y){
+Player::Player(float pos_x, float pos_y)
+{
moving = false;
face = 0;
hp = 3;
@@ -17,75 +18,75 @@
frame.count = 0;
frame.number = 0;
frame.max = 4;
- for (int i = 0; i < bullets_max; i++){valid_bullets[i] = false;}
+ for (int i = 0; i < bullets_max; i++) {
+ valid_bullets[i] = false;
+ }
fire_rate_counter = 0;
-
+
// Upgradable status
fire_rate_delay = 12;
velocity = 1.4;
}
// Accessors
-int Player::get_attack(){return 1;};
+int Player::get_attack()
+{
+ return 1;
+};
// Functions
-void Player::move(float mapped_x, float mapped_y){
- if(!matrix_collision_test(position.x + velocity*mapped_x, position.y, 0)){
+void Player::move(float mapped_x, float mapped_y)
+{
+ if(!matrix_collision_test(position.x + velocity*mapped_x, position.y, 0)) {
position.x += velocity*mapped_x;
}
- if(!matrix_collision_test(position.x, position.y - velocity*mapped_y, 0)){
+ if(!matrix_collision_test(position.x, position.y - velocity*mapped_y, 0)) {
position.y -= velocity*mapped_y;
}
moving = false;
- if (abs(mapped_x) + abs(mapped_y) > 0.1f){
+ if (abs(mapped_x) + abs(mapped_y) > 0.1f) {
moving = true;
- if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)){
+ if (mapped_y < 0 && abs(mapped_y) > abs(mapped_x)) {
face = 2;
- }
- else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)){
+ } else if (mapped_y > 0 && abs(mapped_y) > abs(mapped_x)) {
face = 0;
- }
- else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)){
+ } else if (mapped_x > 0 && abs(mapped_x) > abs(mapped_y)) {
face = 1;
- }
- else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)){
+ } else if (mapped_x < 0 && abs(mapped_x) > abs(mapped_y)) {
face = 3;
}
-
- if (frame.number < frame.max){
+
+ if (frame.number < frame.max) {
frame.count++;
- }
- else {
+ } else {
frame.count = 0;
}
- }
- else{
+ } else {
frame.count = 0;
}
frame.number = (frame.count/4) % frame.max;
}
-int * Player::get_frame(){
+int * Player::get_frame()
+{
return (int *) sprite_player[face][frame.number];
}
-void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X){
+void Player::buttons(bool button_A, bool button_B, bool button_Y, bool button_X)
+{
fire_rate_counter++;
- if (button_Y){
+ if (button_Y) {
face = 0;
- }
- else if (button_B){
+ } else if (button_B) {
face = 1;
- }
- else if (button_A){
+ } else if (button_A) {
face = 2;
- }
- else if (button_X){
+ } else if (button_X) {
face = 3;
}
- if (button_Y || button_B || button_A || button_X){
- for (int i = 0; i < bullets_max; i++){
- if (!valid_bullets[i] && fire_rate_counter > fire_rate_delay){
+ if (button_Y || button_B || button_A || button_X) {
+ for (int i = 0; i < bullets_max; i++) {
+ if (!valid_bullets[i] && fire_rate_counter > fire_rate_delay) {
bullets_array[i] = new Bullets(position.x+2, position.y-2, face);
valid_bullets[i] = true;
fire_rate_counter = 0;
--- a/Entity/Player/Player.h Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Player/Player.h Thu Apr 25 05:27:43 2019 +0000
@@ -5,19 +5,20 @@
const int bullets_max = 20;
-class Player : public Entity {
- public:
+class Player : public Entity
+{
+public:
// Constructors
Player(float, float);
-
+
// Functions
virtual void move(float, float);
virtual int * get_frame();
void buttons(bool, bool, bool, bool);
-
+
// accessors
int get_attack();
-
+
// variables
Bullets *bullets_array[bullets_max];
bool valid_bullets[bullets_max];
@@ -26,7 +27,8 @@
};
const int sprite_player [4][4][12][6] = { // Player [Face][SpriteAnimationFrame][Size_Y][Size_X]
- { // Up
+ {
+ // Up
{
{0,1,1,1,1,0,},
{1,1,1,1,1,1,},
@@ -84,7 +86,8 @@
{0,0,0,0,1,0,}
}
},
- { // Right
+ {
+ // Right
{
{0,1,1,1,1,0,},
{1,1,1,1,1,1,},
@@ -201,7 +204,8 @@
{0,0,0,0,1,0,}
}
},
- { // Left
+ {
+ // Left
{
{0,1,1,1,1,0,},
{1,1,1,1,1,1,},
--- a/Entity/Snake/Snake.cpp Thu Apr 25 03:56:09 2019 +0000
+++ b/Entity/Snake/Snake.cpp Thu Apr 25 05:27:43 2019 +0000
@@ -2,7 +2,8 @@
#include "math.h"
#include <complex>
-Snake::Snake(float pos_x, float pos_y) {
+Snake::Snake(float pos_x, float pos_y)
+{
moving = true;
face = 0;
hp = 4;
@@ -20,61 +21,53 @@
velocity = 0;
}
-void Snake::move(float player_x, float player_y) {
+void Snake::move(float player_x, float player_y)
+{
std::complex<double> pos_diff(player_x - position.x, player_y - position.y); // defining difference in position as a vector
velocity = velocity_pattern[frame.number]; // Creating slithering effect, changing velocity of movement
-
+
// Setting Face
if (frame.number == 0) {
if (abs(pos_diff.real()) > abs(pos_diff.imag())) {
if (pos_diff.real() > 0) {
face = 1;
- }
- else {
+ } else {
face = 3;
}
- }
- else {
+ } else {
if (pos_diff.imag() > 0) {
face = 0;
- }
- else {
+ } else {
face = 2;
}
}
}
-
+
// Movement, Offset and Update
- if (face == 0){
+ if (face == 0) {
position.y += velocity;
- }
- else if (face == 1){
+ } else if (face == 1) {
position.x += velocity;
- }
- else if (face == 2){
+ } else if (face == 2) {
position.y -= velocity;
- }
- else if (face == 3){
+ } else if (face == 3) {
position.x -= velocity;
}
-
+
undo_move_x(matrix_collision_test(position.x, prev_pos.y, 0));
undo_move_y(matrix_collision_test(prev_pos.x, position.y, 0));
-
- if (frame.count < 5) {
- frame.count++;
- }
- else {
+
+ frame.count++;
+ if (frame.count >= 5) {
frame.count = 0;
- if (frame.number < frame.max) {
- frame.number++;
- }
- else {
+ frame.number++;
+ if (frame.number >= frame.max) {
frame.number = 0;
}
}
}
-int * Snake::get_frame() {
+int * Snake::get_frame()
+{
return (int *) sprite_snake[face][frame.number];
}
\ No newline at end of file
--- a/Gamepad/Gamepad.cpp Thu Apr 25 03:56:09 2019 +0000
+++ b/Gamepad/Gamepad.cpp Thu Apr 25 05:27:43 2019 +0000
@@ -99,9 +99,9 @@
if (val > 1.0f) {
val = 1.0f;
}
-
+
switch (n) {
-
+
// check for valid LED number and set value
case 1:
--- a/Gamepad/Gamepad.h Thu Apr 25 03:56:09 2019 +0000
+++ b/Gamepad/Gamepad.h Thu Apr 25 05:27:43 2019 +0000
@@ -50,21 +50,21 @@
class Gamepad
{
public:
-/** Gamepad events
- * @brief List of events that can be registered on the gamepad
- */
-enum GamepadEvent {
- A_PRESSED, ///< Button A has been pressed
- B_PRESSED, ///< Button B has been pressed
- X_PRESSED, ///< Button X has been pressed
- Y_PRESSED, ///< Button Y has been pressed
- L_PRESSED, ///< Button L has been pressed
- R_PRESSED, ///< Button R has been pressed
- BACK_PRESSED, ///< Button "Back" has been pressed
- START_PRESSED, ///< Button "Start" has been pressed
- JOY_PRESSED, ///< Joystick button has been pressed
- N_EVENTS ///< A dummy flag that marks the end of the list
-};
+ /** Gamepad events
+ * @brief List of events that can be registered on the gamepad
+ */
+ enum GamepadEvent {
+ A_PRESSED, ///< Button A has been pressed
+ B_PRESSED, ///< Button B has been pressed
+ X_PRESSED, ///< Button X has been pressed
+ Y_PRESSED, ///< Button Y has been pressed
+ L_PRESSED, ///< Button L has been pressed
+ R_PRESSED, ///< Button R has been pressed
+ BACK_PRESSED, ///< Button "Back" has been pressed
+ START_PRESSED, ///< Button "Start" has been pressed
+ JOY_PRESSED, ///< Joystick button has been pressed
+ N_EVENTS ///< A dummy flag that marks the end of the list
+ };
private:
mbed::PwmOut *_led1;
@@ -153,7 +153,8 @@
* std::cout << gamepad.get_raw_event_state() << std::endl;
* @endcode
*/
- inline std::bitset<N_EVENTS> get_raw_event_state() const {
+ inline std::bitset<N_EVENTS> get_raw_event_state() const
+ {
return _event_state;
}
@@ -190,7 +191,7 @@
private:
void init_buttons();
void tone_off();
-
+
void a_reset_isr();
void b_reset_isr();
void x_reset_isr();
@@ -200,7 +201,7 @@
void back_reset_isr();
void start_reset_isr();
void joy_reset_isr();
-
+
void a_isr();
void b_isr();
void x_isr();
--- a/main.cpp Thu Apr 25 03:56:09 2019 +0000
+++ b/main.cpp Thu Apr 25 05:27:43 2019 +0000
@@ -23,30 +23,28 @@
int counter = 0;
-bool entity_collision(Entity &a, Entity &b){ // returns true if the two entity hitboxes collide
+bool entity_collision(Entity &a, Entity &b) // returns true if the two entity hitboxes collide
+{
if (((b.get_pos_x() <= a.get_pos_x()) && (a.get_pos_x() <= b.get_pos_x() + b.get_hitbox_width() - 1)) ||
- ((b.get_pos_x() <= a.get_pos_x() + a.get_hitbox_width() - 1) && (a.get_pos_x() + a.get_hitbox_width() - 1 <= b.get_pos_x() + b.get_hitbox_width() - 1)))
- {
+ ((b.get_pos_x() <= a.get_pos_x() + a.get_hitbox_width() - 1) && (a.get_pos_x() + a.get_hitbox_width() - 1 <= b.get_pos_x() + b.get_hitbox_width() - 1))) {
if (((b.get_pos_y() <= a.get_pos_y()) && (a.get_pos_y() <= b.get_pos_y() + b.get_hitbox_height() - 1)) ||
- ((b.get_pos_y() <= a.get_pos_y() + a.get_hitbox_height() - 1) && (a.get_pos_y() + a.get_hitbox_height() - 1 <= b.get_pos_y() + b.get_hitbox_height() - 1)))
- {
+ ((b.get_pos_y() <= a.get_pos_y() + a.get_hitbox_height() - 1) && (a.get_pos_y() + a.get_hitbox_height() - 1 <= b.get_pos_y() + b.get_hitbox_height() - 1))) {
return true;
- }
+ }
}
return false;
}
// 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 valid_enemies[]){
- for (int i = 0; i < no_of_enemies; i++){
- if (valid_enemies[i]){
- if(i != 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 (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)))
- {
+ ((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)))
- {
+ ((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;
}
}
@@ -57,18 +55,17 @@
}
// 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 valid_enemies[]){
- for (int i = 0; i < no_of_enemies; i++){
- if (valid_enemies[i]){
- if(i != 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 (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)))
- {
+ ((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)))
- {
+ ((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;
- }
+ }
}
}
}
@@ -81,103 +78,107 @@
lcd.init();
lcd.setContrast(0.45);
gamepad.init();
- while(1){
+ 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;}
+ for (int i = 0; i < no_of_enemies; i++) {
+ valid_enemies[i] = false;
+ }
Entity *enemies[no_of_enemies];
enemies[0] = new Snake(20, 20);
valid_enemies[0] = true;
// enemies[1] = new Snake(20, 30);
// valid_enemies[1] = true;
-// enemies[2] = new Snake(60, 30);
-// valid_enemies[2] = true;
-
- while(1){
+ enemies[2] = new Snake(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 (valid_enemies[i]){
- if(entity_collision(player, *enemies[i])){
+ for (int i = 0; i < no_of_enemies; i++) {
+ 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;
- goto multiple_damage_prevention;
+ }
+ for (int i = 0; i < bullets_max; i++) {
+ if (player.valid_bullets[i]) {
+ if (player.bullets_array[i]->out_of_bounds_check()) {
+ player.valid_bullets[i] = false;
+ player.bullets_array[i]->~Bullets;
+ } else {
+ 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;
+ break;
+ }
}
}
- if (player.bullets_array[i]->out_of_bounds_check()){
- player.valid_bullets[i] = false;
- player.bullets_array[i]->~Bullets;
- }
- };
- multiple_damage_prevention:{}
+ }
}
- };
-
+ }
+
// Player Movement
Vector2D mapped_coord = gamepad.get_mapped_coord();
player.move(mapped_coord.x, mapped_coord.y);
player.buttons(gamepad.check_event(Gamepad::A_PRESSED), gamepad.check_event(Gamepad::B_PRESSED), gamepad.check_event(Gamepad::Y_PRESSED), gamepad.check_event(Gamepad::X_PRESSED));
-
+
// Enemy Death
- for (int i = 0; i < no_of_enemies; i++){
- if(enemies[i]->death_check()){
- valid_enemies[i] = false;
- enemies[i]->~Entity;
+ for (int i = 0; i < no_of_enemies; i++) {
+ if (valid_enemies[i]) {
+ if(enemies[i]->death_check()) {
+ valid_enemies[i] = false;
+ enemies[i]->~Entity;
+ }
}
}
-
+
// Enemy Movement
- for (int i = 0; i < no_of_enemies; i++){
- if (valid_enemies[i]){
+ for (int i = 0; i < no_of_enemies; i++) {
+ if (valid_enemies[i]) {
enemies[i]->update_prev_pos();
enemies[i]->move(pos_x, pos_y);
}
- };
- for (int i = 0; i < bullets_max; i++){
- if (player.valid_bullets[i]){
+ }
+ for (int i = 0; i < bullets_max; i++) {
+ if (player.valid_bullets[i]) {
player.bullets_array[i]->move(1, 0);
}
- };
- for (int i = 0; i < no_of_enemies; i++){
- if (valid_enemies[i]){
+ }
+ for (int i = 0; i < 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
-
+
// MiniMap Screen Detection
-
+
// Pause Detection
- if(gamepad.check_event(Gamepad::START_PRESSED)){
+ if(gamepad.check_event(Gamepad::START_PRESSED)) {
lcd.clear();
lcd.printString("Paused", 0, 0);
lcd.refresh();
wait(0.05);
- while(gamepad.check_event(Gamepad::START_PRESSED)){
- };
+ while(gamepad.check_event(Gamepad::START_PRESSED)) {
+ }
wait(0.05);
- while(!gamepad.check_event(Gamepad::START_PRESSED)){
- };
+ while(!gamepad.check_event(Gamepad::START_PRESSED)) {
+ }
wait(0.05);
- while(gamepad.check_event(Gamepad::START_PRESSED)){
- };
+ while(gamepad.check_event(Gamepad::START_PRESSED)) {
+ }
}
-
-
+
+
// screen update
lcd.clear();
lcd.drawSprite(0,0,screen_height,screen_width,(int *)level_map[1]);
@@ -186,44 +187,46 @@
player.get_sprite_height(),
player.get_sprite_width(),
player.get_frame());
- for (int i = 0; i < no_of_enemies; i++){
- if (valid_enemies[i]){
+ for (int i = 0; i < no_of_enemies; i++) {
+ 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]){
+ }
+ 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++;
-
+
}
- gameover:{
+gameover: {
lcd.clear();
lcd.printString("Game Over", 0, 0);
lcd.printString("Retry?", 0, 1);
lcd.refresh();
player.~Player();
- for (int i = 0; i < no_of_enemies; i++){
- enemies[i]->~Entity();
- };
+ for (int i = 0; i < no_of_enemies; i++) {
+ if (valid_enemies[i]) {
+ enemies[i]->~Entity();
+ }
+ }
wait(0.05);
- while(!gamepad.check_event(Gamepad::A_PRESSED)){
- };
+ while(!gamepad.check_event(Gamepad::A_PRESSED)) {
+ }
wait(0.05);
- while(gamepad.check_event(Gamepad::A_PRESSED)){
- };
+ while(gamepad.check_event(Gamepad::A_PRESSED)) {
+ }
}
- };
+ }
}
\ No newline at end of file