Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

Space shooting game (Allegro / C++)

Finished making your own program (thats not part of the practicing forums) ? Show it off here

Space shooting game (Allegro / C++)

Postby Ruddie on Wed Sep 23, 2009 11:35 am

Well, after making some games in c++, as many others. I moved on!

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
Last edited by Ruddie on Fri Sep 25, 2009 10:35 am, edited 3 times in total.
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Space shooting game (Allegro / C++)

Postby ReiKo on Wed Sep 23, 2009 5:32 pm

Well, could you get us files that are needed to run this (pictures as people who installed Allegro libraries got .dll files needed to run this program) ?

Thanks.
User avatar
ReiKo
 
Posts: 115
Joined: Sat May 09, 2009 5:11 pm

Re: Space shooting game (Allegro / C++)

Postby Ruddie on Thu Sep 24, 2009 11:21 am

ReiKo wrote:Well, could you get us files that are needed to run this (pictures as people who installed Allegro libraries got .dll files needed to run this program) ?

Thanks.



I'm going to try to get it for you, but rapidshare is being annoying today :) should be here soon
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Space shooting game (Allegro / C++)

Postby ReiKo on Thu Sep 24, 2009 1:41 pm

You can use megashare, megaupload...
User avatar
ReiKo
 
Posts: 115
Joined: Sat May 09, 2009 5:11 pm

Re: Space shooting game (Allegro / C++)

Postby Ruddie on Thu Sep 24, 2009 2:20 pm

ReiKo wrote:You can use megashare, megaupload...


their it is, specially for you! :p
Cheese tastes good!
User avatar
Ruddie
 
Posts: 36
Joined: Tue Sep 22, 2009 3:56 pm
Location: Netherlands

Re: Space shooting game (Allegro / C++)

Postby DevGeek++ on Thu Sep 24, 2009 6:08 pm

OMG,dude you could use some imagesharing site,i have to wait some time for all those pictures :S Can you post only the .exe ? Thanks :P
DevGeek++
 
Posts: 28
Joined: Tue Aug 04, 2009 5:54 am

Re: Space shooting game (Allegro / C++)

Postby sawm on Fri Sep 25, 2009 3:03 am

http://rapidshare.de/files/48414837/FUL ... M.zip.html

the full thing compiled :O


hope u dont mind if you do il give you the kill link <3
sawm
 
Posts: 8
Joined: Fri Aug 28, 2009 3:24 pm

Re: Space shooting game (Allegro / C++)

Postby glinka57 on Fri Sep 25, 2009 1:47 pm

AAAARRGH! it says it can run because alleg42.dll was not found. lol
User avatar
glinka57
 
Posts: 195
Joined: Fri Feb 27, 2009 7:32 pm

Re: Space shooting game (Allegro / C++)

Postby DevGeek++ on Fri Sep 25, 2009 1:55 pm

Look here :)
DevGeek++
 
Posts: 28
Joined: Tue Aug 04, 2009 5:54 am

Re: Space shooting game (Allegro / C++)

Postby noobgrammer on Fri Sep 25, 2009 2:03 pm

Copy the .dlls out of the allegro bin folder and paste them in the same folder that has the .exe
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 198
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando


Return to Full programs

Who is online

Users browsing this forum: No registered users and 0 guests