Code: Select all
#include <allegro.h>
int x = 10;
int y = 10;
bool collision = false;
int bass_bb_left = 0;
int bass_bb_top = 0;
int bass_bb_right = 0;
int bass_bb_bottom = 0;
int m_bb_left = 0;
int m_bb_top = 0;
int m_bb_right = 0;
int m_bb_bottom = 0;
int cmap_left[12][16];
int cmap_top[12][16];
int cmap_right[12][16];
int cmap_bottom[12][16];
int map[12][16] = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2},
{1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3},
{1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,4},
{1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3},
{1,1,1,1,1,1,1,1,1,1,2,3,3,3,3,3}};
BITMAP *my_pic = NULL;
int startX[18] = {3,19,51,94,143,186,220,255,290,4,52,84,127,189,236,280,315,349};
int startY[18] = {0,0,0,0,0,12,12,12,12,69,69,69,69,69,69,69,69,69};
int endX[18] = {14,27,39,45,39,31,31,31,41,39,29,33,42,45,40,28,32,41};
int endY[18] = {64,64,64,64,64,52,52,52,52,48,48,48,48,48,48,48,48,48};
int frame = 0;
void setup();
void game();
void init();
void deinit();
int main()
{
setup();
}
END_OF_MAIN()
void setup()
{
init();
game();
deinit();
}
void game()
{
my_pic = load_bitmap("bass1.bmp", NULL);
BITMAP *Buffer = create_bitmap( 640, 480);
if(Buffer == NULL)
{
set_gfx_mode(GFX_TEXT,0,0,0,0);
allegro_message("Could not create buffer!");
exit(EXIT_FAILURE);
}
while (!key[KEY_ESC])
{
clear_keybuf();
clear_bitmap(Buffer);
acquire_screen();
//update bounding Boxes
bass_bb_left = x;
bass_bb_top = y;
bass_bb_right = bass_bb_left + endX[frame];
bass_bb_bottom = bass_bb_top + endY[frame];
m_bb_left = mouse_x;
m_bb_top = mouse_y;
m_bb_right = m_bb_left + endX[frame];
m_bb_bottom = m_bb_top + endY[frame];
for (int i = 0; i <= 12; i++)
{
for( int t = 0; t <= 16; t++)
{
if( map[i][t] == 1)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 440, makecol( 128, 255, 255));
cmap_left[i][t] = 0;
cmap_top[i][t] = 0;
cmap_right[i][t] = 0;
cmap_bottom[i][t] = 0;
}
else if( map[i][t] == 2)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 128, 0));
cmap_left[i][t] = t * 40;
cmap_top[i][t] = i * 40;
cmap_right[i][t] = (t + 1) * 40;
cmap_bottom[i][t] = (i + 1) * 40;
}
else if( map[i][t] == 3)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 0, 0));
cmap_left[i][t] = 0;
cmap_top[i][t] = 0;
cmap_right[i][t] = 0;
cmap_bottom[i][t] = 0;
}
else if( map[i][t] == 4)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 0, 0, 0));
cmap_left[i][t] = 0;
cmap_top[i][t] = 0;
cmap_right[i][t] = 0;
cmap_bottom[i][t] = 0;
}
}
}
collision = true;
if(bass_bb_bottom < m_bb_top)
{
collision = false;
}
else if(bass_bb_top > m_bb_bottom)
{
collision = false;
}
else if(bass_bb_right < m_bb_left)
{
collision = false;
}
else if(bass_bb_left > m_bb_right)
{
collision = false;
}
//Map Collision
for (int r = 0; r <= 12; r++){
for( int c = 0; c <= 16; c++){
if(bass_bb_bottom < cmap_top[r][c] && cmap_top[r][c] != 0)
{
collision = false;
}
else if(bass_bb_top > cmap_bottom[r][c] && cmap_bottom[r][c] != 0)
{
collision = false;
}
else if(bass_bb_right < cmap_left[r][c] && cmap_left[r][c] != 0)
{
collision = false;
}
else if(bass_bb_left > cmap_right[r][c] && cmap_right[r][c] != 0)
{
collision = false;
}
}
}
//get keyboard
if (key[KEY_UP])
{
y -= 3;
if(frame >= 9 && frame <= 16)
{
frame ++;
}
else if(frame < 9)
{
frame = 9;
}
else if (frame >= 17)
{
frame = 9;
}
}
else if (key[KEY_DOWN])
{
y += 3;
if(frame >= 9 && frame <= 16)
{
frame ++;
}
else if(frame < 9)
{
frame = 9;
}
else if (frame >= 17)
{
frame = 9;
}
}
else if (key[KEY_RIGHT])
{
x += 3;
if(frame >= 9 && frame <= 16)
{
frame ++;
}
else if(frame < 9)
{
frame = 9;
}
else if (frame >= 17)
{
frame = 9;
}
}
else if (key[KEY_LEFT])
{
x -= 3;
if(frame >= 9 && frame <= 16)
{
frame ++;
}
else if(frame < 9)
{
frame = 9;
}
else if (frame >= 17)
{
frame = 9;
}
}
else
{
frame = 7;
}
if(collision == true)
{
textout_ex( Buffer, font, "Collision!", 0, 0, makecol( 0, 0, 255), makecol( 0, 0, 0) );
frame = 5;
}
//sprite
masked_blit(my_pic, Buffer, startX[frame],startY[frame],x,y,endX[frame],endY[frame]);
masked_blit(my_pic, Buffer, startX[frame],startY[frame],mouse_x,mouse_y,endX[frame],endY[frame]);
/*
line(Buffer, bass_bb_left, bass_bb_top, bass_bb_right, bass_bb_top, makecol(255,0,0));
line(Buffer, bass_bb_left, bass_bb_bottom, bass_bb_right, bass_bb_bottom, makecol(255,0,0));
line(Buffer, bass_bb_left, bass_bb_top, bass_bb_left, bass_bb_bottom, makecol(255,0,0));
line(Buffer, bass_bb_right, bass_bb_top, bass_bb_right, bass_bb_bottom, makecol(255,0,0));
line(Buffer, m_bb_left, m_bb_top, m_bb_right, m_bb_top, makecol(255,0,0));
line(Buffer, m_bb_left, m_bb_bottom, m_bb_right, m_bb_bottom, makecol(255,0,0));
line(Buffer, m_bb_left, m_bb_top, m_bb_left, m_bb_bottom, makecol(255,0,0));
line(Buffer, m_bb_right, m_bb_top, m_bb_right, m_bb_bottom, makecol(255,0,0));
*/
if (collision == false)
{
y += 10;
}
if (y >= 480)
{
y =0;
}
draw_sprite( screen, Buffer, 0, 0);
release_screen();
rest(50);
}
}
void init() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
}
void deinit() {
clear_keybuf();
}
pretty messy code, I know I should organize them into classes and stuff, but its bugging me, Im trying to draw the map and add collision onto the map, but not working... the collision with the mouse and drawing the map works but when I added the collision stuff for the map, it just makes a black blank screen and exits in like 1 second... I know the problem is here but I see nothing wrong...
Code: Select all
for (int i = 0; i <= 12; i++)
{
for( int t = 0; t <= 16; t++)
{
if( map[i][t] == 1)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 440, makecol( 128, 255, 255));
cmap_left[i][t] = 0;
cmap_top[i][t] = 0;
cmap_right[i][t] = 0;
cmap_bottom[i][t] = 0;
}
else if( map[i][t] == 2)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 128, 0));
cmap_left[i][t] = t * 40;
cmap_top[i][t] = i * 40;
cmap_right[i][t] = (t + 1) * 40;
cmap_bottom[i][t] = (i + 1) * 40;
}
else if( map[i][t] == 3)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 255, 0, 0));
cmap_left[i][t] = 0;
cmap_top[i][t] = 0;
cmap_right[i][t] = 0;
cmap_bottom[i][t] = 0;
}
else if( map[i][t] == 4)
{
rectfill( Buffer, t * 40, i * 40, (t + 1) * 40, (i + 1) * 40, makecol( 0, 0, 0));
cmap_left[i][t] = 0;
cmap_top[i][t] = 0;
cmap_right[i][t] = 0;
cmap_bottom[i][t] = 0;
}
}
}