Search This Blog

Friday, May 6, 2011

Moving bitmaps in allegro

#include "airstrike2.h"

#include

#include

/////////////////////////////////////////////////////////////////////////

// drawtank function

// construct the plane using drawing functions

/////////////////////////////////////////////////////////////////////////

void drawplane(int num)

{

int x = planes[num].x;

int y = planes[num].y;

//draw tank body and turret

rectfill(screen, x-11, y-11, x+11, y+11, planes[num].color);

//line(screen, 150,230, 180, 230, 15);

//line(screen,x-20,y,x+20,y,8);//copy of ahead line

// line(screen, 150,250, 180, 250, 15);

//line(screen, x-20,y, x+20, y, 15);//copy of ahead line

//line(screen, 180, 230, 200, 240, 15);

//line(screen, 180, 250, 200, 240, 15);

//arc(screen, 182, 190, itofix(-65), itofix(-39), 60, 15);

//arc(screen, 132+x, 140+y, itofix(-65), itofix(-39), 60-x, 15-y);//copy of ahead line

//arc(screen, 182, 290, itofix(39), itofix(65), 60, 15);

//floodfill(screen, 130, 240, 15);

/*line(screen, 135, 232, 150, 230, 15);

line(screen, 135, 248, 150, 250, 15);

line(screen, 115, 232, 135, 232, 15);

line(screen, 115, 248, 135, 248, 15);

line(screen, 100, 234, 115, 232, 15);

line(screen, 100, 246, 115, 248, 15);

ellipsefill(screen, 100, 240, 30, 3, 9);

line(screen, 72, 286, 102, 243, 15);

line(screen, 72, 194, 102, 237, 15);

line(screen, 62, 194, 72, 194, 15);

line(screen, 62, 286, 72, 286, 15);

line(screen, 62, 194, 75, 239, 15);

line(screen, 62, 286, 75, 241, 15);

floodfill(screen, 70, 200, 15);

line(screen, 160, 230, 150, 190, 15);

line(screen, 160, 250, 150, 290, 15);

line(screen, 150, 190, 130, 190, 15);

line(screen, 150, 290, 130, 290, 15);

line(screen, 130, 190, 130, 232, 15);

line(screen, 130, 290, 130, 248, 15);

ellipsefill(screen, 150, 200, 18, 4, makecol(255, 0, 0));

ellipsefill(screen, 150, 280, 18, 4, makecol(255, 0, 0));*/



/*

line(screen, x-150,y-230, x+180, x+230, 15);

line(screen, x-150,y-250, x+180, x+250, 15);

//line(screen, 180, 230, 200, 240, 15);

//line(screen, 180, 250, 200, 240, 15);

arc(screen, x-182, y-190, itofix(-65), itofix(-39), x+60, y+15);

arc(screen, x-182, y-290, itofix(39), itofix(65), x+60, y+15);

//floodfill(screen, 130, 240, 15);

line(screen, x-135, y-232, x+150, y+230, 15);

line(screen, x-135, y-248, x+150,y+250, 15);

line(screen, x-115, y-232, x+135, y+232, 15);

line(screen, x-115, y-248, x+135, y+248, 15);

line(screen, x-100, y-234, x+115, y+232, 15);

line(screen, x-100, y-246, x+115, y+248, 15);

ellipsefill(screen, x-100, y-240, x+30, y+3, 9);

line(screen, x-72, y-286, x+102, y+243, 15);

line(screen, x-72, y-194, x+102, y+237, 15);

line(screen, x-62, y-194, x+72, y+194, 15);

line(screen, x-62, y-286, x+72, y+286, 15);

line(screen, x-62, y-194, x+75, y+239, 15);

line(screen, x-62, y-286, x+75, y+241, 15);

floodfill(screen, 70, 200, 15);

line(screen, x-160, y-230, x+150, y+190, 15);

line(screen, x-160, y-250, x+150, y+290, 15);

line(screen, x-150, y-190, x+130, y+190, 15);

line(screen, x-150, y-290, x+130, y+290, 15);

line(screen, x-130, y-190, x+130, y+232, 15);

line(screen, x-130, y-290, x+130, y+248, 15);

ellipsefill(screen, x-150, y-200, x+18, y+4, makecol(255, 0, 0));

ellipsefill(screen, x-150, y-280, x+8, y+4, makecol(255, 0, 0));*/





switch (dir)

{

case 1:

//line(screen,x-20,y,x+20,y,8);

//line(screen,x-20,y+30,x+20,y+30,15);

//arc(screen, x-20, 40+y, itofix(-65+x), itofix(-39+y),x+20,y-40);

rectfill(screen, x-11, y-11, x+11, y+11, planes[num].color);



break;

}

}

/////////////////////////////////////////////////////////////////////////

