### Thread: Car Movement In A 2D Top Down Game - C++ Allegro

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Apr 2010
Posts
4
Rep Power
0

#### Car Movement In A 2D Top Down Game - C++ Allegro

Hello everyone. I'm hoping someone can point me in the right direction here.

I'm a newbie at programming and thought I'd have a bash at making a 2d top down car racer thingy.
As it stands, I have loaded a bitmap to the screen which accepts keyboard inputs to move it forward or backwards, just at a constant speed at the moment (no acceleration or deceleration).

I want to be able to turn the car which is where I have ground to a halt. After much Googling, it appears I should be using sine, cosine, and arctangent functions. This has totally fried my brain.

I would appreciate it if someone could explain in layman's terms how I would go about achieving this. Just the basics, once I pass this hurdle I can move on to making the movement more realistic.

As mention, I'm new to programming so I'm just using structs, functions, and a simple game loop.

Here is my code:

Main.ccp
Code:
```#include <allegro5/allegro5.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_image.h>
#include <math.h>
#include "objects.h"

//Global variables
const int SCREENWIDTH = 1024;
const int SCREENHEIGHT = 768;
enum KEYS{UP, DOWN, LEFT, RIGHT};

//Prototypes
void InitPlayer1(Car &player1, ALLEGRO_BITMAP *image);
void DrawPlayer1(Car &player1);
void MovePlayer1Forward(Car &player1);
void MovePlayer1Backwards(Car &player1);

int main(void)
{
//Variables
bool done = false;
bool isGameOver = false;
bool redraw = true;
bool keys[4] = {false, false, false, false};
int FPS = 60;
float friction = 0.7;

//Object variables
Car player1;

//Allegro variables
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_FONT *font18 = NULL;
ALLEGRO_BITMAP *carImage = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;

//Initialise Allegro
if(!al_init())
{
al_show_native_message_box(NULL, NULL, NULL,
"Failed to initialise Allegro", NULL, NULL);
}

al_install_keyboard();

display = al_create_display(SCREENWIDTH, SCREENHEIGHT);
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);

if(!display)
{
al_show_native_message_box(NULL, NULL, NULL,
"Failed to initialise display", NULL, NULL);
}

//Call Init Fuctions
InitPlayer1(player1, carImage);

// Register event sources
//==================================================
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));

al_start_timer(timer);

//==================================================
//				GAME LOOP
//==================================================
while(!done)
{
// EVENT QUEUE
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if (ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = true;
if(keys[UP])
{
MovePlayer1Forward(player1);
}
if(keys[DOWN])
{
MovePlayer1Backwards(player1);
}
}

if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
}
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
}
}
else if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
{
done = true;
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}

if(redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
DrawPlayer1(player1);
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
//End of game loop
//====================================================================

//================================
//  Destroy functions
//================================
al_destroy_timer(timer);
al_destroy_event_queue(event_queue);
al_destroy_bitmap(carImage);
al_destroy_font(font18);
al_destroy_display(display);

return 0;
}
//========================================
//            Functions
//========================================
void InitPlayer1(Car &player1, ALLEGRO_BITMAP *image)
{
player1.ID = PLAYER;
player1.pos_x = 10;
player1.pos_y = SCREENHEIGHT / 2;
player1.vel_x = 0;
player1.vel_y = 0;
player1.boundx = 100;
player1.boundy = 50;
player1.speed = 7;
player1.revSpeed = 3;
player1.image = image;
}
void DrawPlayer1(Car &player1)
{
al_draw_bitmap(player1.image, player1.pos_x, player1.pos_y, 0);
}
void MovePlayer1Forward(Car &player1)
{
player1.pos_x += player1.speed;
if(player1.pos_x + 100 > SCREENWIDTH)
player1.pos_x = SCREENWIDTH - 100;
}
void MovePlayer1Backwards(Car &player1)
{
player1.pos_x -= player1.revSpeed;
if(player1.pos_x < 0)
player1.pos_x = 0;
}```
Objects.h
Code:
```enum ID{PLAYER};

//Player
struct Car
{
int ID;
int pos_x;
int pos_y;
int vel_x;
int vel_y;
int speed;
int revSpeed;
int boundx;
int boundy;

ALLEGRO_BITMAP *image;
};```
2. For rotating the image look up the function al_draw_rotated_bitmap.