Unwanted behaviour in my space invaders game

Started by
1 comment, last by Octav 11 years, 7 months ago
Hello, I tried to make my first space invaders clone, I have this code, which basically so far is supposed to handle ship movement:

[source lang="cpp"]#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include "Entity.h"

// GLOBALS ==========================================
const int width = 500;
const int height = 500;
const int imgsize = 3;
bool key[5] = {false, false, false, false, false};
bool running = true;
bool draw = true;

// FUNCTIONS ========================================
void initSpaceship(Spaceship &ship);
void moveSpaceshipRight(Spaceship &ship);
void moveSpaceshipLeft(Spaceship &ship);
void initInvader(Invader &invader);
void moveInvaderRight(Invader &invader);
void moveInvaderLeft(Invader &invader);
void initBullet(Bullet &bullet);
void fireBullet();
void doCollision();
void updateInvaders();
void drawText();

enum key_t { UP, DOWN, LEFT, RIGHT, SPACE };
enum source_t { INVADER, DEFENDER };

int main(void)
{
if(!al_init())
{ return -1; }

Spaceship ship;
Invader invader;
Bullet bullet;

al_init_image_addon();
al_install_keyboard();
al_init_font_addon();
al_init_ttf_addon();

ALLEGRO_DISPLAY *display = al_create_display(width, height);
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
ALLEGRO_TIMER *timer = al_create_timer(1.0 / 60);
ALLEGRO_BITMAP *images[imgsize];
ALLEGRO_FONT *font1 = al_load_font("arial.ttf", 20, 0);

al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));

images[0] = al_load_bitmap("defender.bmp");
images[1] = al_load_bitmap("invader.bmp");
images[2] = al_load_bitmap("explosion.bmp");

al_convert_mask_to_alpha(images[0], al_map_rgb(0, 0, 0));
al_convert_mask_to_alpha(images[1], al_map_rgb(0, 0, 0));
al_convert_mask_to_alpha(images[2], al_map_rgb(0, 0, 0));

initSpaceship(ship);
initBullet(bullet);
initInvader(invader);


al_start_timer(timer);
while(running)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if(ev.type == ALLEGRO_EVENT_TIMER)
{
draw = true;
if(key

== true)
moveSpaceshipRight(ship);

if(key

== true)
moveSpaceshipLeft(ship);
}

else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
running = false;

else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_ESCAPE:
running = false;
break;

case ALLEGRO_KEY_LEFT:
key

= true;
break;

case ALLEGRO_KEY_RIGHT:
key

= true;
break;

case ALLEGRO_KEY_SPACE:
key[SPACE] = true;
break;
}
}

else if(ev.type == ALLEGRO_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_LEFT:
key

= false;
break;

case ALLEGRO_KEY_RIGHT:
key

= false;
break;

case ALLEGRO_KEY_SPACE:
key[SPACE] = false;
break;
}
}

if(draw && al_is_event_queue_empty(event_queue))
{
draw = false;

al_draw_bitmap(images[0], ship.pos_x, ship.pos_y, 0);
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}

al_destroy_font(font1);
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
for(int i = 0; i < imgsize; i++)
al_destroy_bitmap(images);
al_destroy_display(display);
}

// FUNCTION LOGIC ======================================
void initSpaceship(Spaceship &ship)
{
ship.lives = 3;
ship.speed = 2;
ship.pos_x = width / 2;
ship.pos_y = height - 20;
}

void initInvader(Invader &invader)
{
invader.health = 100;
invader.count = 40;
invader.speed = 0.5;
invader.pos_x = 300;
invader.pos_y = 300;
}

void initBullet(Bullet &bullet)
{
bullet.speed = 10;
}

void moveSpaceshipRight(Spaceship &ship)
{
ship.pos_x += ship.speed;
if(ship.pos_x >= width)
ship.pos_x = width-30;
}

void moveSpaceshipLeft(Spaceship &ship)
{
ship.pos_x -= ship.speed;
if(ship.pos_x <= 0)
ship.pos_x = 0+30;
}[/source]

It works mostly good except the ship continues to move even if the left/right keys are up. If I press the opposite key of the direction he is moving the ship stops and no matter what I do it won't move.

This is some very very very weird behavior, when a key is down it checks for that key, it says it's down, then it's only supposed to move the ship IF the key is down, but I also specified in the code that when a key is up moving should stop.

This game is damaging my brain, why does it do that when my code is extremely specifical?

Advertisement
specifical?

ALLEGRO_EVENT_KEY_UP

specifical?

ALLEGRO_EVENT_KEY_UP

Oh how didn't I see that. Thanks!

This topic is closed to new replies.

Advertisement