diff --git a/Assignment3Project/Assignment3Project/bullet.cpp b/Assignment3Project/Assignment3Project/bullet.cpp index cabbf37..d60d256 100644 --- a/Assignment3Project/Assignment3Project/bullet.cpp +++ b/Assignment3Project/Assignment3Project/bullet.cpp @@ -14,7 +14,7 @@ void Bullet::reset_pos(float x, float y) { void Bullet::draw() { if (!oob) { - al_draw_filled_rectangle(x_pos, y_pos, x_pos + width, y_pos + height, al_map_rgb(0, 0, 0)); + al_draw_filled_rectangle(x_pos, y_pos, x_pos + width, y_pos + height, al_map_rgb(255, 255, 255)); } } diff --git a/Assignment3Project/Assignment3Project/enums.h b/Assignment3Project/Assignment3Project/enums.h index 62ce4b6..7cbc7cc 100644 --- a/Assignment3Project/Assignment3Project/enums.h +++ b/Assignment3Project/Assignment3Project/enums.h @@ -1,4 +1,7 @@ #pragma once + +#define SCREEN_W 640 +#define SCREEN_H 480 enum State { Start, diff --git a/Assignment3Project/Assignment3Project/game_element.h b/Assignment3Project/Assignment3Project/game_element.h index ec8fc3b..49cead8 100644 --- a/Assignment3Project/Assignment3Project/game_element.h +++ b/Assignment3Project/Assignment3Project/game_element.h @@ -7,8 +7,8 @@ public: float x_pos; float y_pos; float speed; - int height; - int width; + float height; + float width; bool oob; virtual void reset_pos(float x, float y) = 0; diff --git a/Assignment3Project/Assignment3Project/game_screen.cpp b/Assignment3Project/Assignment3Project/game_screen.cpp index 5fb8129..ff5f64a 100644 --- a/Assignment3Project/Assignment3Project/game_screen.cpp +++ b/Assignment3Project/Assignment3Project/game_screen.cpp @@ -58,6 +58,8 @@ void GameScreen::run(ALLEGRO_FONT* font) { redraw(font); al_flip_display(); + int fired_counter = 0; + bool keys[ALLEGRO_KEY_MAX]; for (int i = 0; i < ALLEGRO_KEY_MAX; i++) keys[i] = false; @@ -100,7 +102,19 @@ void GameScreen::run(ALLEGRO_FONT* font) { //Firing if (keys[KEYSPACE]) { if (bullets.size() < max_bullets) { - bullets.push_back(ship.fire()); + if (!ship.fired) { + bullets.push_back(ship.fire()); + } + ship.fired = true; + } + } + if (ship.fired) { + if (fired_counter >= 20) { + ship.fired = false; + fired_counter = 0; + } + else { + fired_counter++; } } @@ -111,15 +125,19 @@ void GameScreen::run(ALLEGRO_FONT* font) { } if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { + case ALLEGRO_KEY_S: case ALLEGRO_KEY_DOWN: keys[KEYDOWN] = true; break; + case ALLEGRO_KEY_W: case ALLEGRO_KEY_UP: keys[KEYUP] = true; break; + case ALLEGRO_KEY_D: case ALLEGRO_KEY_RIGHT: keys[KEYRIGHT] = true; break; + case ALLEGRO_KEY_A: case ALLEGRO_KEY_LEFT: keys[KEYLEFT] = true; break; @@ -143,15 +161,19 @@ void GameScreen::run(ALLEGRO_FONT* font) { } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { + case ALLEGRO_KEY_S: case ALLEGRO_KEY_DOWN: keys[KEYDOWN] = false; break; + case ALLEGRO_KEY_W: case ALLEGRO_KEY_UP: keys[KEYUP] = false; break; + case ALLEGRO_KEY_D: case ALLEGRO_KEY_RIGHT: keys[KEYRIGHT] = false; break; + case ALLEGRO_KEY_A: case ALLEGRO_KEY_LEFT: keys[KEYLEFT] = false; break; @@ -192,8 +214,10 @@ void GameScreen::redraw(ALLEGRO_FONT* font) { if (bullets.at(i).oob) { unload.push_back(i); } - bullets.at(i).move(U); - bullets.at(i).draw(); + else { + bullets.at(i).move(U); + bullets.at(i).draw(); + } } for (unsigned int i = 0; i < unload.size(); i++) { int x = unload.at(i); diff --git a/Assignment3Project/Assignment3Project/screen.h b/Assignment3Project/Assignment3Project/screen.h index 427be60..f235019 100644 --- a/Assignment3Project/Assignment3Project/screen.h +++ b/Assignment3Project/Assignment3Project/screen.h @@ -7,9 +7,6 @@ #include "enums.h" -#define SCREEN_W 640 -#define SCREEN_H 480 - //Screen base class, implemented by every game screen class Screen { public: diff --git a/Assignment3Project/Assignment3Project/ship.cpp b/Assignment3Project/Assignment3Project/ship.cpp index 0b6b92c..f560830 100644 --- a/Assignment3Project/Assignment3Project/ship.cpp +++ b/Assignment3Project/Assignment3Project/ship.cpp @@ -4,6 +4,12 @@ Ship::Ship() { speed = 1.0; height = 40; width = 40; + + l_bound = 50; + r_bound = 590; + r_bound -= width; + h_bound = SCREEN_H; + h_bound -= height; } void Ship::reset_pos(float x, float y) { @@ -51,10 +57,28 @@ void Ship::move(Direction dir) { y_pos += factor; break; } + + if (x_pos <= l_bound) { + x_pos = l_bound; + } + else if (x_pos >= r_bound) { + x_pos = r_bound; + } + if (y_pos <= 0.0) { + y_pos = 0.0; + } + else if (y_pos >= h_bound) { + y_pos = h_bound; + } } Bullet Ship::fire() { - Bullet new_bullet; - reset_pos((x_pos + width) / 2, y_pos + new_bullet.height); - return new_bullet; + if (!fired) { + Bullet new_bullet; + float x = (x_pos) + width / 2.0; + float y = y_pos - new_bullet.height; + new_bullet.reset_pos(x, y); + fired = true; + return new_bullet; + } } \ No newline at end of file diff --git a/Assignment3Project/Assignment3Project/ship.h b/Assignment3Project/Assignment3Project/ship.h index 0001f0f..3edede7 100644 --- a/Assignment3Project/Assignment3Project/ship.h +++ b/Assignment3Project/Assignment3Project/ship.h @@ -9,6 +9,11 @@ class Ship : public GameElement { public: ALLEGRO_BITMAP* sprite; + bool fired; + float h_bound; + float l_bound; + float r_bound; + Ship(); void reset_pos(float x, float y);