// eraseplane function

// erase the tank using rectfill

/////////////////////////////////////////////////////////////////////////

void eraseplane(int num)

{

//calculate box to encompass the tank

int left = planes[num].x - 20;

int top = planes[num].y - 20;

int right = planes[num].x + 20;

int bottom = planes[num].y + 20;

//erase the plane

rectfill(screen, left, top, right, bottom, 12);



}

/////////////////////////////////////////////////////////////////////////

// moveplane function

// move the plane in the current direction

/////////////////////////////////////////////////////////////////////////

void moveplane(int num)

{

int speed = planes[num].speed;

//update plane position based on direction

switch(dir)

{

case 1:

planes[num].x += speed;

break;

}

}



/////////////////////////////////////////////////////////////////////////

// forward function

// increase the plane’s speed

/////////////////////////////////////////////////////////////////////////

void forward(int num)

{

planes[num].speed=planes[num].speed+4;

if (planes[num].speed > MAXSPEED)

planes[num].speed = MAXSPEED;

}

/////////////////////////////////////////////////////////////////////////

// backward function

// decrease the plane’s speed

/////////////////////////////////////////////////////////////////////////

void backward(int num)

{

planes[num].speed=planes[num].speed-4;

if (planes[num].speed < -MAXSPEED) planes[num].speed = -MAXSPEED; } ///////////////////////////////////////////////////////////////////////// // turnleft function // rotate the plane counter-clockwise ///////////////////////////////////////////////////////////////////////// void up(int num) { planes[num].y=planes[num].y-2; } void down(int num) {planes[num].y=planes[num].y+2;} ///////////////////////////////////////////////////////////////////////// // getinput function // check for player input keys ///////////////////////////////////////////////////////////////////////// void getinput() { //hit ESC to quit if (key[KEY_ESC]) gameover = 1; if (key[KEY_RIGHT]) forward(0); if (key[KEY_LEFT]) backward(0); if (key[KEY_UP]) up(0); if (key[KEY_DOWN]) down(0); //short delay after keypress rest(10); } ///////////////////////////////////////////////////////////////////////// // setupplanes function // ///////////////////////////////////////////////////////////////////////// void setupplanes() { planes[0].x = 50; planes[0].y = 50; planes[0].speed = 0; planes[0].color = 9; } void setupenemy() { planes[1].x = 590; planes[1].y = 50; planes[1].speed = 0; planes[1].color = 5; } ///////////////////////////////////////////////////////////////////////// // collission function // checkpath and getpixel ///////////////////////////////////////////////////////////////////////// checkpath(int x1,int x2,int x3,int x4,int y1,int y2,int y3,int y4) { if(getpixel(screen,planes[0].x,planes[0].y)) { rectfill(screen,50,10,50,10,6); } } ///////////////////////////////////////////////////////////////////////// // setupscreen function // set up the graphics mode and game screen ///////////////////////////////////////////////////////////////////////// void setupscreen() { //set video mode int ret = set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0); if (ret != 0) { allegro_message(allegro_error); return; } //draw screen border rect(screen, 0, 12, SCREEN_W-1, SCREEN_H-1, 4); rect(screen, 1, 13, SCREEN_W-2, SCREEN_H-2, 4); floodfill(screen, 70, 200, 12); } //set up music int setupsound() { if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)) { allegro_message("Sound Error: %s", allegro_error); return 1; }set_volume(255, 255); //digital and music to max levels SoundPlayDigi(SAMPLE* samp, int vol=-1, int loopflag=false); } ///////////////////////////////////////////////////////////////////////// // main function // start point of the program ///////////////////////////////////////////////////////////////////////// void main(void) { //initialize everything allegro_init(); install_keyboard(); install_timer(); srand(time(NULL)); setupscreen(); setupplanes(); setupenemy(); setupmusic(); //game loop while(!gameover) { //erase the planes eraseplane(0); eraseplane(1); //move the planes moveplane(0); //draw the planes drawplane(0); drawplane(1); //check for keypresses if (keypressed()) getinput(); //slow the game down (adjust as necessary) rest(30); } //checkpath(); //end program allegro_exit(); } END_OF_MAIN(); ///////////////////////////////////////// ////////////HEADER FILE //////////////////////////////////////////// #ifndef _PLANE_H #define _PLANE_H #include "allegro.h" //define some game constants #define MODE GFX_AUTODETECT_WINDOWED #define WIDTH 640 #define HEIGHT 480 #define MAXSPEED 2 #define dir 1 //define PALNE structure struct Plane { int x,y; int speed; int color; int score; } planes[1]; int gameover = 0; void drawplane(int num); void eraseplane(int num); void moveplane(int num); void forward(int num); void backward(int num); void getinput(); void setupplanes(); void setupscreen(); void collision(); #endif