Advertisement
Guest User

HF Help

a guest
Aug 18th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 29.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <Allegro5/allegro.h>
  3. #include <Allegro5/allegro_native_dialog.h>
  4. #include <Allegro5/allegro_font.h>
  5. #include <Allegro5/allegro_ttf.h>
  6. #include <Allegro5/allegro_primitives.h>
  7. #include <Allegro5/allegro_image.h>
  8. #include <Allegro5/allegro_audio.h>
  9. #include <Allegro5/allegro_acodec.h>
  10.  
  11. #include "globals.h"
  12. #include "buttons.h"
  13. #include "screenState.h"
  14. #include "nebulaBig.h"
  15. #include "tutorialShoot.h"
  16. #include "player.h"
  17. #include "bullets.h"
  18. #include "bulletEnemy.h"
  19. #include "cometBig.h"
  20. #include "cometSmall.h"
  21. #include "healthup.h"
  22. #include "enemyShipNormal.h"
  23. #include "backgroundAnim.h"
  24.  
  25. int main(int argc, char **argv) {
  26.  
  27.     /*SHELL VARIABLES*/
  28.     unsigned short addonCheck = 0;
  29.     unsigned long animCounter = 0;
  30.  
  31.     bool running = false;
  32.     bool rendering = false;
  33.  
  34.     int gameFPS = 0;
  35.     int frames = 0;
  36.     float gameTime = 0;
  37.     int seconds = 0;
  38.     int minutes = 0;
  39.     int framesElapsed = 0;
  40.  
  41.     NebulaBig nebulaBigC;
  42.     TutorialShoot tutorialShootC;
  43.  
  44.     Buttons *bPlay = new Buttons();
  45.     Buttons *bHelp = new Buttons();
  46.     Buttons *bOptn = new Buttons();
  47.     Buttons *bBack = new Buttons();
  48.     Buttons *bExit = new Buttons();
  49.     Buttons *bMenu = new Buttons();
  50.     Buttons *bExit2 = new Buttons();
  51.     Buttons *bVolumeP = new Buttons();
  52.     Buttons *bVolumeM = new Buttons();
  53.     Buttons *bDifficultyP = new Buttons();
  54.     Buttons *bDifficultyM = new Buttons();
  55.  
  56.     Bullet bullets[20];
  57.     //BulletEnemy bulletEnemy[20];
  58.  
  59.     CometBig cometsBig[20];
  60.     CometSmall cometsSmall[20];
  61.     HealthUp healthUp[1];
  62.  
  63.     EnemyShip enemyShip[1];
  64.  
  65.     Player ship;
  66.  
  67.     GameBackground bgStars;
  68.     GameBackground bgNebulas;
  69.  
  70.  
  71.     /*ALLEGRO VARIABLES*/
  72.     al_init();
  73.     ALLEGRO_DISPLAY *display = 0;
  74.     ALLEGRO_EVENT_QUEUE *eventQueue = 0;
  75.     ALLEGRO_TIMER *timer = 0;
  76.     ALLEGRO_BITMAP *mouseBMP;
  77.     ALLEGRO_FONT *mainFont, *smallFont, *titleFont, *bigFont;
  78.     ALLEGRO_BITMAP *player, *playerDamaged, *playerLeft, *playerRight, *playerShield, *enemyNormal;
  79.     ALLEGRO_BITMAP *cometBigBMP, *cometSmallBMP, *livesIconRotated;
  80.     ALLEGRO_BITMAP *bulletPlayer, *bulletEnemyBMP;
  81.     ALLEGRO_BITMAP *nebulaBig, *nebulaSmall, *nebulaVerySmall;
  82.     ALLEGRO_BITMAP *gameScreenFast, *gameScreenNebulas, *livesIcon;
  83.     ALLEGRO_BITMAP *tutorialShoot;
  84.     ALLEGRO_SAMPLE *introSound;
  85.     ALLEGRO_SAMPLE *bulletSound, *explosionSound, *healthdownSound, *healthupSound;
  86.  
  87.     /*CREATE DISPLAY*/
  88.     al_set_new_display_flags(ALLEGRO_FULLSCREEN);
  89.     display = al_create_display(screenW, screenH);
  90.     if (!display) {
  91.         al_show_native_message_box(0, "ERROR", "Failed to initialize the display!", "Press OK to terminate.", 0, ALLEGRO_MESSAGEBOX_ERROR);
  92.         return -1;
  93.     }
  94.  
  95.     al_hide_mouse_cursor(display);
  96.  
  97.     /*INSTALL ADDONS*/
  98.     al_init_font_addon(); addonCheck++;
  99.     al_init_ttf_addon(); addonCheck++;
  100.     al_init_primitives_addon(); addonCheck++;
  101.     al_init_image_addon(); addonCheck++;
  102.     al_install_keyboard(); addonCheck++;
  103.     al_install_mouse(); addonCheck++;
  104.     al_install_audio(); addonCheck++;
  105.     al_init_acodec_addon(); addonCheck++;
  106.     if (addonCheck != 8) {
  107.         al_show_native_message_box(0, "ERROR", "Failed to load Allegro addons!", "Press OK to terminate.", 0, ALLEGRO_MESSAGEBOX_ERROR);
  108.         return -1;
  109.     }
  110.  
  111.     /*FILE LOADING*/
  112.  
  113.     //LOADING SCREEN
  114.     bigFont = al_load_font("HOOG0555.ttf", 55, 0);
  115.     al_clear_to_color(al_map_rgb(0, 0, 0));
  116.     al_draw_text(bigFont, al_map_rgb(255, 255, 255), screenW / 2.5, screenH / 2, 0, "LOADING.");
  117.     al_flip_display();
  118.  
  119.     //LOADING IMAGES
  120.     titleFont = al_load_font("HOOG0555.ttf", 125, 0);
  121.     mainFont = al_load_font("HOOG0555.ttf", 35, 0);
  122.     smallFont = al_load_font("HOOG0555.ttf", 15, 0);
  123.  
  124.     ALLEGRO_PATH *imagesP = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
  125.     al_append_path_component(imagesP, "images");
  126.     al_change_directory(al_path_cstr(imagesP, '/'));
  127.     al_destroy_path(imagesP);
  128.  
  129.     mouseBMP = al_load_bitmap("mouse.png");
  130.  
  131.     player = al_load_bitmap("player.png");
  132.     playerDamaged = al_load_bitmap("playerDamaged.png");
  133.     playerLeft = al_load_bitmap("playerLeft.png");
  134.     playerRight = al_load_bitmap("playerRight.png");
  135.     playerShield = al_load_bitmap("shield.png");
  136.  
  137.     enemyNormal = al_load_bitmap("enemyShip.png");
  138.  
  139.     cometBigBMP = al_load_bitmap("meteorBig.png");
  140.     cometSmallBMP = al_load_bitmap("meteorSmall.png");
  141.  
  142.     bulletPlayer = al_load_bitmap("laserGreen.png");
  143.     bulletEnemyBMP = al_load_bitmap("laserRed.png");
  144.  
  145.     nebulaBig = al_load_bitmap("nebulaBig.png");
  146.     nebulaSmall = al_load_bitmap("nebulaSmall.png");
  147.     nebulaVerySmall = al_load_bitmap("nebulaVerySmall.png");
  148.  
  149.     gameScreenFast = al_load_bitmap("movingBGlines.png");
  150.     al_convert_mask_to_alpha(gameScreenFast, al_map_rgb(35, 10, 55));
  151.     gameScreenNebulas = al_load_bitmap("movingBGnebulas.png");
  152.     al_convert_mask_to_alpha(gameScreenNebulas, al_map_rgb(35, 10, 55));
  153.     livesIcon = al_load_bitmap("life.png");
  154.     livesIconRotated = al_load_bitmap("life180.png");
  155.  
  156.     tutorialShoot = al_load_bitmap("tutorialShoot.png");
  157.  
  158.     al_draw_text(bigFont, al_map_rgb(255, 255, 255), screenW / 2.5, screenH / 2, 0, "LOADING..");
  159.     al_flip_display();
  160.  
  161.     //LOADING SOUNDS
  162.     al_reserve_samples(5);
  163.  
  164.     ALLEGRO_PATH *soundsP = al_get_standard_path(ALLEGRO_RESOURCES_PATH);
  165.     al_append_path_component(soundsP, "sounds");
  166.     al_change_directory(al_path_cstr(soundsP, '/'));
  167.     al_destroy_path(soundsP);
  168.  
  169.     introSound = al_load_sample("introSound.wav");
  170.  
  171.     bulletSound = al_load_sample("bulletSound.wav");
  172.     explosionSound = al_load_sample("explosionSound.wav");
  173.     healthdownSound = al_load_sample("healthdownSound.wav");
  174.     healthupSound = al_load_sample("healthupSound.wav");
  175.  
  176.     al_draw_text(bigFont, al_map_rgb(255, 255, 255), screenW / 2.5, screenH / 2, 0, "LOADING...");
  177.     al_flip_display();
  178.  
  179. start:
  180.     bPlay->setHover(false);
  181.     objectCount = 0;
  182.     shieldTimer = 5;
  183.     enemyFireTimer = 0;
  184.  
  185.     /*FADE-IN COUNTERS*/
  186.     counter255r = 35;
  187.     counter255g = 10;
  188.     counter255b = 55;
  189.     counter185r = 35;
  190.     counter185g = 10;
  191.     counter185b = 55;
  192.     counterHelp185r = 35;
  193.     counterHelp185g = 10;
  194.     counterHelp185b = 55;
  195.     counterHelp255r = 35;
  196.     counterHelp255g = 10;
  197.     counterHelp255b = 55;
  198.  
  199.     /*GAME OBJECT INITIALIZATION*/
  200.     nebulaBigC.init(nebulaBig);
  201.     tutorialShootC.init(tutorialShoot);
  202.  
  203.     bPlay->init(screenW / 2, screenH / 2 + 100, 65, 18, bigFont);
  204.     bHelp->init(screenW / 2, screenH / 2 + 140, 65, 18, bigFont);
  205.     bOptn->init(screenW / 2, screenH / 2 + 180, 65, 18, bigFont);
  206.     bBack->init(screenW / 2, screenH - 80, 65, 18, bigFont);
  207.     bExit->init(screenW / 2, screenH / 2 + 220, 65, 18, bigFont);
  208.     bMenu->init(screenW / 2, screenH / 2 + 40, 65, 18, bigFont);
  209.     bExit2->init(screenW / 2, screenH / 2 + 80, 65, 18, bigFont);
  210.     bVolumeP->init(screenW / 2 + 90, screenH / 3, 18, 18, bigFont);
  211.     bVolumeM->init(screenW / 2 - 90, screenH / 3, 18, 18, bigFont);
  212.     bDifficultyP->init(screenW / 2 + 100, screenH / 3 + 175, 18, 18, bigFont);
  213.     bDifficultyM->init(screenW / 2 - 100, screenH / 3 + 175, 18, 18, bigFont);
  214.  
  215.     initBullet(bullets, bulletPlayer, bulletSound, explosionSound);
  216.     bulletEnemy->init(bulletEnemy, bulletEnemyBMP, bulletSound, explosionSound, healthdownSound);
  217.  
  218.     initCometBig(cometsBig, cometBigBMP, healthdownSound);
  219.     initCometSmall(cometsSmall, cometSmallBMP, healthdownSound);
  220.     healthUp->init(healthUp, livesIconRotated, healthupSound);
  221.  
  222.     enemyShip->init(enemyShip, enemyNormal, explosionSound);
  223.  
  224.     ship.init(player, playerLeft, playerRight, playerDamaged, playerShield, livesIcon);
  225.  
  226.     bgStars.init(gameScreenFast, 6, 1);
  227.     bgNebulas.init(gameScreenNebulas, 2, 2);
  228.  
  229.     /*EQ, TIMER AND REGISTER SOURCES*/
  230.     eventQueue = al_create_event_queue();
  231.     timer = al_create_timer(1.0 / 120);
  232.  
  233.     al_register_event_source(eventQueue, al_get_timer_event_source(timer));
  234.     al_register_event_source(eventQueue, al_get_display_event_source(display));
  235.     al_register_event_source(eventQueue, al_get_mouse_event_source());
  236.     al_register_event_source(eventQueue, al_get_keyboard_event_source());
  237.  
  238.     /*FINAL PREPERATIONS, START TIMER*/
  239.     running = true;
  240.     ScreenStateC *screenStateC = new ScreenStateC();
  241.     screenStateC->setToState(1);
  242.  
  243.     srand(time(NULL));
  244.  
  245.     al_start_timer(timer);
  246.     gameTime = al_current_time();
  247.     al_play_sample(introSound, volume, ALLEGRO_AUDIO_PAN_NONE, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
  248.  
  249.     while (running) {
  250.         if (screenState[TITLESCREEN] && !screenState[GAMESCREEN]) {
  251.             ALLEGRO_EVENT titleEvent;
  252.             al_wait_for_event(eventQueue, &titleEvent);
  253.  
  254.             if (titleEvent.type == ALLEGRO_EVENT_MOUSE_AXES) {
  255.                 mouseX = titleEvent.mouse.x;
  256.                 mouseY = titleEvent.mouse.y;
  257.             }
  258.  
  259.             if (titleEvent.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
  260.                 if (!screenState[HELPSCREEN] && !screenState[OPTIONSCREEN] && !screenState[GAMESCREEN]) {
  261.                     if (bPlay->getHover()) {
  262.                         if (titleEvent.mouse.button & 1) {
  263.                             screenStateC->setToState(2);
  264.                         }
  265.                     }
  266.                     if (bHelp->getHover()) {
  267.                         if (titleEvent.mouse.button & 1) {
  268.                             screenStateC->setToState(3);
  269.                         }
  270.                     }
  271.                     if (bOptn->getHover()) {
  272.                         if (titleEvent.mouse.button & 1) {
  273.                             screenStateC->setToState(4);
  274.                         }
  275.                     }
  276.                     if (bExit->getHover()) {
  277.                         if (titleEvent.mouse.button & 1) {
  278.                             screenState[TITLESCREEN] = false;
  279.                             running = false;
  280.                         }
  281.                     }
  282.                 }
  283.                 else if (screenState[HELPSCREEN]) {
  284.                     if (bBack->getHover() & 1) {
  285.                         screenStateC->setToState(1);
  286.                     }
  287.                 }
  288.                 else if (screenState[OPTIONSCREEN]) {
  289.                     if (bBack->getHover() & 1) {
  290.                         screenStateC->setToState(1);
  291.                     }
  292.                     if (bVolumeP->getHover() & 1) {
  293.                         if (volume == 0.0) {
  294.                             volume = 0.2;
  295.                         }
  296.                         else if (volume == 0.2) {
  297.                             volume = 0.4;
  298.                         }
  299.                         else if (volume == 0.4) {
  300.                             volume = 0.6;
  301.                         }
  302.                         else if (volume == 0.6) {
  303.                             volume = 0.8;
  304.                         }
  305.                         else if (volume == 0.8) {
  306.                             volume = 1.0;
  307.                         }
  308.                     }
  309.                     if (bVolumeM->getHover() & 1) {
  310.                         if (volume == 0.2) {
  311.                             volume = 0.0;
  312.                         }
  313.                         else if (volume == 0.4) {
  314.                             volume = 0.2;
  315.                         }
  316.                         else if (volume == 0.6) {
  317.                             volume = 0.4;
  318.                         }
  319.                         else if (volume == 0.8) {
  320.                             volume = 0.6;
  321.                         }
  322.                         else if (volume == 1.0) {
  323.                             volume = 0.8;
  324.                         }
  325.                     }
  326.                     if (bDifficultyP->getHover() & 1) {
  327.                         if (difficulty[EASY]) {
  328.                             difficulty[EASY] = false;
  329.                             difficulty[NORM] = true;
  330.                             difficulty[HARD] = false;
  331.                         }
  332.                         else if (difficulty[NORM]) {
  333.                             difficulty[EASY] = false;
  334.                             difficulty[NORM] = false;
  335.                             difficulty[HARD] = true;
  336.                         }
  337.                     }
  338.                     if (bDifficultyM->getHover() & 1) {
  339.                         if (difficulty[NORM]) {
  340.                             difficulty[EASY] = true;
  341.                             difficulty[NORM] = false;
  342.                             difficulty[HARD] = false;
  343.                         }
  344.                         else if (difficulty[HARD]) {
  345.                             difficulty[EASY] = false;
  346.                             difficulty[NORM] = true;
  347.                             difficulty[HARD] = false;
  348.                         }
  349.                     }
  350.                 }
  351.             }
  352.  
  353.             if (titleEvent.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  354.                 running = false;
  355.             }
  356.  
  357.             if (titleEvent.type == ALLEGRO_EVENT_KEY_UP) {
  358.                 switch (titleEvent.keyboard.keycode) {
  359.                 case ALLEGRO_KEY_F3:
  360.                     if (keys[F3])
  361.                         keys[F3] = false;
  362.                     else if (!keys[F3])
  363.                         keys[F3] = true;
  364.                     break;
  365.                 }
  366.             }
  367.  
  368.             if (titleEvent.type == ALLEGRO_EVENT_TIMER) {
  369.                 rendering = true;
  370.  
  371.                 if (screenState[HELPSCREEN]) {
  372.                     tutorialShootC.update();
  373.  
  374.                     if (tutorialShootC.getFinal()) {
  375.                         if (counterHelp185r < 185)
  376.                             counterHelp185r++;
  377.                         if (counterHelp185g < 185)
  378.                             counterHelp185g++;
  379.                         if (counterHelp185b < 185)
  380.                             counterHelp185b++;
  381.  
  382.                         if (counterHelp255r < 255)
  383.                             counterHelp255r++;
  384.                         if (counterHelp255g < 255)
  385.                             counterHelp255g++;
  386.                         if (counterHelp255b < 255)
  387.                             counterHelp255b++;
  388.                     }
  389.                 }
  390.  
  391.                 if (nebulaBigC.getFinal()) {
  392.                     if (counter185r < 185)
  393.                         counter185r++;
  394.                     if (counter185g < 185)
  395.                         counter185g++;
  396.                     if (counter185b < 185)
  397.                         counter185b++;
  398.  
  399.                     if (counter255r < 255)
  400.                         counter255r++;
  401.                     if (counter255g < 255)
  402.                         counter255g++;
  403.                     if (counter255b < 255)
  404.                         counter255b++;
  405.  
  406.                     if (counter255g == 254)
  407.                     bPlay->hoverDetect();
  408.                     bHelp->hoverDetect();
  409.                     bOptn->hoverDetect();
  410.                     bBack->hoverDetect();
  411.                     bExit->hoverDetect();
  412.                     bVolumeP->hoverDetect();
  413.                     bVolumeM->hoverDetect();
  414.                     bDifficultyP->hoverDetect();
  415.                     bDifficultyM->hoverDetect();
  416.                 }
  417.  
  418.                 nebulaBigC.update();
  419.  
  420.                 framesElapsed++;
  421.                 seconds = framesElapsed / 120;
  422.  
  423.                 if (seconds >= 60) {
  424.                     minutes++;
  425.                     seconds = 0;
  426.                     framesElapsed = 0;
  427.                 }
  428.  
  429.                 frames++;
  430.                 if (al_current_time() - gameTime >= 1) {
  431.                     gameTime = al_current_time();
  432.                     gameFPS = frames;
  433.                     frames = 0;
  434.                 }
  435.             }
  436.  
  437.             if (rendering && al_is_event_queue_empty(eventQueue)) {
  438.                 rendering = false;
  439.  
  440.                 al_clear_to_color(al_map_rgb(35, 10, 55));
  441.  
  442.                 if (!screenState[HELPSCREEN] && !screenState[OPTIONSCREEN]) {
  443.                     nebulaBigC.render();
  444.  
  445.                     al_draw_text(titleFont, al_map_rgb(counter255r, counter255g, counter255b), screenW / 2 - 30, screenH / 7 + 20, 1, "GALAXY");
  446.                     al_draw_text(titleFont, al_map_rgb(counter255r, counter255g, counter255b), screenW / 2, screenH / 4 + 50, 1, "QUEST");
  447.                     al_draw_text(smallFont, al_map_rgb(counter255r, counter255g, counter255b), screenW / 2, screenH / 1.05, ALLEGRO_ALIGN_CENTRE, author);
  448.                     al_draw_text(smallFont, al_map_rgb(counter255r, counter255g, counter255b), screenW / 1.56, screenH / 3 + 54, 1, version);
  449.  
  450.                     /*DEBUG MODE*/
  451.                     if (keys[F3]) {
  452.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 5, ALLEGRO_ALIGN_LEFT, "FPS: %i", gameFPS);
  453.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 17, ALLEGRO_ALIGN_LEFT, "Time: %i min, %i sec", minutes, seconds);
  454.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 29, ALLEGRO_ALIGN_LEFT, "Frames: %i", framesElapsed);
  455.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 41, ALLEGRO_ALIGN_LEFT, "Mouse X: %i", mouseX);
  456.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 53, ALLEGRO_ALIGN_LEFT, "Mouse Y: %i", mouseY);
  457.                         al_draw_filled_rectangle(bPlay->getX() - bPlay->getBoundX(), bPlay->getY(), bPlay->getX() + bPlay->getBoundX(), bPlay->getY() + bPlay->getBoundY() * 2, al_map_rgb(0, 255, 20));
  458.                         al_draw_filled_rectangle(bHelp->getX() - bHelp->getBoundX(), bHelp->getY(), bHelp->getX() + bHelp->getBoundX(), bHelp->getY() + bHelp->getBoundY() * 2, al_map_rgb(0, 255, 20));
  459.                         al_draw_filled_rectangle(bOptn->getX() - bOptn->getBoundX(), bOptn->getY(), bOptn->getX() + bOptn->getBoundX(), bOptn->getY() + bOptn->getBoundY() * 2, al_map_rgb(0, 255, 20));
  460.                         al_draw_filled_rectangle(bExit->getX() - bExit->getBoundX(), bExit->getY(), bExit->getX() + bExit->getBoundX(), bExit->getY() + bExit->getBoundY() * 2, al_map_rgb(0, 255, 20));
  461.                     }
  462.  
  463.                     bPlay->render(1);
  464.                     bHelp->render(2);
  465.                     bOptn->render(3);
  466.                     bExit->render(5);
  467.                 }
  468.                 else if (screenState[HELPSCREEN] && !screenState[OPTIONSCREEN]) {
  469.                     tutorialShootC.render();
  470.  
  471.                     al_draw_text(bigFont, al_map_rgb(counterHelp255r, counterHelp255g, counterHelp255b), screenW / 5, 40, 0, "BASICS");
  472.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 6, 0, "Galaxy quest is a space invaders type game,");
  473.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 6 + 35, 0, "in which the                is to get the highest");
  474.                     al_draw_text(mainFont, al_map_rgb(counterHelp255r, counterHelp255g, counterHelp255b), screenW / 30, screenH / 6 + 35, 0, "                  objective");
  475.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 6 + 70, 0, "possible score.           are earned by destroying");
  476.                     al_draw_text(mainFont, al_map_rgb(counterHelp255r, counterHelp255g, counterHelp255b), screenW / 30 + 5, screenH / 6 + 70, 0, "                       Points");
  477.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 6 + 105, 0, "comets and enemy space ships.");
  478.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 6 + 140, 0, "Enemy commoness scales with difficulty setting.");
  479.  
  480.                     al_draw_text(bigFont, al_map_rgb(counterHelp255r, counterHelp255g, counterHelp255b), screenW / 5, screenH / 2 - 25, 0, "POWER UPS");
  481.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 2 + 70, 0, "During the game you might pick up health power");
  482.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 2 + 105, 0, "ups which will replenish your lives. The higher");
  483.                     al_draw_text(mainFont, al_map_rgb(counterHelp185r, counterHelp185g, counterHelp185b), screenW / 30, screenH / 2 + 140, 0, "the difficulty, the rarer the power ups!");
  484.  
  485.                     /*DEBUG MODE*/
  486.                     if (keys[F3]) {
  487.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 5, ALLEGRO_ALIGN_LEFT, "FPS: %i", gameFPS);
  488.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 17, ALLEGRO_ALIGN_LEFT, "Time: %i min, %i sec", minutes, seconds);
  489.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 29, ALLEGRO_ALIGN_LEFT, "Frames: %i", framesElapsed);
  490.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 41, ALLEGRO_ALIGN_LEFT, "Mouse X: %i", mouseX);
  491.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 53, ALLEGRO_ALIGN_LEFT, "Mouse Y: %i", mouseY);
  492.                         al_draw_filled_rectangle(bBack->getX() - bBack->getBoundX(), bBack->getY(), bBack->getX() + bBack->getBoundX(), bBack->getY() + bBack->getBoundY() * 2, al_map_rgb(0, 255, 20));
  493.                     }
  494.  
  495.                     bBack->render(4);
  496.                 }
  497.                 else if (!screenState[HELPSCREEN] && screenState[OPTIONSCREEN]) {
  498.                     /*DEBUG MODE*/
  499.                     if (keys[F3]) {
  500.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 5, ALLEGRO_ALIGN_LEFT, "FPS: %i", gameFPS);
  501.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 17, ALLEGRO_ALIGN_LEFT, "Time: %i min, %i sec", minutes, seconds);
  502.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 29, ALLEGRO_ALIGN_LEFT, "Frames: %i", framesElapsed);
  503.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 41, ALLEGRO_ALIGN_LEFT, "Mouse X: %i", mouseX);
  504.                         al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 53, ALLEGRO_ALIGN_LEFT, "Mouse Y: %i", mouseY);
  505.                         al_draw_filled_rectangle(bBack->getX() - bBack->getBoundX(), bBack->getY(), bBack->getX() + bBack->getBoundX(), bBack->getY() + bBack->getBoundY() * 2, al_map_rgb(0, 255, 20));
  506.                         al_draw_filled_rectangle(bVolumeP->getX() - bVolumeP->getBoundX(), bVolumeP->getY(), bVolumeP->getX() + bVolumeP->getBoundX(), bVolumeP->getY() + bVolumeP->getBoundY() * 2, al_map_rgb(0, 255, 20));
  507.                         al_draw_filled_rectangle(bVolumeM->getX() - bVolumeM->getBoundX(), bVolumeM->getY(), bVolumeM->getX() + bVolumeM->getBoundX(), bVolumeM->getY() + bVolumeM->getBoundY() * 2, al_map_rgb(0, 255, 20));
  508.                         al_draw_filled_rectangle(bDifficultyP->getX() - bDifficultyP->getBoundX(), bDifficultyP->getY(), bDifficultyP->getX() + bDifficultyP->getBoundX(), bDifficultyP->getY() + bDifficultyP->getBoundY() * 2, al_map_rgb(0, 255, 20));
  509.                         al_draw_filled_rectangle(bDifficultyM->getX() - bDifficultyM->getBoundX(), bDifficultyM->getY(), bDifficultyM->getX() + bDifficultyM->getBoundX(), bDifficultyM->getY() + bDifficultyM->getBoundY() * 2, al_map_rgb(0, 255, 20));
  510.                     }
  511.  
  512.                     al_draw_text(bigFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 - 60, ALLEGRO_ALIGN_CENTRE, "Volume");
  513.                     if (volume == 0.0)
  514.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 5, ALLEGRO_ALIGN_CENTRE, "0%");
  515.                     else if (volume == 0.2)
  516.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 5, ALLEGRO_ALIGN_CENTRE, "20%");
  517.                     else if (volume == 0.4)
  518.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 5, ALLEGRO_ALIGN_CENTRE, "40%");
  519.                     else if (volume == 0.6)
  520.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 5, ALLEGRO_ALIGN_CENTRE, "60%");
  521.                     else if (volume == 0.8)
  522.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 5, ALLEGRO_ALIGN_CENTRE, "80%");
  523.                     else if (volume == 1.0)
  524.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 5, ALLEGRO_ALIGN_CENTRE, "100%");
  525.  
  526.                     al_draw_text(bigFont, al_map_rgb(255, 255, 255), screenW / 2, (screenH / 3 + 175) - 60, ALLEGRO_ALIGN_CENTRE, "Difficulty");
  527.                     if (difficulty[EASY])
  528.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 180, ALLEGRO_ALIGN_CENTRE, "EASY");
  529.                     else if (difficulty[NORM])
  530.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 180, ALLEGRO_ALIGN_CENTRE, "NORM");
  531.                     else if (difficulty[HARD])
  532.                         al_draw_text(mainFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 3 + 180, ALLEGRO_ALIGN_CENTRE, "HARD");
  533.  
  534.                     bBack->render(4);
  535.                     bVolumeP->render(7);
  536.                     bVolumeM->render(8);
  537.                     bDifficultyP->render(9);
  538.                     bDifficultyM->render(10);
  539.                 }
  540.  
  541.                 al_draw_bitmap(mouseBMP, mouseX, mouseY, 0);
  542.  
  543.                 al_flip_display();
  544.                 al_clear_to_color(al_map_rgb(0, 0, 0));
  545.             }
  546.         }
  547.  
  548.         /**********************************************************
  549.         G A M E         L O O P
  550.         **********************************************************/
  551.  
  552.         if (screenState[GAMESCREEN] && !screenState[TITLESCREEN]) {
  553.  
  554.             ALLEGRO_EVENT gameEvent;
  555.             al_wait_for_event(eventQueue, &gameEvent);
  556.  
  557.             if (gameEvent.type == ALLEGRO_EVENT_MOUSE_AXES) {
  558.                 mouseX = gameEvent.mouse.x;
  559.                 mouseY = gameEvent.mouse.y;
  560.             }
  561.  
  562.             if (gameEvent.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) {
  563.                 if (screenState[MENUSCREEN]) {
  564.                     if (bExit2->getHover()) {
  565.                         if (gameEvent.mouse.button & 1) {
  566.                             screenState[GAMESCREEN] = false;
  567.                             running = false;
  568.                         }
  569.                     }
  570.                     if (bMenu->getHover()) {
  571.                         if (gameEvent.mouse.button & 1) {
  572.                             screenStateC->setToState(1);
  573.                             goto start;
  574.                         }
  575.                     }
  576.                 }
  577.             }
  578.  
  579.             if (gameEvent.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  580.                 running = false;
  581.             }
  582.  
  583.             if (gameEvent.type == ALLEGRO_EVENT_KEY_DOWN) {
  584.                 switch (gameEvent.keyboard.keycode) {
  585.                 case ALLEGRO_KEY_SPACE:
  586.                     keys[SPACE] = false;
  587.                     spacePressed = true;
  588.                     break;
  589.                 case ALLEGRO_KEY_W:
  590.                     keys[W] = true;
  591.                     if (!wPressed)
  592.                         wPressed = true;
  593.                     break;
  594.                 case ALLEGRO_KEY_S:
  595.                     keys[S] = true;
  596.                     if (!sPressed)
  597.                         sPressed = true;
  598.                     break;
  599.                 case ALLEGRO_KEY_A:
  600.                     keys[A] = true;
  601.                     if (!aPressed)
  602.                         aPressed = true;
  603.                     break;
  604.                 case ALLEGRO_KEY_D:
  605.                     keys[D] = true;
  606.                     if (!dPressed)
  607.                         dPressed = true;
  608.                     break;
  609.                 }
  610.             }
  611.  
  612.             if (gameEvent.type == ALLEGRO_EVENT_KEY_UP) {
  613.                 switch (gameEvent.keyboard.keycode) {
  614.                 case ALLEGRO_KEY_F3:
  615.                     if (keys[F3])
  616.                         keys[F3] = false;
  617.                     else if (!keys[F3])
  618.                         keys[F3] = true;
  619.                     break;
  620.                 case ALLEGRO_KEY_ESCAPE:
  621.                     if (screenStateC->getState() != 5 && screenState[GAMESCREEN])
  622.                         screenStateC->setToState(5);
  623.                     else if (screenStateC->getState() == 5 && screenState[GAMESCREEN])
  624.                         screenStateC->setToState(2);
  625.                     break;
  626.                 case ALLEGRO_KEY_SPACE:
  627.                     keys[SPACE] = false;
  628.                     if (ship.getLives() > 0) {
  629.                         fireBullet(bullets, ship.getX(), ship.getY() - ship.getBoundY());
  630.                     }
  631.                     break;
  632.                 case ALLEGRO_KEY_W:
  633.                     keys[W] = false;
  634.                     break;
  635.                 case ALLEGRO_KEY_S:
  636.                     keys[S] = false;
  637.                     break;
  638.                 case ALLEGRO_KEY_A:
  639.                     keys[A] = false;
  640.                     break;
  641.                 case ALLEGRO_KEY_D:
  642.                     keys[D] = false;
  643.                     break;
  644.                 }
  645.             }
  646.  
  647.             if (gameEvent.type == ALLEGRO_EVENT_TIMER) {
  648.                 rendering = true;
  649.  
  650.                 if (screenState[MENUSCREEN]) {
  651.                     bExit2->hoverDetect();
  652.                     bMenu->hoverDetect();
  653.                 }
  654.  
  655.                 if (wPressed && sPressed && aPressed && dPressed &&spacePressed) {
  656.                     if (counterGame185r > 35)
  657.                         counterGame185r--;
  658.                     if (counterGame185g > 10)
  659.                         counterGame185g--;
  660.                     if (counterGame185b > 55)
  661.                         counterGame185b--;
  662.                 }
  663.                 else {
  664.                     if (counterGame185r < 185)
  665.                         counterGame185r++;
  666.                     if (counterGame185g < 185)
  667.                         counterGame185g++;
  668.                     if (counterGame185b < 185)
  669.                         counterGame185b++;
  670.                 }
  671.  
  672.                 if (!screenState[MENUSCREEN] && ship.getLives() > 0) {
  673.  
  674.                     bgStars.update();
  675.                     bgNebulas.update();
  676.  
  677.                     updateBullet(bullets);
  678.                     collideBullet(bullets, cometsBig, cometsSmall, enemyShip, ship);
  679.  
  680.                     bulletEnemy->update(bulletEnemy);
  681.                     bulletEnemy->collide(bulletEnemy, ship);
  682.  
  683.                     startCometBig(cometsBig);
  684.                     updateCometBig(cometsBig);
  685.                     collideCometBig(cometsBig, ship);
  686.  
  687.                     startCometSmall(cometsSmall);
  688.                     updateCometSmall(cometsSmall);
  689.                     collideCometSmall(cometsSmall, ship);
  690.  
  691.                     healthUp->start(healthUp);
  692.                     healthUp->update(healthUp);
  693.                     healthUp->collide(healthUp, ship);
  694.  
  695.                     enemyShip->start(enemyShip);
  696.                     enemyShip->update(enemyShip, ship);
  697.                     enemyShip->collide(enemyShip, ship);
  698.  
  699.                     if (shieldTimer < 120 * 10) {
  700.                         shieldTimer++;
  701.                         ship.setShield(0);
  702.                     }
  703.                     else if (shieldTimer == 1200) {
  704.                         ship.setShield(1);
  705.                     }
  706.  
  707.                     if (keys[W]) {
  708.                         ship.moveUp();
  709.                     }
  710.                     else if (keys[S]) {
  711.                         ship.moveDown();
  712.                     }
  713.                     if (keys[A]) {
  714.                         ship.moveLeft();
  715.                     }
  716.                     else if (keys[D]) {
  717.                         ship.moveRight();
  718.                     }
  719.  
  720.                     framesElapsed++;
  721.                     seconds = framesElapsed / 120;
  722.  
  723.                     if (seconds >= 60) {
  724.                         minutes++;
  725.                         seconds = 0;
  726.                         framesElapsed = 0;
  727.                     }
  728.                 }
  729.  
  730.                 frames++;
  731.                 if (al_current_time() - gameTime >= 1) {
  732.                     gameTime = al_current_time();
  733.                     gameFPS = frames;
  734.                     frames = 0;
  735.                 }
  736.             }
  737.  
  738.             if (rendering && al_is_event_queue_empty(eventQueue)) {
  739.                 rendering = false;
  740.  
  741.                 al_clear_to_color(al_map_rgb(35, 10, 55));
  742.  
  743.                 bgStars.render();
  744.                 bgNebulas.render();
  745.  
  746.                 renderBullet(bullets);
  747.  
  748.                 renderCometBig(cometsBig);
  749.                 renderCometSmall(cometsSmall);
  750.  
  751.                 bulletEnemy->render(bulletEnemy);
  752.  
  753.                 healthUp->render(healthUp);
  754.                 enemyShip->render(enemyShip);
  755.                 ship.render();
  756.  
  757.                 if (counterGame185r > 36) {
  758.                     al_draw_text(mainFont, al_map_rgb(counterGame185r, counterGame185g, counterGame185b), screenW / 2, screenH / 6, 1, "Use WSAD to move");
  759.                     al_draw_text(mainFont, al_map_rgb(counterGame185r, counterGame185g, counterGame185b), screenW / 2, screenH / 5 + 30, 1, "Use SPACE to shoot");
  760.                 }
  761.  
  762.                 if (ship.getLives() <= 0) {
  763.                     al_draw_text(titleFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 2 - 80, 1, "GAME OVER");
  764.                 }
  765.  
  766.                 al_draw_filled_rectangle(5, 41, shieldTimer / 10, 59, al_map_rgb(255, 204, 0));
  767.                 al_draw_rectangle(5, 41, 1200 / 10, 59, al_map_rgb(255, 255, 255), 2);
  768.                 al_draw_textf(smallFont, al_map_rgb(255, 255, 255), 5, 65, 0, "Score: %i", ship.getScore());
  769.  
  770.                 /*DEBUG MODE*/
  771.                 if (keys[F3]) {
  772.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 77, ALLEGRO_ALIGN_LEFT, "FPS: %i", gameFPS);
  773.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 89, ALLEGRO_ALIGN_LEFT, "Time: %i min, %i sec", minutes, seconds);
  774.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 101, ALLEGRO_ALIGN_LEFT, "Frames: %i", framesElapsed);
  775.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 113, ALLEGRO_ALIGN_LEFT, "Mouse X: %i", mouseX);
  776.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 125, ALLEGRO_ALIGN_LEFT, "Mouse Y: %i", mouseY);
  777.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 137, ALLEGRO_ALIGN_LEFT, "Objects: %i", objectCount);
  778.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 149, ALLEGRO_ALIGN_LEFT, "Lives: %i", ship.getLives());
  779.                     al_draw_textf(smallFont, al_map_rgb(0, 255, 0), 5, 161, ALLEGRO_ALIGN_LEFT, "Shield: %i", ship.getShield());
  780.  
  781.                     al_draw_bitmap(mouseBMP, mouseX, mouseY, 0);
  782.                 }
  783.  
  784.                 if (screenState[MENUSCREEN]) {
  785.                     /*DEBUG MODE*/
  786.                     if (keys[F3]) {
  787.                         al_draw_filled_rectangle(bExit2->getX() - bExit2->getBoundX(), bExit2->getY(), bExit2->getX() + bExit2->getBoundX(), bExit2->getY() + bExit2->getBoundY() * 2, al_map_rgb(0, 255, 20));
  788.                         al_draw_filled_rectangle(bMenu->getX() - bMenu->getBoundX(), bMenu->getY(), bMenu->getX() + bMenu->getBoundX(), bMenu->getY() + bMenu->getBoundY() * 2, al_map_rgb(0, 255, 20));
  789.                     }
  790.  
  791.                     if (ship.getLives() > 0)
  792.                         al_draw_text(titleFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 2 - 80, 1, "PAUSED");
  793.                     else if (ship.getLives() <= 0)
  794.                         al_draw_text(titleFont, al_map_rgb(255, 255, 255), screenW / 2, screenH / 2 - 80, 1, "GAME OVER");
  795.  
  796.                     bExit2->render(5);
  797.                     bMenu->render(6);
  798.  
  799.                     al_draw_bitmap(mouseBMP, mouseX, mouseY, 0);
  800.                 }
  801.  
  802.                 al_flip_display();
  803.                 al_clear_to_color(al_map_rgb(0, 0, 0));
  804.             }
  805.         }
  806.     }
  807.  
  808.     delete screenStateC;
  809.     delete bPlay;
  810.     delete bHelp;
  811.     delete bOptn;
  812.     delete bBack;
  813.     delete bExit;
  814.     delete bMenu;
  815.     delete bExit2;
  816.     delete bVolumeP;
  817.     delete bVolumeM;
  818.     delete bDifficultyP;
  819.     delete bDifficultyM;
  820.  
  821.     al_destroy_display(display);
  822.     al_destroy_event_queue(eventQueue);
  823.     al_destroy_timer(timer);
  824.     al_destroy_bitmap(mouseBMP);
  825.     al_destroy_font(mainFont);
  826.     al_destroy_font(smallFont);
  827.     al_destroy_font(titleFont);
  828.     al_destroy_font(bigFont);
  829.  
  830.     al_destroy_bitmap(player);
  831.     al_destroy_bitmap(playerDamaged);
  832.     al_destroy_bitmap(playerLeft);
  833.     al_destroy_bitmap(playerRight);
  834.     al_destroy_bitmap(playerShield);
  835.     al_destroy_bitmap(bulletPlayer);
  836.     al_destroy_bitmap(nebulaBig);
  837.     al_destroy_bitmap(nebulaSmall);
  838.     al_destroy_bitmap(nebulaVerySmall);
  839.     al_destroy_bitmap(livesIcon);
  840.  
  841.     al_destroy_sample(bulletSound);
  842.     al_destroy_sample(introSound);
  843.  
  844.     return 0;
  845. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement