Code:
//#define DEBUGMODE //include this if your debugging
#include <stdio.h>
#include <allegro.h>
#include "test.h"
#define left 1
#define right 2
#define up 3
#define down 4
#define UpandLeft 5
#define DownandLeft 6
#define UpandRight 7
#define DownandRight 8
#define space 9
#define ericw 43 // width of eric.bmp
#define erich 61 // hight of eric.bmp
struct bullet{
int bulletx;
int bullety;
int direction;
}bullets[256]; // this stores information on all the bullets in the air
void calculate_shot_movment(int direction, int shot_draw_number);
PALETTE Pal;
BITMAP *ImageBuffer;
BITMAP *Ammobuffer; // stores the the immage of the amunition
int main(void)
{
BITMAP *dblbuffer;
int x=100;
int y=100;
int keyvar=0; //a keyboard buffer
signed short shot_number=-1; //which shot are we dealing with now
int lastkeypressed=0;
signed short shot_draw_number=0;//which shot to draw
int waskeydown=FALSE; //used to tell if a keypressed was aready there
allegro_init();
install_keyboard();
install_timer(); /* needed for `rest' function */
set_color_depth(16);
if (set_gfx_mode(GFX_SAFE, 800, 600, 0, 0) < 0)
{
printf("%s\n", allegro_error);
exit(1);
}
clear(screen);
// Pay attention!!! We HAVE to allocate memory for our memory bitmap
dblbuffer = create_bitmap(SCREEN_W, SCREEN_H);
clear(dblbuffer);
ImageBuffer = load_bmp("Eric.bmp", Pal);
Ammobuffer = load_bmp("rock.bmp", Pal);
while(1)// main loop
{
/////////////////// input
if (key[KEY_LEFT])
{
x--;
lastkeypressed=left;
}
if (key[KEY_RIGHT])
{
x++;
lastkeypressed=right;
}
if (key[KEY_UP])
{
y--;
lastkeypressed=up;
}
if (key[KEY_DOWN])
{
y++;
lastkeypressed=down;
}
if (key[KEY_LEFT] && key[KEY_UP])
{
lastkeypressed=UpandLeft;
}
if (key[KEY_LEFT] && key[KEY_DOWN])
{
lastkeypressed=DownandLeft;
}
if (key[KEY_RIGHT] && key[KEY_UP])
{
lastkeypressed=UpandRight;
}
if (key[KEY_RIGHT] && key[KEY_DOWN])
{
lastkeypressed=DownandRight;
}
/*
if(keypressed())
keyvar=readkey();
else keyvar=0;
*/
if (key[KEY_SPACE] && waskeydown==FALSE)
{
waskeydown=FALSE;// to prevent mutiple shots from one press
shot_number++;
if (shot_number>255)
{shot_number = shot_number-255;} // makes it so shot number has a max value of 255
bullets[shot_number].direction=lastkeypressed;
if(lastkeypressed==up || lastkeypressed==UpandLeft || // if there was an up direction
lastkeypressed==UpandRight)
{
bullets[shot_number].bullety=y-5; // the y position of the bullet is five pixles above the player
}
else if(lastkeypressed==down || lastkeypressed==DownandLeft // if there was a down direction
||lastkeypressed==DownandRight)
{
bullets[shot_number].bullety=y+erich+5; // the y position of the bullet is five pixles below the player
}
else
{
bullets[shot_number].bullety=y+(erich/2); // the y position of the bullet is level with the player}
}
if(lastkeypressed==left || lastkeypressed==UpandLeft // if there was a left direction
|| lastkeypressed==DownandLeft)
{
bullets[shot_number].bulletx=x-5; //// the x position of the bullet is five pixles to the left of
}
else if(lastkeypressed==right || bullets[shot_number].direction==UpandRight // if there was a left direction
|| lastkeypressed==DownandRight)
{
bullets[shot_number].bulletx=x+ericw+5; //// the x position of the bullet is five pixles to the right of the player
}
else
{
bullets[shot_number].bulletx=x+(ericw/2);
}
}
if (keypressed()!=TRUE)
{
waskeydown=FALSE;
}
//////////////////output
if (lastkeypressed==right)
{
rectfill(dblbuffer, x - 1 , y ,x + ericw - 1, y + erich, makecol(000,000,000)); /* erase from last place */
}
if (lastkeypressed==left)
{
rectfill(dblbuffer, x + 1 , y ,x + ericw + 1, y + erich, makecol(000,000,000)); /* erase from last place */
}
if (lastkeypressed==up)
{
rectfill(dblbuffer, x , y + 1,x + ericw , y + erich + 1, makecol(000,000,000)); /* erase from last place */
}
if (lastkeypressed==down)
{
rectfill(dblbuffer, x , y - 1,x + ericw ,y + erich - 1, makecol(000,000,000)); /* erase from last place */
}
if (lastkeypressed==UpandLeft)
{
rectfill(dblbuffer, x + 1 ,y + 1,x + ericw + 1, y + erich + 1, makecol(000,000,000)); /* erase from last place */
}
if (lastkeypressed==DownandLeft)
{
rectfill(dblbuffer, x + 1 ,y - 1,x + ericw + 1, y + erich - 1, makecol(000,000,000)); /* erase from last place */
}
if (lastkeypressed==UpandRight)
{
rectfill(dblbuffer, x - 1 ,y + 1,x + ericw - 1, y + erich + 1, makecol(000,000,000)); /* erase from last place */
}
if (lastkeypressed==DownandRight)
{
rectfill(dblbuffer, x - 1 ,y - 1,x + ericw - 1, y + erich - 1, makecol(000,000,000)); /* erase from last place */
}
// if (lastkeypressed=space)
// {
// draw_sprite(dblbuffer, Ammobuffer, shotx, shoty);
// }
draw_sprite(dblbuffer, ImageBuffer, x, y); //draw bitmap on dblbuffer
while (shot_draw_number < shot_number)
{
if (bullets[shot_draw_number].direction==DownandRight)
{
bullets[shot_draw_number].bulletx++;
bullets[shot_draw_number].bullety++;
}
if (bullets[shot_draw_number].direction==UpandLeft)
{
bullets[shot_draw_number].bulletx--;
bullets[shot_draw_number].bullety--;
}
if (bullets[shot_draw_number].direction==DownandLeft)
{
bullets[shot_draw_number].bulletx--;
bullets[shot_draw_number].bullety++;
}
if (bullets[shot_draw_number].direction==UpandRight)
{
bullets[shot_draw_number].bulletx++;
bullets[shot_draw_number].bullety--;
}
if (bullets[shot_draw_number].direction==down)
{
bullets[shot_draw_number].bullety++;
}
if (bullets[shot_draw_number].direction==up)
{
bullets[shot_draw_number].bullety--;
}
if (bullets[shot_draw_number].direction==left)
{
bullets[shot_draw_number].bulletx--;
}
if (bullets[shot_draw_number].direction==right)
{
bullets[shot_draw_number].bulletx++;
}
//calculate_shot_movment(bullets[shot_draw_number].direction, shot_draw_number);
draw_sprite(dblbuffer, Ammobuffer, bullets[shot_number].bulletx, bullets[shot_draw_number].bullety); //draw the bullet
shot_draw_number++;
}
shot_draw_number=0;
vsync();
blit(dblbuffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // copy from double buffer to screen
if (key[KEY_ESC])//clean up and quit
{
// Now, remember to FREE the memory you previously allocated
destroy_bitmap(dblbuffer);
destroy_bitmap(ImageBuffer);
destroy_bitmap(Ammobuffer);
allegro_exit();
exit(1);
}
}
return 0;
}
END_OF_MAIN();
// a function i'm working on to calculate a shot movement
void calculate_shot_movment(int direction, int shot_draw_number)
{
int* rbulletx = &bullets[shot_draw_number].bulletx; // note to self change to references
int* rbullety = &bullets[shot_draw_number].bullety;
if (direction==DownandRight)
{
*rbulletx++;
*rbullety++;
}
if (direction==UpandLeft)
{
*rbulletx--;
*rbullety--;
}
if (direction==DownandLeft)
{
*rbulletx--;
*rbullety++;
}
if (direction==UpandRight)
{
*rbulletx++;
*rbullety--;
}
if (direction==down)
{
*rbullety++;
}
if (direction==up)
{
*rbullety--;
}
if (direction==left)
{
*rbulletx--;
}
if (direction==right)
{
*rbulletx++;
}
}