Jac0blee

Member
  • Content count

    4
  • Joined

  • Last visited

Community Reputation

100 Neutral

About Jac0blee

  • Rank
    Newbie
  1. Help with tiled maps

    Thank you guys so much for the help you really explained it well, i do know about collision detection and was just wondering how to implement it into tiled maps so that i could have walls and such and the code for the mouse directional shooting is very helpful thankyou!
  2. Hi guys i was experementing with tiled maps and wondered how to make the user stop when they hit a wall. also how to make directional bullet firing controlled by mouse. im a new programmer and need a little bit of a kickstart thanks for the help!
  3. First Post

    Hi this is my first forum post, as of now i have developed one game that i will share with you all soon, im working on a second game that uses finite ai (the bad guys move towards you) and was wondering how to make walls and how to have a tiled background move correctly with the player and enemies heres the code for the game:[CODE] #include <allegro5\allegro.h> #include <allegro5\allegro_image.h> #include <allegro5\allegro_primitives.h> #include <allegro5\allegro_font.h> #include <allegro5\allegro_ttf.h> #include <iostream> #include <math.h> #include "classes.h" using namespace std; const int WIDTH = 800; const int HEIGHT = 600; const int NUM_BULLETS = 10; const int NUM_BADDIES = 50; int num; bool movingMap = true; const int cavex = WIDTH - 100; const int cavey = HEIGHT / 2; bool keys[] = {false, false, false, false, false}; enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE}; enum STATE{IDLE, CHASING, RETREATING, ENTERING}; ALLEGRO_FONT *font18; void ChangeState(int &state, int newState); float CheckDistance(int x1, int y1, int x2, int y2); float AngleToTarget(int x1, int y1, int x2, int y2); void IsPlayerDead(Player &player, Enemy bad[]); void InitPlayer(Player &player); void MovePlayerRight(Player &player); void MovePlayerLeft(Player &player); void MovePlayerUp(Player &player); void MovePlayerDown(Player &player); //void ResetPlayerDir(Player &player); void InitEnemy(Enemy bad[]); void StartEnemy(Enemy bad[]); void InitBullet(Bullet bullets[], int size); void FireBullet(Bullet bullets[], Player &player, int size); void DrawBullet(Bullet bullets[], int size); void UpdateBullet(Bullet bullets[], int size); void CollideBullet(Bullet bullets[], int size, Enemy bad[]); int main(int argc, char **argv) { //============================================== //SHELL VARIABLES //============================================== bool done = false; bool render = false; float gameTime = 0; int frames = 0; int gameFPS = 0; int xOff = 0; int yOff = 0; int mapColumns = 10; int mapSize = 100; int tileSize = 100; int map[] = {0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0}; //============================================== //PROJECT VARIABLES //============================================== Player player; Enemy bad[NUM_BADDIES]; Bullet bullets[NUM_BULLETS]; int state = -1; int threshold = 200; //============================================== //ALLEGRO VARIABLES //============================================== ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *bgSheet = NULL; //============================================== //ALLEGRO INIT FUNCTIONS //============================================== if(!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if(!display) //test display object return -1; //============================================== //ADDON INSTALL //============================================== al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); //============================================== //PROJECT INIT //============================================== font18 = al_load_font("grunge.ttf", 50, 0); bgSheet = al_load_bitmap("Background.bmp"); InitPlayer(player); InitEnemy(bad); InitBullet(bullets, NUM_BULLETS); al_install_mouse(); ChangeState(state, IDLE); //============================================== //TIMER INIT AND STARTUP //============================================== event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_start_timer(timer); gameTime = al_current_time(); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); //============================================== //INPUT //============================================== if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: player.dirX = -1; player.dirY = 0; keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: player.dirX = 1; player.dirY = 0; keys[RIGHT] = true; break; case ALLEGRO_KEY_UP: player.dirY = -1; player.dirX = 0; keys[UP] = true; break; case ALLEGRO_KEY_DOWN: player.dirY = 1; player.dirX = 0; keys[DOWN] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; FireBullet(bullets, player, NUM_BULLETS); break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES) { //playerx = ev.mouse.x; //playery = ev.mouse.y; } //============================================== //GAME UPDATE //============================================== else if(ev.type == ALLEGRO_EVENT_TIMER) { if(player.live == true) { if(xOff + (tileSize * mapColumns) > WIDTH) xOff -= keys[RIGHT] * player.speed; if(xOff < 0) xOff += keys[LEFT] * player.speed; if(yOff + (tileSize * mapColumns) > HEIGHT) yOff -= keys[DOWN] * player.speed; if(yOff < 0) yOff += keys[UP] * player.speed; } // moving the player if(keys[UP] == true) MovePlayerUp(player); else if(keys[DOWN] == true) MovePlayerDown(player); if(keys[LEFT] == true) MovePlayerLeft(player); else if(keys[RIGHT] == true) MovePlayerRight(player); StartEnemy(bad); IsPlayerDead(player, bad); UpdateBullet(bullets, NUM_BULLETS); CollideBullet(bullets, NUM_BULLETS, bad); if(!player.live) { al_draw_textf(font18, al_map_rgb(255,255,0), WIDTH - 600, 10, 0, "You are Dead!"); } render = true; //UPDATE FPS=========== frames++; if(al_current_time() - gameTime >= 1) { gameTime = al_current_time(); gameFPS = frames; frames = 0; } //===================== for(int i = 0; i < NUM_BADDIES; i++) { if(bad[i].live) { // if(state == IDLE) // { // if(threshold > CheckDistance(player.x, player.y, bad.x, bad.y)) // ChangeState(state, CHASING); // } /*else*/ //if(state == CHASING) // { // if(threshold < CheckDistance(bad.x, bad.y, cavex, cavey)) // { // ChangeState(state, RETREATING); // } //else //{ float angle = AngleToTarget(bad[i].x, bad[i].y, player.x, player.y); bad[i].y += (bad[i].speed * sin(angle)); bad[i].x += (bad[i].speed * cos(angle)); // if(threshold < CheckDistance(player.x, player.y, bad.x, bad.y)) // ChangeState(state, RETREATING); //} // } /*else if(state == RETREATING) { if(2 >= CheckDistance(bad.x, bad.y, cavex, cavey)) { bad.x = cavex; bad.y = cavey; ChangeState(state, IDLE); } else { float angle = AngleToTarget(bad.x, bad.y, cavex, cavey); bad.y += (2 * sin(angle)); bad.x += (2 * cos(angle)); if(threshold > CheckDistance(bad.x, bad.y, player.x, player.y)) ChangeState(state, CHASING); } }*/ } else if(!bad[i].live) { bad[i].x = -10; bad[i].y = -10; } } } //============================================== //RENDER //============================================== if(render && al_is_event_queue_empty(event_queue)) { for(int i = 0; i < mapSize; i++) { al_draw_bitmap_region(bgSheet, tileSize * map[i], 0, tileSize, tileSize, xOff + tileSize * (i % mapColumns), yOff + tileSize * (i / mapColumns), 0); } render = false; al_draw_textf(font18, al_map_rgb(255 ,0 ,255), 5, 5, 0, "FPS: %i", gameFPS); //display FPS on screen al_draw_textf(font18, al_map_rgb(255 ,0 ,255), 100, 5, 0, "Lives: %i", player.lives / 10); al_draw_textf(font18, al_map_rgb(255 ,0 ,255), 100, 20, 0, "SPAWN: %i", num); //BEGIN PROJECT RENDER================ if(player.live) { al_draw_filled_rectangle(player.x - 10, player.y - 10, player.x + 10, player.y + 10, al_map_rgba_f(.3,0,.2,.5)); } DrawBullet(bullets, NUM_BULLETS); al_draw_textf(font18, al_map_rgb(255,0,255), 5, 50, 0, "Player dirX: %i PlayerdirY: %i", player.dirX, player.dirY); for(int i = 0; i < NUM_BADDIES; i++) { if(bad[i].live) { al_draw_filled_rectangle(bad[i].x - 10, bad[i].y - 10, bad[i].x + 10, bad[i].y + 10, al_map_rgba_f(.5,0,.5,.5)); al_draw_rectangle(player.x - player.boundx, player.y - player.boundy, player.x + player.boundx, player.y + player.boundy, al_map_rgb(0,0,255), 1); al_draw_rectangle(bad[i].x - bad[i].boundx, bad[i].y - bad[i].boundy, bad[i].x + bad[i].boundx, bad[i].y + bad[i].boundy, al_map_rgb(0,0,255), 1); } } //FLIP BUFFERS======================== //ResetPlayerDir(player); al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } //============================================== //DESTROY PROJECT OBJECTS //============================================== //SHELL OBJECTS================================= al_destroy_font(font18); al_destroy_timer(timer); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; } void ChangeState(int &state, int newState) { if(state == IDLE) { cout << "leaving IDLE state" << endl; } else if(state == CHASING) { cout << "leaving CHASING state" << endl; } else if(state == RETREATING) { cout << "leaving RETREATING state" << endl; } state = newState; if(state == IDLE) { cout << "now IDLING" << endl; } else if(state == CHASING) { cout << "now CHASING" << endl; } else if(state == RETREATING) { cout << "now RETREATING" << endl; } } float CheckDistance(int x1, int y1, int x2, int y2) { return sqrt(pow((float)x1 - x2, 2) + pow((float)y1 - y2, 2)); } float AngleToTarget(int x1, int y1, int x2, int y2) { float deltaX = (x2 - x1); float deltaY = (y2 - y1); return atan2(deltaY, deltaX); } void InitPlayer(Player &player) { player.x = 10; player.y = WIDTH / 2; player.dirX = 0; player.dirY = 0; player.boundx = 10; player.boundy = 10; player.speed = 5; player.live = true; player.lives = 200; } void MovePlayerUp(Player &player) { //player.animationRow = 0; if(player.live) { player.y -= player.speed; if(player.y < 0) player.y = 0; } player.dirY = 1; } void MovePlayerDown(Player &player) { if(player.live) { //player.animationRow = 2; player.y += player.speed; if(player.y > HEIGHT) player.y = HEIGHT; } player.dirY = -1; } void MovePlayerLeft(Player &player) { if(player.live) { //player.curFrame = 2; player.x -= player.speed; if(player.x < 0) player.x = 0; } player.dirX = -1; } void MovePlayerRight(Player &player) { if(player.live) { //player.curFrame = 1; player.x += player.speed; if(player.x > WIDTH) player.x = WIDTH; } player.dirX = 1; } /*void ResetPlayerDir(Player &player) { player.dirX = 0; player.dirY = 0; }*/ void IsPlayerDead(Player &player, Enemy bad[]) { for(int i = 0; i < NUM_BADDIES; i++) { if(bad[i].x - bad[i].boundx < player.x + player.boundx && bad[i].x + bad[i].boundx > player.x - player.boundx && bad[i].y - bad[i].boundy < player.y + player.boundy && bad[i].y + bad[i].boundy > player.y - player.boundy) { if(player.live) { al_draw_textf(font18, al_map_rgb(255,0,0), (WIDTH / 2) - 200, 10, 0, "BITTEN"); //player.lives--; } if(player.lives <= 0) player.live = false; } } } void InitEnemy(Enemy bad[]) { for(int i = 0; i < NUM_BADDIES; i++) { bad[i].boundx = 10; bad[i].boundy = 10; bad[i].live = false; } } void StartEnemy(Enemy bad[]) { for(int i = 0; i < NUM_BADDIES; i++) { if(!bad[i].live) { if(rand() % 10 == 0) { if(rand() % 200 == 0) { bad[i].live = true; bad[i].x = 30 + rand() % WIDTH; bad[i].y = 0; bad[i].speed = rand() % 3 + 2; num = 0; } if(rand() % 4 == 1) { bad[i].live = true; bad[i].x = 0; bad[i].y = 30 + rand() % HEIGHT; bad[i].speed = rand() % 3 + 2; num = 1; } if(rand() % 4 == 2) { bad[i].live = true; bad[i].x = WIDTH; bad[i].y = 30 + rand() % HEIGHT; bad[i].speed = rand() % 3 + 2; num = 2; } if(rand() % 4 == 3) { num = 3; bad[i].live = true; bad[i].x = 30 + rand() % WIDTH; bad[i].y = HEIGHT; bad[i].speed = rand() % 3 + 2; } } } } } void InitBullet(Bullet bullets[], int size) { for(int i = 0; i < size; i++) { bullets[i].speed = 10; bullets[i].dirX = 0; bullets[i].dirY = 0; bullets[i].live = false; } } void FireBullet(Bullet bullets[], Player &player, int size) { for(int i = 0; i < size; i++) { if(!bullets[i].live) { cout << "firing Bullet!" << endl; if(player.dirX == 1) { bullets[i].live = true; bullets[i].dirX = 1; bullets[i].x = player.x + 2; bullets[i].y = player.y; break; } else if(player.dirX == -1) { bullets[i].dirX = -1; bullets[i].x = player.x - 2; bullets[i].y = player.y; bullets[i].live = true; break; } else if(player.dirY == 1) { bullets[i].dirY = 1; bullets[i].y = player.y - 2; bullets[i].x = player.x; bullets[i].live = true; break; } else if(player.dirY == -1) { bullets[i].live = true; bullets[i].dirY = -1; bullets[i].y = player.y + 2; bullets[i].x = player.x; break; } } } } void DrawBullet(Bullet bullets[], int size) { for(int i = 0; i < size; i++) { if(bullets[i].live) al_draw_filled_rectangle(bullets[i].x, bullets[i].y + 3, bullets[i].x - 4, (bullets[i].y - 4) + 3, al_map_rgb(255,0,255)); } } void UpdateBullet(Bullet bullets[], int size) { for(int i = 0; i < size; i++) { if(bullets[i].live == true) { if(bullets[i].dirX == 1) { bullets[i].x += bullets[i].speed; if(bullets[i].x > WIDTH) { bullets[i].live = false; cout << "bullet went out of bounds!" << endl; bullets[i].dirX = 0; bullets[i].dirY = 0; } } else if(bullets[i].dirX == -1) { bullets[i].x -= bullets[i].speed; if(bullets[i].x < 0) { bullets[i].live = false; cout << "bullet went out of bounds!" << endl; bullets[i].dirX = 0; bullets[i].dirY = 0; } } else if(bullets[i].dirY == -1) { bullets[i].y += bullets[i].speed; if(bullets[i].y > HEIGHT) { bullets[i].live = false; cout << "bullet went out of bounds!" << endl; bullets[i].dirX = 0; bullets[i].dirY = 0; } } else if(bullets[i].dirY == 1) { bullets[i].y -= bullets[i].speed; if(bullets[i].y < 0) { bullets[i].live = false; bullets[i].dirX = 0; bullets[i].dirY = 0; cout << "bullet went out of bounds!" << endl; } } /* if(bullets[i].dirX == -1 && bullets[i].dirY == -1) { bullets[i].y -= (bullets[i].speed / 2); bullets[i].x -= (bullets[i].speed / 2); if(bullets[i].y < 0) { bullets[i].live = false; bullets[i].dirX = 0; bullets[i].dirY = 0; cout << "bullet went out of bounds!" << endl; } if(bullets[i].x < 0) { bullets[i].live = false; bullets[i].dirX = 0; bullets[i].dirY = 0; cout << "bullet went out of bounds!" << endl; } }*/ } } } void CollideBullet(Bullet bullets[], int size, Enemy bad[]) { for(int i = 0; i < size; i++) { if(bullets[i].live) { for(int j = 0; j < NUM_BADDIES; j++) { if(bad[j].live) { if(bullets[i].x > bad[j].x - bad[j].boundx && bullets[i].x < bad[j].x + bad[j].boundx && bullets[i].y > bad[j].y - bad[j].boundy && bullets[i].y < bad[j].y + bad[j].boundy) { bullets[i].live = false; bad[j].live = false; cout <<"you got him!" << endl; break; } } } } } } // --Header file classes.h-- struct Player { int x; int y; int dirX; int dirY; int boundx; int boundy; int speed; int lives; bool live; }; struct Enemy { int x; int y; int speed; int boundx; int boundy; bool live; }; struct Bullet { int x; int y; int dirX; int dirY; int speed; bool live; }; [/CODE] thank you. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]