I've been trying to make a pong game with C++ and allegro5, I found moving the ball around easy with SFML, but in allegro5 I don't know what I can do to move it around, right now, it moves but only when one of the paddles is moving, could you please help me?
Here is the code I have now:
int main(){
int d = 200;
int f = 200;
int x = 700;
int y = 100;
int a = 100;
int b = 100;
int ballx = 10;
int bally = 10;
bool ballmoving = true;
al_init_image_addon();
ALLEGRO_BITMAP *Player1;
ALLEGRO_BITMAP *Player2;
ALLEGRO_BITMAP *ball;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT ev;
ALLEGRO_EVENT_QUEUE *eventqueue = NULL;
bool playing = true;
al_init();
al_init_primitives_addon();
display = al_create_display(800, 600);
al_install_keyboard();
eventqueue = al_create_event_queue();
al_register_event_source(eventqueue, al_get_keyboard_event_source());
al_register_event_source(eventqueue, al_get_display_event_source(display));
Player1 = al_load_bitmap("rectt.png");
Player2 = al_load_bitmap("rectt.png");
ball = al_load_bitmap("ball.png");
while (playing){
ballx = ballx + 5;
bally = bally + 5;
al_clear_to_color(al_map_rgb(255, 255, 0));
al_wait_for_event(eventqueue, &ev);
if (ev.type = ALLEGRO_EVENT_KEY_DOWN){
switch (ev.keyboard.keycode){
case ALLEGRO_KEY_UP:
y -= 5;
break;
case ALLEGRO_KEY_DOWN:
y += 5;
break;
case ALLEGRO_KEY_W:
b -= 5;
break;
case ALLEGRO_KEY_S:
b += 5;
break;
}
}
al_draw_bitmap(Player1, x,y, 0);
al_draw_bitmap(Player2, a,b, 0);
al_draw_bitmap(ball, ballx, bally, 0);
al_flip_display();
}
al_flip_display();
al_rest(5);
}
EDIT:- I'm now trying to use timers, but for some reason it isn't working, the ball isn't moving at all, could you please help? This is the new code:-
#include <stdio.h>
#include <allegro5/allegro.h>
#include "allegro5/allegro.h"
#include "allegro5/allegro_image.h"
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/color.h>
#include <allegro5/allegro_color.h>
#include <allegro5/allegro5.h>
#include <allegro5/color.h>
ALLEGRO_EVENT ev;
int main(){
int d = 200;
int f = 200;
int x = 700;
int y = 100;
int a = 100;
int b = 100;
int ballx = 10;
int bally = 10;
bool ballmoving = true;
al_init_image_addon();
ALLEGRO_BITMAP *Player1;
ALLEGRO_BITMAP *Player2;
ALLEGRO_BITMAP *ball;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *eventqueue = NULL;
ALLEGRO_TIMER *timer = NULL;
bool playing = true;
al_init();
al_init_primitives_addon();
display = al_create_display(800, 600);
al_install_keyboard();
eventqueue = al_create_event_queue();
timer = al_create_timer(1.0 / 60.0);
al_register_event_source(eventqueue, al_get_keyboard_event_source());
al_register_event_source(eventqueue, al_get_display_event_source(display));
al_register_event_source(eventqueue, al_get_timer_event_source(timer));
Player1 = al_load_bitmap("rectt.png");
Player2 = al_load_bitmap("rectt.png");
ball = al_load_bitmap("ball.png");
while (playing){
al_clear_to_color(al_map_rgb(255, 255, 0));
al_wait_for_event(eventqueue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER){
ballx = ballx + 10;
bally = bally + 10;
}
if (ev.type = ALLEGRO_EVENT_KEY_DOWN){
switch (ev.keyboard.keycode){
case ALLEGRO_KEY_UP:
y -= 5;
break;
case ALLEGRO_KEY_DOWN:
y += 5;
break;
case ALLEGRO_KEY_W:
b -= 5;
break;
case ALLEGRO_KEY_S:
b += 5;
break;
}
}
al_draw_bitmap(Player1, x, y, 0);
al_draw_bitmap(Player2, a, b, 0);
al_draw_bitmap(ball, ballx, bally, 0);
al_flip_display();
}
al_flip_display();
al_rest(5);
}
EDIT: I've solved the problem by actually starting the timer. Here is the code for anybody who needs help with their code. It's definitely not perfect, but I hope it can help who needs it.
ALLEGRO_EVENT ev;
int main(){
int d = 200;
int f = 200;
int x = 700;
int y = 100;
int a = 100;
int b = 100;
int ballx = 10;
int bally = 10;
bool ballmoving = true;
al_init_image_addon();
ALLEGRO_BITMAP *Player1;
ALLEGRO_BITMAP *Player2;
ALLEGRO_BITMAP *ball;
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *eventqueue = NULL;
ALLEGRO_TIMER *timer = NULL;
bool playing = true;
al_init();
al_init_primitives_addon();
display = al_create_display(800, 600);
al_install_keyboard();
eventqueue = al_create_event_queue();
timer = al_create_timer(1.0 / 60.0);
al_register_event_source(eventqueue, al_get_keyboard_event_source());
al_register_event_source(eventqueue, al_get_display_event_source(display));
al_register_event_source(eventqueue, al_get_timer_event_source(timer));
Player1 = al_load_bitmap("rectt.png");
Player2 = al_load_bitmap("rectt.png");
ball = al_load_bitmap("ball.png");
while (playing){
al_clear_to_color(al_map_rgb(255, 255, 0));
al_wait_for_event(eventqueue, &ev);
al_start_timer(timer);
if (ev.type == ALLEGRO_EVENT_TIMER){
ballx = ballx + 10;
bally = bally + 10;
}
if (ev.type = ALLEGRO_EVENT_KEY_DOWN){
switch (ev.keyboard.keycode){
case ALLEGRO_KEY_UP:
y -= 5;
break;
case ALLEGRO_KEY_DOWN:
y += 5;
break;
case ALLEGRO_KEY_W:
b -= 5;
break;
case ALLEGRO_KEY_S:
b += 5;
break;
}
}
al_draw_bitmap(Player1, x, y, 0);
al_draw_bitmap(Player2, a, b, 0);
al_draw_bitmap(ball, ballx, bally, 0);
al_flip_display();
}
al_flip_display();
al_rest(5);
}
Thanks everyone for the help, I really appreciate it :)