Site hosted by Angelfire.com: Build your free website today!

 

 

 

 

 

 

 

 

Moving a Character Across the Screen

DOWNLOAD CPP FILE

#include <allegro.h>

#define RIGHT 3 //you can change these values depending on how much you want the player to move in each direction

#define LEFT 3

#define UP 3

#define DOWN 3

void setup();

void shutdown();

void redraw();

BITMAP *character;

BITMAP *buffer;

short x = 0, y = 0;

int main()

{

setup();

x = SCREEN_W / 2;

y = SCREEN_W / 2;

redraw();

while(!key[KEY_ESC]) //while Esc key isn't pressed

{

if (key[KEY_RIGHT] && (x + RIGHT < SCREEN_W))

x += RIGHT;

else if (key[KEY_LEFT] && (x - lEFT > 0))

x -= LEFT;

else if (key[KEY_UP] && (y - UP > 0))

y -= UP;

else if (key[KEY_DOWN] && (y + DOWN < SCREEN_H))

y += DOWN;

redraw();

}

shutdown();

return 0;

}

END_OF_MAIN();

void setup()

{

allegro_init(); //initializes allegro

install_keyboard(); //installs the keyboard routines

set_color_depth(32); //this is setting the color depth to a true color mode

set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0); //setting the screen to 640, 480 resolution in fullscreen

buffer = create_bitmap(SCREEN_W, SCREEN_H); //creating the buffer as the same size as the screen

character = load_bitmap("name_of_bitmap.bmp", NULL); //loading the character bitmap

}

void shutdown()

{

clear_keybuf();

destroy_bitmap(buffer);

destroy_bitmap(character);

}

void redraw()

{

clear_bitmap(buffer); //clearing the buffer

draw_sprite(buffer, character, x, y); //drawing the character bmp to the buffer

blit(buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); //blitting the buffer to the screen so you can see everything

}