1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#include "image.h"
#include "program9handler.h"
#include "bulletholder.h"
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
#include <cstdlib>
int height=480;
int width=640;
int margin=40;
int delay=50;
int speed=10;
int bullet_speed=5;
int it = 8;
ALLEGRO_COLOR wallcolor;
ALLEGRO_DISPLAY* display;
//ALLEGRO_BITMAP* scc;
ALLEGRO_BITMAP* back;
ALLEGRO_BITMAP* t1 = 0;
ALLEGRO_BITMAP* t2 = 0;
ALLEGRO_BITMAP* bul = 0;
ALLEGRO_BITMAP* dt = 0;
ALLEGRO_TIMER *timer;
const float FPS = 24;
//pics
int t1len=0;
int t2len=0;
int dtlen = 0;
int bullen = 0;
int fact1 = 0;
int fact2 = 0;
int facdt = 0;
int facb = 0;
int dly = 200;
int count;
image imagemaker;
bulletholder bt1;
bulletholder bt2;
program9handler tank1, tank2;
void draw();
int setup();
void initializetanks();
void initializebullets();
...
void initializebullets()
{
bt1.initialize(width,height,bullet_speed,it);
bt2.initialize(width,height,bullet_speed,it);
char bb[256];
char td[256];
for(int i=0;i<it;i++)
{
bt1.sethitboundaries(al_create_sub_bitmap(bul,i*bullen,0,bullen,al_get_bitmap_height(bul)),i);
bt2.sethitboundaries(al_create_sub_bitmap(bul,i*bullen,0,bullen,al_get_bitmap_height(bul)),i);
}
}
void draw()
{
al_set_target_bitmap(al_get_backbuffer(display));
al_clear_to_color(wallcolor);
if(bt1.checkkill(tank2))
{
tank2.kill();
}
if(bt2.checkkill(tank1))
{
fprintf(stdout,"Its a hit tank 1 should be dead\n");
tank1.kill();
}
bt1.move();
bt2.move();
tank1.move(tank2);
tank2.move(tank1);
//al_draw_bitmap(t1,0,0,0);
//al_draw_bitmap(t2,0,200,0);
if(tank2.fire==1)
{
bt2.shoot(tank2.x,tank2.y,tank2.dir);
tank2.fired();
}
if(tank1.fire==1)
{
fprintf(stdout,"Tank 1 In if statement\n");
bt1.shoot(tank1.x,tank1.y,tank1.dir);
tank1.fired();
}
if(tank1.isdead==0)
{
//fact1,al_get_bitmap_width(t1)/it
al_draw_bitmap(al_create_sub_bitmap(t1,t1len*tank1.dir,0,t1len,t1len),(float)tank1.x,(float)tank1.y,0);
}
else
{
//facdt
al_draw_bitmap(al_create_sub_bitmap(dt,dtlen*tank1.dir,0,dtlen,dtlen),(float)tank1.x,(float)tank1.y,0);
}
if(tank2.isdead==0)
{
//fact2
al_draw_bitmap(al_create_sub_bitmap(t2,t2len*tank2.dir,0,t2len,t2len),(float)tank2.x,(float)tank2.y,0);
}
else
{
//facdt
al_draw_bitmap(al_create_sub_bitmap(dt,dtlen*tank2.dir,0,dtlen,dtlen),(float)tank2.x,(float)tank2.y,0);
}
int a = bt2.getnumbullets();
for(int i=0;i<a;i++)
{
//facb
al_draw_bitmap(al_create_sub_bitmap(bul,bullen*bt2.getdir(i),0,al_get_bitmap_width(bul)/it,al_get_bitmap_height(bul)),(float)bt2.getx(i),(float)bt2.gety(i),0);
}
a = bt1.getnumbullets();
for(int i=0;i<a;i++)
{
//facb
al_draw_bitmap(al_create_sub_bitmap(bul,bullen*bt1.getdir(i),0,al_get_bitmap_width(bul)/it,al_get_bitmap_height(bul)),(float)bt1.getx(i),(float)bt1.gety(i),0);
}
al_flip_display();
}
int main(int argc, char **argv)
{
int i = setup();
initializetanks();
initializebullets();
wallcolor = al_map_rgb(50,230,10);
if(i != 0)
{
return i;
}
else
{
ALLEGRO_EVENT_QUEUE *queue;
queue = al_create_event_queue();
if(!queue) {
fprintf(stderr, "failed to create event queue!\n");
al_destroy_display(display);
return -1;
}
al_register_event_source(queue, al_get_display_event_source(display));
al_register_event_source(queue, al_get_keyboard_event_source());
al_register_event_source(queue, al_get_mouse_event_source());
al_register_event_source(queue, al_get_timer_event_source(timer));
al_start_timer(timer);
int quit = 0;
int redraw = 0;
while (!quit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(queue, &ev);
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
if(ev.keyboard.keycode==ALLEGRO_KEY_LEFT)
{
tank1.changedirection(-1);
}
else if(ev.keyboard.keycode==ALLEGRO_KEY_RIGHT)
{
tank1.changedirection(1);
}
else if(ev.keyboard.keycode==ALLEGRO_KEY_DOWN)
{
tank1.goback();
}
else if(ev.keyboard.keycode==ALLEGRO_KEY_SPACE)
{
tank1.startstop();
}
else if(ev.keyboard.keycode==ALLEGRO_KEY_UP)
{
tank1.shoot();
}
else if(ev.keyboard.keycode==ALLEGRO_KEY_ESCAPE)
{
quit=1;
}
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
quit = 1;
}
else if(ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = 1;
}
if(redraw && al_is_event_queue_empty(queue))
{
draw();
}
}
al_destroy_event_queue(queue);
}
//al_destroy_event_queue(queue);
al_destroy_bitmap(t1);
al_destroy_bitmap(t2);
al_destroy_bitmap(bul);
al_destroy_bitmap(dt);
al_destroy_display(display);
al_destroy_timer(timer);
fprintf(stdout,"made it to last line\n");
return 0;
}
|