Ship firing and movement boundaries functional

main
BraydonKains 6 years ago
parent 7002d85693
commit a016d98fbd

@ -14,7 +14,7 @@ void Bullet::reset_pos(float x, float y) {
void Bullet::draw() { void Bullet::draw() {
if (!oob) { 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));
} }
} }

@ -1,5 +1,8 @@
#pragma once #pragma once
#define SCREEN_W 640
#define SCREEN_H 480
enum State { enum State {
Start, Start,
Gameplay, Gameplay,

@ -7,8 +7,8 @@ public:
float x_pos; float x_pos;
float y_pos; float y_pos;
float speed; float speed;
int height; float height;
int width; float width;
bool oob; bool oob;
virtual void reset_pos(float x, float y) = 0; virtual void reset_pos(float x, float y) = 0;

@ -58,6 +58,8 @@ void GameScreen::run(ALLEGRO_FONT* font) {
redraw(font); redraw(font);
al_flip_display(); al_flip_display();
int fired_counter = 0;
bool keys[ALLEGRO_KEY_MAX]; bool keys[ALLEGRO_KEY_MAX];
for (int i = 0; i < ALLEGRO_KEY_MAX; i++) keys[i] = false; for (int i = 0; i < ALLEGRO_KEY_MAX; i++) keys[i] = false;
@ -100,7 +102,19 @@ void GameScreen::run(ALLEGRO_FONT* font) {
//Firing //Firing
if (keys[KEYSPACE]) { if (keys[KEYSPACE]) {
if (bullets.size() < max_bullets) { 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) { if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) { switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_S:
case ALLEGRO_KEY_DOWN: case ALLEGRO_KEY_DOWN:
keys[KEYDOWN] = true; keys[KEYDOWN] = true;
break; break;
case ALLEGRO_KEY_W:
case ALLEGRO_KEY_UP: case ALLEGRO_KEY_UP:
keys[KEYUP] = true; keys[KEYUP] = true;
break; break;
case ALLEGRO_KEY_D:
case ALLEGRO_KEY_RIGHT: case ALLEGRO_KEY_RIGHT:
keys[KEYRIGHT] = true; keys[KEYRIGHT] = true;
break; break;
case ALLEGRO_KEY_A:
case ALLEGRO_KEY_LEFT: case ALLEGRO_KEY_LEFT:
keys[KEYLEFT] = true; keys[KEYLEFT] = true;
break; break;
@ -143,15 +161,19 @@ void GameScreen::run(ALLEGRO_FONT* font) {
} }
else if (ev.type == ALLEGRO_EVENT_KEY_UP) { else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) { switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_S:
case ALLEGRO_KEY_DOWN: case ALLEGRO_KEY_DOWN:
keys[KEYDOWN] = false; keys[KEYDOWN] = false;
break; break;
case ALLEGRO_KEY_W:
case ALLEGRO_KEY_UP: case ALLEGRO_KEY_UP:
keys[KEYUP] = false; keys[KEYUP] = false;
break; break;
case ALLEGRO_KEY_D:
case ALLEGRO_KEY_RIGHT: case ALLEGRO_KEY_RIGHT:
keys[KEYRIGHT] = false; keys[KEYRIGHT] = false;
break; break;
case ALLEGRO_KEY_A:
case ALLEGRO_KEY_LEFT: case ALLEGRO_KEY_LEFT:
keys[KEYLEFT] = false; keys[KEYLEFT] = false;
break; break;
@ -192,8 +214,10 @@ void GameScreen::redraw(ALLEGRO_FONT* font) {
if (bullets.at(i).oob) { if (bullets.at(i).oob) {
unload.push_back(i); unload.push_back(i);
} }
bullets.at(i).move(U); else {
bullets.at(i).draw(); bullets.at(i).move(U);
bullets.at(i).draw();
}
} }
for (unsigned int i = 0; i < unload.size(); i++) { for (unsigned int i = 0; i < unload.size(); i++) {
int x = unload.at(i); int x = unload.at(i);

@ -7,9 +7,6 @@
#include "enums.h" #include "enums.h"
#define SCREEN_W 640
#define SCREEN_H 480
//Screen base class, implemented by every game screen //Screen base class, implemented by every game screen
class Screen { class Screen {
public: public:

@ -4,6 +4,12 @@ Ship::Ship() {
speed = 1.0; speed = 1.0;
height = 40; height = 40;
width = 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) { void Ship::reset_pos(float x, float y) {
@ -51,10 +57,28 @@ void Ship::move(Direction dir) {
y_pos += factor; y_pos += factor;
break; 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 Ship::fire() {
Bullet new_bullet; if (!fired) {
reset_pos((x_pos + width) / 2, y_pos + new_bullet.height); Bullet new_bullet;
return 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;
}
} }

@ -9,6 +9,11 @@ class Ship : public GameElement {
public: public:
ALLEGRO_BITMAP* sprite; ALLEGRO_BITMAP* sprite;
bool fired;
float h_bound;
float l_bound;
float r_bound;
Ship(); Ship();
void reset_pos(float x, float y); void reset_pos(float x, float y);

Loading…
Cancel
Save