I learned to do a little bit of allegro, lacking c++, but.. working on that part now!
Created a little game, that makes you able to move, shoot, destroy and win!
Well, not really sure if anyone here is into Allegro. But I thought, hey! Let's give it a shot! +)
- Code: Select all
#include <allegro.h>
BITMAP *Character1;
BITMAP *Character2;
BITMAP *Bullet;
BITMAP *buffer;
BITMAP *movingimage1;
BITMAP *movingimage2;
BITMAP *Character3;
BITMAP *Character4;
BITMAP *player1win;
BITMAP *player2win;
BITMAP *itsadraw;
BITMAP *background;
int selectmode = 1;
int x_p1 = 0;
int y_p1 = 0;
int x_p2 = 550;
int y_p2 = 400;
int move_p1 = 2;
int move_p2 = 1;
int bullet_y_p1;
int bullet_x_p1;
int bullet_y_p2;
int bullet_x_p2;
int win = 0;
int p1_life = 4;
int p2_life = 4;
bool alive_p1 = true;
bool alive_p2 = true;
bool shot_l_p1 = false;
bool shot_r_p1 = false;
bool shot_l_p2 = false;
bool shot_r_p2 = false;
bool stillshooting_p1 = false;
bool stillshooting_p2 = false;
void checkwin(){
if ( alive_p2 == false && alive_p1 == true && p2_life > 0 ){
alive_p2 = true;
p2_life--;
}
else if ( alive_p1 == false && alive_p2 == true && p1_life > 0 ){
alive_p1 = true;
p1_life--;
}
else if ( p1_life == 0){
win = 2;
}
else if ( p2_life == 0){
win = 1;
}
}
void checkhit(){
if( bullet_x_p1 > x_p2 && bullet_x_p1 < x_p2+87 && bullet_y_p1 < y_p2+10 && bullet_y_p1 > y_p2-60 )
alive_p2 = false;
if( bullet_x_p2 > x_p1 && bullet_x_p2 < x_p1+87 && bullet_y_p2 < y_p1+10 && bullet_y_p2 > y_p1-60 )
alive_p1 = false;
}
void drawbackground(){
draw_sprite( buffer, background, 0,0);
}
void drawships(){
if (move_p1 == 1 && alive_p1 == true){
draw_sprite( buffer, Character1, x_p1, y_p1);
}
else if (move_p1 == 2 && alive_p1 == true){
draw_sprite( buffer, Character2, x_p1, y_p1);
}
if (move_p2 == 1 && alive_p2 == true){
draw_sprite( buffer, Character3, x_p2, y_p2);
}
else if (move_p2 == 2 && alive_p2 == true){
draw_sprite( buffer, Character4, x_p2, y_p2);
}
rest(10);
}
void movecharacter_p1(){
if (key[KEY_UP] && y_p1>-20){
y_p1=y_p1-2;
}
else if (key[KEY_DOWN] && y_p1<420){
y_p1=y_p1+2;
}
if (key[KEY_LEFT]&& x_p1>-10){
move_p1 = 1;
draw_sprite( screen, movingimage1, x_p1+85, y_p1+5);
x_p1=x_p1-2;
}
else if (key[KEY_RIGHT]&& x_p1<565){
move_p1 = 2;
draw_sprite( screen, movingimage2, x_p1-80, y_p1+10);
x_p1=x_p1+2;
}
}
void movecharacter_p2(){
if (key[KEY_W] && y_p2>-20){
y_p2=y_p2-2;
}
else if (key[KEY_S] && y_p2<420){
y_p2=y_p2+2;
}
if (key[KEY_A] && x_p2>-10){
move_p2 = 1;
draw_sprite( screen, movingimage1, x_p2+85, y_p2+5);
x_p2=x_p2-2;
}
else if (key[KEY_D] && x_p2<565){
move_p2 = 2;
draw_sprite( screen, movingimage2, x_p2-80, y_p2+10);
x_p2=x_p2+2;
}
}
void firebullet_p1(){
if (key[KEY_ENTER_PAD] && move_p1 == 2){
bullet_y_p1 = y_p1;
bullet_x_p1 = x_p1;
shot_l_p1 = false;
draw_sprite( buffer, Bullet, bullet_x_p1+75,bullet_y_p1+47 );
shot_r_p1 = true;
blit( buffer, screen, 0,0,0,0, 640, 480);
checkhit();
}
if (key[KEY_ENTER_PAD] && move_p1 == 1){
bullet_y_p1 = y_p1;
bullet_x_p1 = x_p1;
shot_r_p1 = false;
draw_sprite( buffer, Bullet, bullet_x_p1-75,bullet_y_p1+47 );
shot_l_p1 = true;
blit( buffer, screen, 0,0,0,0, 640, 480);
checkhit();
}
}
void firebullet_p2(){
if (key[KEY_SPACE] && move_p2 == 2){
bullet_y_p2 = y_p2;
bullet_x_p2 = x_p2;
shot_l_p2 = false;
draw_sprite( buffer, Bullet, bullet_x_p1+75,bullet_y_p1+47 );
shot_r_p2 = true;
blit( buffer, screen, 0,0,0,0, 640, 480);
checkhit();
}
if (key[KEY_SPACE] && move_p2 == 1){
bullet_y_p2 = y_p2;
bullet_x_p2 = x_p2;
shot_r_p2 = false;
draw_sprite( buffer, Bullet, bullet_x_p2-75,bullet_y_p2+47 );
shot_l_p2 = true;
blit( buffer, screen, 0,0,0,0, 640, 480);
checkhit();
}
}
void whowins(){
if( win == 0 ){
draw_sprite( buffer, itsadraw, 230,200);
}
else if( win == 1 ){
draw_sprite( buffer, player1win, 210, 200);
}
else if (win == 2 ){
draw_sprite( buffer, player2win, 210, 200);
}
}
int main(){
allegro_init();
install_keyboard();
install_mouse();
install_timer();
set_color_depth(32);
set_gfx_mode( GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
clear_to_color( screen, makecol( 0, 0, 0));
Character1 = load_bitmap( "GFX\\spaceship1.bmp", NULL);
Character2 = load_bitmap( "GFX\\spaceship2.bmp", NULL);
movingimage1 = load_bitmap( "GFX\\movingimage1.bmp", NULL);
movingimage2 = load_bitmap( "GFX\\movingimage2.bmp", NULL);
Bullet = load_bitmap ("GFX\\bullet.bmp", NULL);
Character3 = load_bitmap( "GFX\\spaceship3.bmp", NULL);
Character4 = load_bitmap( "GFX\\spaceship4.bmp", NULL);
itsadraw = load_bitmap ( "GFX\\itsadraw.bmp", NULL);
player1win = load_bitmap ( "GFX\\player1win.bmp", NULL);
player2win = load_bitmap ( "GFX\\player2win.bmp", NULL);
background = load_bitmap ( "GFX\\background.bmp", NULL);
buffer = create_bitmap ( 640, 480 );
draw_sprite( buffer, Character2, x_p1, y_p1);
while (!key[KEY_ESC] && win == 0){
checkwin();
movecharacter_p1();
movecharacter_p2();
drawbackground();
drawships();
firebullet_p1();
firebullet_p2();
if ( shot_l_p1 == true && bullet_x_p1 >= 0){
bullet_x_p1--;
draw_sprite( buffer, Bullet, bullet_x_p1-75,bullet_y_p1+47 );
checkhit();
bullet_x_p1--;
draw_sprite( buffer, Bullet, bullet_x_p1-75,bullet_y_p1+47 );
checkhit();
bullet_x_p1--;
draw_sprite( buffer, Bullet, bullet_x_p1-75,bullet_y_p1+47 );
checkhit();
bullet_x_p1--;
draw_sprite( buffer, Bullet, bullet_x_p1-75,bullet_y_p1+47 );
checkhit();
}
if ( shot_r_p1 == true && bullet_x_p1 <= 640){
bullet_x_p1++;
draw_sprite( buffer, Bullet, bullet_x_p1+75,bullet_y_p1+47 );
checkhit();
bullet_x_p1++;
draw_sprite( buffer, Bullet, bullet_x_p1+75,bullet_y_p1+47 );
checkhit();
bullet_x_p1++;
draw_sprite( buffer, Bullet, bullet_x_p1+75,bullet_y_p1+47 );
checkhit();
bullet_x_p1++;
draw_sprite( buffer, Bullet, bullet_x_p1+75,bullet_y_p1+47 );
checkhit();
}
if ( shot_l_p2 == true && bullet_x_p2 >= 0){
bullet_x_p2--;
draw_sprite( buffer, Bullet, bullet_x_p2-75,bullet_y_p2+47 );
checkhit();
bullet_x_p2--;
draw_sprite( buffer, Bullet, bullet_x_p2-75,bullet_y_p2+47 );
checkhit();
bullet_x_p2--;
draw_sprite( buffer, Bullet, bullet_x_p2-75,bullet_y_p2+47 );
checkhit();
bullet_x_p2--;
draw_sprite( buffer, Bullet, bullet_x_p2-75,bullet_y_p2+47 );
checkhit();
}
if ( shot_r_p2 == true && bullet_x_p2 <= 640) {
bullet_x_p2++;
draw_sprite( buffer, Bullet, bullet_x_p2+75,bullet_y_p2+47 );
checkhit();
bullet_x_p2++;
draw_sprite( buffer, Bullet, bullet_x_p2+75,bullet_y_p2+47 );
checkhit();
bullet_x_p2++;
draw_sprite( buffer, Bullet, bullet_x_p2+75,bullet_y_p2+47 );
checkhit();
bullet_x_p2++;
draw_sprite( buffer, Bullet, bullet_x_p2+75,bullet_y_p2+47 );
checkhit();
}
blit( buffer, screen, 0,0,0,0, 640, 480);
clear_bitmap(buffer);
}
clear_bitmap (buffer);
while (!key[KEY_ENTER]){
clear_bitmap (buffer);
drawbackground();
whowins();
blit( buffer, screen, 0,0,0,0, 640, 480);
}
destroy_bitmap( Character1 );
destroy_bitmap( Character2 );
destroy_bitmap( Character3 );
destroy_bitmap( Character4 );
destroy_bitmap ( Bullet );
destroy_bitmap ( buffer );
destroy_bitmap ( movingimage1 );
destroy_bitmap ( movingimage2 );
destroy_bitmap ( player1win );
destroy_bitmap ( player2win );
destroy_bitmap ( itsadraw );
destroy_bitmap ( background );
return 0;
}
END_OF_MAIN();
Well, of course... Because this is allegro.. It will NOT compile unless you include the allegro library, and the pictures I had been drawing myself (No, their not good looking :p lol)
So, the library is something you have to get yourself, and here is the link to the epic art!:
http://www.megaupload.com/?f=U9OD9MIV
By,
Ruud
