#include "tanks.h" void erasesprite(BITMAP *dest, SPRITE *spr) { //erase the sprite using BLACK color fill rectfill(dest, spr->x, spr->y, spr->x + spr->width, spr->y + spr->height, BLACK); } void updatesprite(SPRITE *spr) { if (++spr->xcount > spr->xdelay) spr->xcount = 0; //update y position if (++spr->ycount > spr->ydelay) spr->ycount = 0; //update frame based on dir if (++spr->framecount > spr->framedelay) { spr->framecount = 0; //wouth && southwest if(spr->dir == 1 || spr->dir == 7) { spr->curframe++; if (spr->curframe < 0) spr->curframe = 2; else if(spr->curframe > 2) spr->curframe = 0; } //north && northeast && northwest else if (spr->dir == 2 || spr->dir == 5 || spr->dir == 8) { ++spr->curframe; if(spr->curframe < 3) spr->curframe = 5; else if(spr->curframe > 5) spr->curframe = 3; } //east && southeast else if(spr->dir == 3 || spr->dir == 6) { ++spr->curframe; if(spr->curframe < 6) spr->curframe = 8; else if(spr->curframe > 8) spr->curframe = 6; } //south else if(spr->dir == 4) { if(spr->curframe != 11) spr->curframe++; if(spr->curframe == 11) spr->curframe = 9; else if(spr->curframe < 9) spr->curframe = 11; } } } void move_character() { int dir = mario->dir; //update character position based on direction switch(dir) { //west case 1: mario->x -= num1; break; //north case 2: mario->y -= num1; break; //east case 3: mario->x += num1; break; //south case 4: mario->y += num1; break; //northeast case 5: mario->x += num2; mario->y -= num2; break; //souteast case 6: mario->x += num2; mario->y += num2; break; //southwest case 7: mario->x -= num2; mario->y += num2; break; //northwest case 8: mario->x -= num2; mario->y -= num2; break; } //fix this if (mario->x > (SCREEN_W-spr->w))//18) mario->x = SCREEN_W-spr->w; if (mario->x < spr->w)//18) mario->x = spr->w; if (mario->y > (SCREEN_H-spr->h))//18) mario->y = SCREEN_H-spr->h; if (mario->y < spr->h)//18) mario->y = spr->h; } void getinput() { //hit ESC to quit //if (key[KEY_ESC]) gameover = 1; //WASD - SPACE keys control tank 1 if(key[KEY_UP]) north(); if(key[KEY_RIGHT]) east(); if(key[KEY_LEFT]) west(); if(key[KEY_DOWN]) south(); if((key[KEY_UP]) && (key[KEY_RIGHT])) northeast(); if((key[KEY_RIGHT]) && (key[KEY_DOWN])) southeast(); if((key[KEY_LEFT]) && (key[KEY_DOWN])) southwest(); if((key[KEY_LEFT]) && (key[KEY_UP])) northwest(); //short delay after keypress rest(10); } void draw_character_mario() { int dir = mario->dir; draw_sprite(screen, marioimg[mario->curframe], mario->x, mario->y); } void calculate_position(int x, int y) { if(mario->x < x) { xdir = 3; xnew = x - mario->x; if(mario->y > y) { ydir = 2; ynew = mario->y - y; } else if(mario->y < y) { ydir = 4; ynew = y - mario->y; } } else if(mario->x > x) { xdir = 1; xnew = mario->x - x; if(mario->y > y) { ydir = 2; ynew = mario->y - y; } else if(mario->y < y) { ydir = 4; ynew = y - mario->y; } } } BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame) { BITMAP *temp = create_bitmap(width,height); int x = startx + (frame % columns) * width; int y = starty + (frame / columns) * height; masked_blit(source,temp,x,y,0,0,width,height); return temp; } void setup_screen() { //initialize allegro_init(); set_color_depth(32); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); install_keyboard(); install_mouse(); install_timer(); srand(time(NULL)); textout(screen, font, "Project 2: Animation (ESC to quit)", 0, 0, WHITE); } void setup_frames(BITMAP* temp) { //load 32-frame tiled sprite image temp = load_bitmap("linkss.bmp", NULL); int n; for (n=0; n<12; n++) marioimg[n] = grabframe(temp,30,30, 0,0,3,n); destroy_bitmap(temp); //initialize the sprite's struct fields mario->x = 300; mario->y = 300; mario->width = marioimg[0]->w; mario->height = marioimg[0]->h; mario->xdelay = 1; mario->ydelay = 0; mario->xcount = 0; mario->ycount = 0; mario->curframe = 0; mario->maxframe = 11; mario->framecount = 0; mario->framedelay = 1; mario->animdir = 1; mario->dir = 1; show_mouse(screen); } void main(void) { BITMAP* temp; setup_screen(); setup_frames(temp); //while loop while(!key[KEY_ESC]) { if(mouse_b & 1 && !rmb_down) { mx = mouse_x; my = mouse_y; calculate_position(mx, my); int x1, y1; mario->dir = xdir; //tweak the position so Link ends up exactly at cursor if(mario->dir == 3) xnew = xnew/5 - 2; else if(mario->dir == 1) xnew = xnew/5 + 3; for(x1 = 0; x1dir = ydir; if(mario->dir == 2) ynew = ynew/5 + 2; else if(mario->dir == 4) ynew = ynew/5 - 2; for(y1 = 0; y1dir = 0; acquire_screen(); draw_character_mario(); release_screen(); rest(70); } return 0; } END_OF_MAIN();