Ultraboy94

Dumb shit I did today

Jan 28th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.64 KB | None | 0 0
  1. #include <allegro5\allegro.h>
  2. #include <allegro5\allegro_primitives.h>
  3. #include <allegro5\allegro_font.h>
  4. #include <allegro5\allegro_ttf.h>
  5. #include <allegro5\allegro_image.h>
  6. #include "objects.h"
  7.  
  8. //GLOBALS==============================
  9. const int WIDTH = 800;
  10. const int HEIGHT = 400;
  11. const int NUM_BULLETS = 999;
  12. const int NUM_COMETS = 10;
  13. enum KEYS{UP, DOWN, LEFT, RIGHT, SPACE};
  14. bool keys[5] = {false, false, false, false, false};
  15.  
  16. //prototypes
  17. void InitShip(SpaceShip &ship, ALLEGRO_BITMAP *image);
  18. void ResetShipAnimation(SpaceShip &ship, int position);
  19. void DrawShip(SpaceShip &ship);
  20. void MoveShipUp(SpaceShip &ship);
  21. void MoveShipDown(SpaceShip &ship);
  22. void MoveShipLeft(SpaceShip &ship);
  23. void MoveShipRight(SpaceShip &ship);
  24.  
  25. void InitBullet(Bullet bullet[], int size);
  26. void DrawBullet(Bullet bullet[], int size);
  27. void FireBullet(Bullet bullet[], int size, SpaceShip &ship);
  28. void UpdateBullet(Bullet bullet[], int size);
  29. void CollideBullet(Bullet bullet[], int bsize, Comet comets[], int csize, SpaceShip &ship);
  30.  
  31. void InitComet(Comet comets[], int size);
  32. void DrawComet(Comet comets[], int size);
  33. void StartComet(Comet comets[], int size);
  34. void UpdateComet(Comet comets[], int size);
  35. void CollideComet(Comet comets[], int csize, SpaceShip &ship);
  36.  
  37. int main(void)
  38. {
  39.     //primitive variable
  40.     bool done = false;
  41.     bool redraw = true;
  42.     const int FPS = 60;
  43.     bool isGameOver = false;
  44.  
  45.     //object variables
  46.     SpaceShip ship;
  47.     Bullet bullets[NUM_BULLETS];
  48.     Comet comets[NUM_COMETS];
  49.  
  50.     //Allegro variables
  51.     ALLEGRO_DISPLAY *display = NULL;
  52.     ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  53.     ALLEGRO_TIMER *timer = NULL;
  54.     ALLEGRO_FONT *font18 = NULL;
  55.     ALLEGRO_BITMAP *shipImage;
  56.  
  57.     //Initialization Functions
  58.     if(!al_init())                                      //initialize Allegro
  59.         return -1;
  60.  
  61.     display = al_create_display(WIDTH, HEIGHT);         //create our display object
  62.  
  63.     if(!display)                                        //test display object
  64.         return -1;
  65.  
  66.     al_init_primitives_addon();
  67.     al_install_keyboard();
  68.     al_init_font_addon();
  69.     al_init_ttf_addon();
  70.  
  71.     al_init_image_addon();
  72.  
  73.     event_queue = al_create_event_queue();
  74.     timer = al_create_timer(1.0 / FPS);
  75.  
  76.     shipImage = al_load_bitmap("Spaceship_sprites_by_arboris.png");
  77.     al_convert_mask_to_alpha(shipImage, al_map_rgb(255,0,255));
  78.  
  79.     srand(time(NULL));
  80.     InitShip(ship, shipImage);
  81.     InitBullet(bullets, NUM_BULLETS);
  82.     InitComet(comets, NUM_COMETS);
  83.    
  84.     font18 = al_load_font("arial.ttf", 18, 0);
  85.  
  86.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  87.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  88.     al_register_event_source(event_queue, al_get_display_event_source(display));
  89.  
  90.     al_start_timer(timer);
  91.     while(!done)
  92.     {
  93.         ALLEGRO_EVENT ev;
  94.         al_wait_for_event(event_queue, &ev);
  95.  
  96.         if(ev.type == ALLEGRO_EVENT_TIMER)
  97.         {
  98.             redraw = true;
  99.             if(keys[UP])
  100.                 MoveShipUp(ship);
  101.             else if(keys[DOWN])
  102.                 MoveShipDown(ship);
  103.             else
  104.                 ResetShipAnimation(ship, 1);
  105.  
  106.             if(keys[LEFT])
  107.                 MoveShipLeft(ship);
  108.             else if(keys[RIGHT])
  109.                 MoveShipRight(ship);
  110.             else
  111.                 ResetShipAnimation(ship, 2);
  112.  
  113.             if(keys[SPACE])
  114.                 FireBullet(bullets, NUM_BULLETS, ship);
  115.  
  116.             if(!isGameOver)
  117.             {
  118.                 UpdateBullet(bullets, NUM_BULLETS);
  119.                 StartComet(comets, NUM_COMETS);
  120.                 UpdateComet(comets, NUM_COMETS);
  121.                 CollideBullet(bullets, NUM_BULLETS, comets, NUM_COMETS, ship);
  122.                 CollideComet(comets, NUM_COMETS, ship);
  123.  
  124.                 if(ship.lives <= 0)
  125.                     isGameOver = true;
  126.             }
  127.         }
  128.         else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  129.         {
  130.             done = true;
  131.         }
  132.         else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
  133.         {
  134.             switch(ev.keyboard.keycode)
  135.             {
  136.             case ALLEGRO_KEY_ESCAPE:
  137.                 done = true;
  138.                 break;
  139.             case ALLEGRO_KEY_UP:
  140.                 keys[UP] = true;
  141.                 break;
  142.             case ALLEGRO_KEY_DOWN:
  143.                 keys[DOWN] = true;
  144.                 break;
  145.             case ALLEGRO_KEY_LEFT:
  146.                 keys[LEFT] = true;
  147.                 break;
  148.             case ALLEGRO_KEY_RIGHT:
  149.                 keys[RIGHT] = true;
  150.                 break;
  151.             case ALLEGRO_KEY_SPACE:
  152.                 keys[SPACE] = true;
  153.                 //FireBullet(bullets, NUM_BULLETS, ship);
  154.                 break;
  155.             }
  156.         }
  157.         else if(ev.type == ALLEGRO_EVENT_KEY_UP)
  158.         {
  159.             switch(ev.keyboard.keycode)
  160.             {
  161.             case ALLEGRO_KEY_ESCAPE:
  162.                 done = true;
  163.                 break;
  164.             case ALLEGRO_KEY_UP:
  165.                 keys[UP] = false;
  166.                 break;
  167.             case ALLEGRO_KEY_DOWN:
  168.                 keys[DOWN] = false;
  169.                 break;
  170.             case ALLEGRO_KEY_LEFT:
  171.                 keys[LEFT] = false;
  172.                 break;
  173.             case ALLEGRO_KEY_RIGHT:
  174.                 keys[RIGHT] = false;
  175.                 break;
  176.             case ALLEGRO_KEY_SPACE:
  177.                 keys[SPACE] = false;
  178.                 break;
  179.             }
  180.         }
  181.  
  182.         //if(ev.type == ALLEGRO_EVENT_TIMER * 2)
  183.         //{
  184.         //  if(keys[SPACE])
  185.                 //FireBullet(bullets, NUM_BULLETS, ship);
  186.  
  187.         //}
  188.  
  189.         if(redraw && al_is_event_queue_empty(event_queue))
  190.         {
  191.             redraw = false;
  192.  
  193.             if(!isGameOver)
  194.             {
  195.                 DrawShip(ship);
  196.                 DrawBullet(bullets, NUM_BULLETS);
  197.                 DrawComet(comets, NUM_COMETS);
  198.                 al_draw_textf(font18, al_map_rgb(0,255,0), 5, 5, 0, "Lives: %i", ship.lives);
  199.                 al_draw_textf(font18, al_map_rgb(255,0,0), HEIGHT - 5, 5, 0,"Score: %i", ship.score);
  200.             }
  201.             else
  202.             {
  203.                 al_draw_textf(font18, al_map_rgb(0,255,0), WIDTH /  2, HEIGHT / 2, ALLEGRO_ALIGN_CENTER, "Game over. Final score, %i.", ship.score);
  204.             }
  205.            
  206.  
  207.        
  208.             al_flip_display();
  209.             al_clear_to_color(al_map_rgb(0,0,0));
  210.         }
  211.     }
  212.  
  213.     al_destroy_event_queue(event_queue);
  214.     al_destroy_timer(timer);
  215.     al_destroy_display(display);                        //destroy our display object
  216.     al_destroy_bitmap(shipImage);
  217.     al_destroy_font(font18);
  218.  
  219.     return 0;
  220. }
  221.  
  222. void InitShip(SpaceShip &ship, ALLEGRO_BITMAP *image)
  223. {
  224.     ship.x = 20;
  225.     ship.y = HEIGHT / 2;
  226.     ship.ID = PLAYER;
  227.     ship.lives = 3;
  228.     ship.speed = 6;
  229.     ship.boundx = 10;
  230.     ship.boundy = 12;
  231.     ship.score = 0;
  232.  
  233.     ship.maxFrame = 3;
  234.     ship.curFrame = 0;
  235.     ship.frameCount = 0;
  236.     ship.frameDelay = 50;
  237.     ship.frameWidth = 46;
  238.     ship.frameHeight = 41;
  239.     ship.animationColumns = 3;
  240.     ship.animationDirection = 1;
  241.  
  242.     ship.animationRow = 1;
  243.  
  244.     ship.image = image;
  245.  
  246. }
  247.  
  248. void ResetShipAnimation(SpaceShip &ship, int position)
  249. {
  250.     if(position == 1)
  251.         ship.animationRow = 1;
  252.     else
  253.         ship.curFrame = 0;
  254. }
  255.  
  256. void DrawShip(SpaceShip &ship)
  257. {
  258.     int fx = (ship.curFrame % ship.animationColumns) * ship.frameWidth;
  259.     int fy = ship.animationRow * ship.frameHeight;
  260.  
  261.     al_draw_bitmap_region(ship.image, fx, fy, ship.frameWidth, ship.frameHeight, ship.x - ship.frameWidth / 2,
  262.                             ship.y - ship.frameHeight / 2, 0);
  263. }
  264. void MoveShipUp(SpaceShip &ship)
  265. {
  266.     ship.animationRow = 0;
  267.     ship.y -= ship.speed;
  268.     if(ship.y < 0)
  269.         ship.y = 0;
  270. }
  271. void MoveShipDown(SpaceShip &ship)
  272. {
  273.     ship.animationRow = 2;
  274.     ship.y += ship.speed;
  275.     if(ship.y > HEIGHT)
  276.         ship.y = HEIGHT;
  277. }
  278. void MoveShipLeft(SpaceShip &ship)
  279. {
  280.     ship.curFrame = 2;
  281.     ship.x -= ship.speed;
  282.     if(ship.x < 0)
  283.         ship.x = 0;
  284. }
  285. void MoveShipRight(SpaceShip &ship)
  286. {
  287.     ship.curFrame = 1;
  288.     ship.x += ship.speed;
  289.     if(ship.x > WIDTH / 2)
  290.         ship.x = WIDTH / 2;
  291. }
  292.  
  293. void InitBullet(Bullet bullet[], int size)
  294. {
  295.     for(int i = 0; i < size; i++)
  296.     {
  297.         bullet[i].ID = BULLET;
  298.         bullet[i].speed = 10;
  299.         bullet[i].live = false;
  300.     }
  301. }
  302. void DrawBullet(Bullet bullet[], int size)
  303. {
  304.     for( int i = 0; i < size; i++)
  305.     {
  306.         if(bullet[i].live)
  307.             al_draw_filled_circle(bullet[i].x, bullet[i].y, 2, al_map_rgb(0, 255, 255));
  308.     }
  309. }
  310. void FireBullet(Bullet bullet[], int size, SpaceShip &ship)
  311. {
  312.     for( int i = 0; i < size; i++)
  313.     {
  314.         if(!bullet[i].live)
  315.         {
  316.             bullet[i].x = ship.x + 17;
  317.             bullet[i].y = ship.y;
  318.             bullet[i].live = true;
  319.             break;
  320.         }
  321.     }
  322. }
  323. void UpdateBullet(Bullet bullet[], int size)
  324. {
  325.     for(int i = 0; i < size; i++)
  326.     {
  327.         if(bullet[i].live)
  328.         {
  329.             bullet[i].x += bullet[i].speed;
  330.             if(bullet[i].x > WIDTH)
  331.                 bullet[i].live = false;
  332.         }
  333.     }
  334. }
  335.  
  336. void CollideBullet(Bullet bullet[], int bsize, Comet comets[], int csize, SpaceShip &ship)
  337. {
  338.     for (int i = 0; i < bsize; i++)
  339.     {
  340.         if(bullet[i].live)
  341.         {
  342.             for(int j = 0; j < csize; j++)
  343.             {
  344.                 if(comets[j].live)
  345.                 {
  346.                     if(bullet[i].x > (comets[j].x - comets[j].boundx) &&
  347.                         bullet[i].x < (comets[j].x + comets[j].boundx) &&
  348.                         bullet[i].y > (comets[j].y - comets[j].boundy) &&
  349.                         bullet[i].y < (comets[j].y + comets[j].boundy))
  350.                     {
  351.                         bullet[i].live = false;
  352.                         comets[j].live = false;
  353.                         ship.score = ship.score + 10000;
  354.                     }
  355.                 }
  356.             }
  357.         }
  358.     }
  359. }
  360.  
  361. void InitComet(Comet comets[], int size)
  362. {
  363.     for(int i = 0; i < size; i++)
  364.     {
  365.         comets[i].ID = ENEMY;
  366.         comets[i].live = false;
  367.         comets[i].speed = 5;
  368.         comets[i].boundx = 18;
  369.         comets[i].boundy = 18;
  370.     }
  371. }
  372. void DrawComet(Comet comets[], int size)
  373. {
  374.     for(int i = 0; i < size; i++)
  375.     {
  376.         if(comets[i].live)
  377.         {
  378.             al_draw_filled_circle(comets[i].x, comets[i].y, 20, al_map_rgb(255, 0, 0));
  379.         }
  380.     }
  381. }
  382. void StartComet(Comet comets[], int size)
  383. {
  384.     for(int i = 0; i < size; i++)
  385.     {
  386.         if(!comets[i].live)
  387.         {
  388.             if(rand() % 500 == 0)
  389.             {
  390.                 comets[i].live = true;
  391.                 comets[i].x = WIDTH;
  392.                 comets[i].y = 30 + rand() % (HEIGHT - 60);
  393.  
  394.                 break;
  395.             }
  396.         }
  397.     }
  398. }
  399. void UpdateComet(Comet comets[], int size)
  400. {
  401.     for(int i = 0; i < size; i++)
  402.     {
  403.         if(comets[i].live)
  404.         {
  405.             comets[i].x -= comets[i].speed;
  406.  
  407.             if(comets[i].x < 0)
  408.                 comets[i].live = false;
  409.         }
  410.     }
  411. }
  412.  
  413. void CollideComet(Comet comets[], int csize, SpaceShip &ship)
  414. {
  415.     for(int i = 0; i < csize; i++)
  416.     {
  417.         if(comets[i].live)
  418.         {
  419.             if(comets[i].x - comets[i].boundx < ship.x + ship.boundx &&
  420.                 comets[i].x + comets[i].boundx > ship.x - ship.boundx &&
  421.                 comets[i].y - comets[i].boundy < ship.y + ship.boundy &&
  422.                 comets[i].y + comets[i].boundy > ship.y - ship.boundy)
  423.             {
  424.                 ship.lives--;
  425.                 comets[i].live = false;
  426.             }
  427.             else if(comets[i].x < 0)
  428.             {
  429.                 comets[i].live = false;
  430.                 ship.lives--;
  431.             }
  432.         }
  433.     }
  434. }
Advertisement
Add Comment
Please, Sign In to add comment