Advertisement
Zilvisss

zaidimas

May 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 25.45 KB | None | 0 0
  1. // Projektas_Ktu.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <stdlib.h>
  7. #include <allegro5/allegro.h>
  8. #include <allegro5/allegro_native_dialog.h>
  9. #include <allegro5/allegro_font.h>
  10. #include <allegro5/allegro_ttf.h>
  11. #include <allegro5/allegro_primitives.h>
  12. #include <allegro5/allegro_image.h>
  13. #include <allegro5/allegro_audio.h>
  14. #include <allegro5/allegro_acodec.h>
  15. #include "objects.h"
  16.  
  17. #define width 1600  //Taip daroma, kad negalima butu  toliau pakeisti situ verciu
  18. #define height 900  //Panasiai kaip su const
  19.  
  20. //Funkciju prototipai
  21. void initPlayer(Player *player);
  22. void moveUp(Player *player);
  23. void moveDown(Player &player);
  24. void moveLeft(Player &player);
  25. void moveRight(Player &player);
  26. void initBullet(Bullet bullet[], int size);
  27. void initEnemyBullet(Bullet bullet[], int size);
  28. void drawBullet(Bullet bullet[], int size);
  29. void fireBullet(Bullet bullet[], int size, Player &player);
  30. void updateBullet(Bullet bullet[], int size, Player &player);
  31. void initEnemy(const char* file_name, Enemy enemy[], int size);
  32. void drawEnemy(Enemy enemy[], int size, ALLEGRO_BITMAP *picture);
  33. void updateEnemy(Enemy enemy[], int size, Bullet bullet[], int bulletSize, Player &player);
  34. void updateScreen(Player &player, int &screenNumb);
  35. void fireEnemyBullet(Bullet bullet[], int size, Enemy enemy[], int sizeEnemy);
  36. void writeHighScore(Player &player);
  37. void delayReload(bool *reload);     //AR REIKIA POINTERIO???
  38. void drawPowerUp(int time, bool notUsed, ALLEGRO_BITMAP *picture);
  39. void updatePowerUp(Player *player, bool *notUsed);
  40. int* initHighscore();
  41. //void drawHighScore(int* a, ALLEGRO_FONT* font);
  42.  
  43. //Globalus kintamieji
  44. enum direction { DOWN, LEFT, RIGHT, UP };   // by default enums are 32bit intigers     Down lygus 0, left 1, right 2, up 3
  45.  
  46. int dir = DOWN;  //??? NAUDIJAMAS I KURIA PUSE SAUTI. AR REIKIA KAD I VISAS GALIMA BUTU???
  47. int bulletCount = 100;    // DABARTINIS KULKU SKAICIUS, NE MAX
  48. int reloadTime = 60;    //Ciklu skaicius
  49. const int NUM_BULLETS = 100; //Galima ir defininti bet nera skirtumo (taip patogiau)
  50. const int NUM_BULLETS_ENEMY1 = 150;
  51. const int NUM_ENEMY = 3;    //Stukturu masyvas dydis turi buti pastovus skaicius!!
  52. const int NUM_ENEMY_1 = 5;
  53. const char* enemy_tut = "enemyTutorial.txt";
  54. const char* enemy_lvl_1 = "enemy1_LVL.txt";
  55. int screenPosition = 0;
  56. int timeCount = 9000;
  57. int* p;
  58. ALLEGRO_SAMPLE* judesys = NULL;
  59. int main()
  60. {
  61.     ALLEGRO_DISPLAY* display;
  62.     ALLEGRO_SAMPLE* sample = NULL;
  63.    
  64.  
  65.     if (!al_init()) //Cekina ar pasileidzia alegro
  66.     {
  67.         al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize allegro", NULL, NULL);
  68.         return -1;
  69.     }
  70.     display = al_create_display(width, height);
  71.  
  72.     if (!display)   //Patikrina ar susikuria ekrano vaizdas(display)
  73.     {
  74.         al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize the display", NULL, NULL);
  75.         return -1;
  76.     }
  77.  
  78.  
  79.  
  80.     Player player; //Sukuria Player struktura pavadinimu player is objects.h
  81.     Bullet bullet[NUM_BULLETS]; //Same kas su player tik cia Bullet strukturos bullet masyvas kurio dydis yra NUM_BULLETS
  82.     Bullet enemBullet[NUM_BULLETS_ENEMY1];
  83.  
  84.     Enemy enemy[NUM_ENEMY]; //Nukelti zemiau skirtingiems lvl bus siktingas!!!
  85.     Enemy enemy1[NUM_ENEMY_1];
  86.  
  87.  
  88.     bool done = false, active = false, draw = true;  //REIKES TAISYTI!!! KAD NAUJU BUTI KELIEMS EKRANAMS
  89.     bool mainMeniu = true, reloadBool = false;
  90.     bool redraw = true, optionsBool = false, highscoreBool = false, redrawOptions = false, redrawHigh = false, powerUpBool = true;
  91.     bool dudeBool = false, redrawDude = false;
  92.     bool instructionBool = false, redrawInstruction = false;
  93.  
  94.     const float FPS = 60; //GLOBALUS turetu but????
  95.  
  96.  
  97.     al_init_image_addon();
  98.     al_init_primitives_addon();
  99.     al_install_keyboard();
  100.     al_init_font_addon();
  101.     al_init_ttf_addon();
  102.     initPlayer(&player);
  103.     initBullet(bullet, NUM_BULLETS);
  104.     initEnemy(enemy_tut, enemy, NUM_ENEMY);
  105.     initEnemy(enemy_lvl_1, enemy1, NUM_ENEMY_1);
  106.     initEnemyBullet(enemBullet, NUM_BULLETS_ENEMY1);
  107.     int arowX = 350, arowY = 350;
  108.    
  109.  
  110.     al_install_audio();
  111.     al_init_acodec_addon();
  112.     al_reserve_samples(2);
  113.  
  114.  
  115.     sample = al_load_sample("Tema.ogg");
  116.     judesys = al_load_sample("JUDESYS.ogg");
  117.    
  118.  
  119.  
  120.     ALLEGRO_KEYBOARD_STATE keystate;
  121.     //Paleidziami failai
  122.     ALLEGRO_BITMAP* character = al_load_bitmap("bicas60x60.png");   //REIKES GALE ISTRINTI!!!
  123.     ALLEGRO_BITMAP* character_2 = al_load_bitmap("bicas_2_60x60.png");
  124.     ALLEGRO_BITMAP* character_1 = al_load_bitmap("bicas60x60.png");
  125.     ALLEGRO_BITMAP* target = al_load_bitmap("target_50x50.png");
  126.     ALLEGRO_BITMAP* mushroom = al_load_bitmap("mushroom70x70.png");
  127.     ALLEGRO_BITMAP* arrow = al_load_bitmap("arrow110x60.png");
  128.     ALLEGRO_BITMAP* healthPotion = al_load_bitmap("PotionRedBottle30x30.png");
  129.     ALLEGRO_FONT* font = al_load_font("Rastas.ttf", 25, 0);
  130.     ALLEGRO_TIMER* timer = al_create_timer(1.0 / FPS);
  131.     ALLEGRO_EVENT_QUEUE * event_queue = al_create_event_queue();
  132.     al_register_event_source(event_queue, al_get_keyboard_event_source());
  133.     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  134.     al_register_event_source(event_queue, al_get_display_event_source(display));
  135.  
  136.     ALLEGRO_BITMAP * buttonUP = al_load_bitmap("buttonUP_50x50.png");
  137.     ALLEGRO_BITMAP * buttonDOWN = al_load_bitmap("buttonDOWN_50x50.png");
  138.     ALLEGRO_BITMAP * buttonLEFT = al_load_bitmap("buttonLEFT_50x50.png");
  139.     ALLEGRO_BITMAP * buttonRIGHT = al_load_bitmap("buttonRIGHT_50x50.png");
  140.     ALLEGRO_BITMAP * buttonQ = al_load_bitmap("buttonQ_50x50.png");
  141.     ALLEGRO_BITMAP * buttonR = al_load_bitmap("buttonR_50x50.png");
  142.     ALLEGRO_BITMAP * buttonESC = al_load_bitmap("buttonESC_50x50.png");
  143.     ALLEGRO_BITMAP * buttonSPACEBAR = al_load_bitmap("buttonSPACEBAR_105x50.png");
  144.     ALLEGRO_BITMAP * buttonBACKSPACE = al_load_bitmap("buttonBACKSPACE_105x50.png");
  145.  
  146.  
  147.     al_play_sample(sample, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, NULL);
  148.  
  149.  
  150.     al_start_timer(timer);
  151.  
  152.  
  153.     while (!done)
  154.     {
  155.         ALLEGRO_EVENT event;
  156.         al_wait_for_event(event_queue, &event);
  157.         al_get_keyboard_state(&keystate);
  158.  
  159.  
  160.  
  161.         p = initHighscore();
  162.  
  163.         while (mainMeniu)
  164.         {
  165.             ALLEGRO_EVENT event;
  166.             al_wait_for_event(event_queue, &event);
  167.             al_get_keyboard_state(&keystate);
  168.  
  169.             if (al_key_down(&keystate, ALLEGRO_KEY_ENTER))
  170.             {
  171.  
  172.                 switch (arowY)
  173.                 {
  174.                 case 350:
  175.                     mainMeniu = false;
  176.                     break;
  177.  
  178.                 case 450:
  179.                     optionsBool = true;
  180.                     redrawOptions = true;
  181.                     while (optionsBool)
  182.                     {
  183.                         ALLEGRO_EVENT event;
  184.                         al_wait_for_event(event_queue, &event);
  185.                         al_get_keyboard_state(&keystate);
  186.  
  187.                         if (al_key_down(&keystate, ALLEGRO_KEY_Q))
  188.                         {
  189.                             optionsBool = false;
  190.                             arowY = 350;
  191.                         }
  192.  
  193.  
  194.  
  195.                         redrawOptions = true;
  196.  
  197.                         if (redrawOptions && al_is_event_queue_empty(event_queue)) {
  198.                             redrawOptions = false;
  199.                             al_draw_textf(font, al_map_rgb(255, 255, 255), 500, 50, 0, "Controls");
  200.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 500, 100, 0, "Move up ");
  201.                             al_draw_bitmap(buttonUP, 900, 100, 0);
  202.  
  203.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 500, 200, 0, "Move down");
  204.                             al_draw_bitmap(buttonDOWN, 900, 200, 0);
  205.  
  206.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 500, 300, 0, "Move right ");
  207.                             al_draw_bitmap(buttonRIGHT, 900, 300, 0);
  208.  
  209.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 500, 400, 0, "Move left ");
  210.                             al_draw_bitmap(buttonLEFT, 900, 400, 0);
  211.  
  212.                             al_draw_textf(font, al_map_rgb(139, 0, 0), 500, 800, 0, "Shoot ");
  213.                             al_draw_bitmap(buttonSPACEBAR, 900, 800, 0);
  214.  
  215.                             al_draw_textf(font, al_map_rgb(139, 0, 0), 500, 700, 0, "Reload weapon ");
  216.                             al_draw_bitmap(buttonR, 900, 700, 0);
  217.  
  218.                             al_draw_textf(font, al_map_rgb(0, 128, 0), 500, 500, 0, "Back to main menu ");
  219.                             al_draw_bitmap(buttonQ, 900, 500, 0);
  220.  
  221.                             al_draw_textf(font, al_map_rgb(0, 128, 0), 500, 600, 0, "Quit the game ");
  222.                             al_draw_bitmap(buttonESC, 900, 600, 0);
  223.  
  224.  
  225.  
  226.                             al_flip_display();
  227.                             al_clear_to_color(al_map_rgb(0, 0, 0));
  228.                         }
  229.  
  230.  
  231.                     }
  232.                     break;
  233.  
  234.  
  235.                 case 550:
  236.                     highscoreBool = true;
  237.                     redrawHigh = true;
  238.  
  239.                     while (highscoreBool)
  240.                     {
  241.                         ALLEGRO_EVENT event;
  242.                         al_wait_for_event(event_queue, &event);
  243.                         al_get_keyboard_state(&keystate);
  244.  
  245.                         if (al_key_down(&keystate, ALLEGRO_KEY_Q))
  246.                         {
  247.                             highscoreBool = false;
  248.                             arowY = 350;
  249.                         }
  250.  
  251.                         redrawHigh = true;
  252.  
  253.                         if (redrawHigh && al_is_event_queue_empty(event_queue)) {
  254.                             redrawHigh = false;
  255.                             //drawHighScore(p, font);
  256.                             //al_draw_textf(font, al_map_rgb(0, 0, 255), width / 2, 100, 0, "JJ   : 1500 ");
  257.                             //al_draw_textf(font, al_map_rgb(0, 0, 255), width / 2, 150, 0, "Kitas plat  :500");
  258.                             //al_draw_textf(font, al_map_rgb(0, 0, 255), width / 2, 200, 0, "kkk 12");
  259.                             //al_draw_textf(font, al_map_rgb(0, 0, 255), width / 2, 400, 0, "%d",p[0]);
  260.                             al_flip_display();
  261.                             al_clear_to_color(al_map_rgb(0, 0, 0));
  262.  
  263.                         }
  264.  
  265.  
  266.                     }
  267.  
  268.                     break;
  269.                     // code to be executed if n is equal to constant2;
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.                 case 650:
  280.                     dudeBool = true;
  281.                     redrawDude = true;
  282.  
  283.                     while (dudeBool)
  284.                     {
  285.                         ALLEGRO_EVENT event;
  286.                         al_wait_for_event(event_queue, &event);
  287.                         al_get_keyboard_state(&keystate);
  288.  
  289.                         if (al_key_down(&keystate, ALLEGRO_KEY_Q))
  290.                         {
  291.                             dudeBool = false;
  292.                             arowY = 350;
  293.                         }
  294.  
  295.                         redrawDude = true;
  296.  
  297.  
  298.  
  299.                         if (redrawDude && al_is_event_queue_empty(event_queue)) {
  300.                             redrawDude = false;
  301.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 500, 200, 0, "1 Zmogeliukas ");
  302.                             al_draw_bitmap(character_1, 500, 250, 0);
  303.  
  304.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 500, 600, 0, "2 Zmogeliukas ");
  305.                             al_draw_bitmap(character_2, 500, 650, 0);
  306.                             al_flip_display();
  307.                             al_clear_to_color(al_map_rgb(0, 0, 0));
  308.                             al_draw_bitmap(arrow, arowX, arowY, 0);
  309.                         }
  310.  
  311.                         if (al_key_down(&keystate, ALLEGRO_KEY_DOWN)) {
  312.                             arowY += 400;
  313.                             al_rest(0.2);
  314.                         }
  315.  
  316.                         if (al_key_down(&keystate, ALLEGRO_KEY_UP)) {
  317.                             arowY -= 400;
  318.                             al_rest(0.2);
  319.                         }
  320.                         if (al_key_down(&keystate, ALLEGRO_KEY_ENTER))
  321.                         {
  322.  
  323.                             switch (arowY)
  324.                             {
  325.                             case 250:
  326.  
  327.                                 character = al_load_bitmap("bicas60x60.png");
  328.  
  329.                                 break;
  330.  
  331.                             case 650:
  332.  
  333.                                 character = al_load_bitmap("bicas_2_60x60.png");
  334.                                 break;
  335.  
  336.  
  337.  
  338.                             }
  339.  
  340.  
  341.                         }
  342.  
  343.  
  344.                     }
  345.  
  346.                     break;
  347.  
  348.  
  349.                 case 750:
  350.                     instructionBool = true;
  351.                     redrawInstruction = true;
  352.  
  353.                     while (instructionBool)
  354.                     {
  355.                         ALLEGRO_EVENT event;
  356.                         al_wait_for_event(event_queue, &event);
  357.                         al_get_keyboard_state(&keystate);
  358.  
  359.                         if (al_key_down(&keystate, ALLEGRO_KEY_Q))
  360.                         {
  361.                             instructionBool = false;
  362.                             arowY = 350;
  363.                         }
  364.  
  365.                         redrawInstruction = true;
  366.  
  367.                         if (redrawInstruction && al_is_event_queue_empty(event_queue)) {
  368.                             redrawInstruction = false;
  369.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 300, 300, 0, "Shoot the target");
  370.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 300, 350, 0, "Try to hit as many bullets as you can");
  371.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 300, 400, 0, "Avoid enemy bullets");
  372.                             al_draw_textf(font, al_map_rgb(0, 191, 255), 300, 450, 0, "Complete all levels as possible faster");
  373.  
  374.                             al_draw_textf(font, al_map_rgb(0, 0, 255), 900, 200, 0, " Bullet count is written in bottom left corner");
  375.                             al_draw_textf(font, al_map_rgb(0, 0, 255), 900, 250, 0, "Your score is written in bottom at middle");
  376.                             al_draw_textf(font, al_map_rgb(0, 0, 255), 900, 300, 0, "Timer is written in Top left corner");
  377.  
  378.  
  379.  
  380.                             al_draw_bitmap(character_2, 900, 400, 0);
  381.                             al_draw_bitmap(target, 1300, 400, 0);
  382.                             al_draw_filled_circle(970, 420, 2, al_map_rgb(255, 0, 0));
  383.                             al_draw_filled_circle(1000, 420, 2, al_map_rgb(255, 0, 0));
  384.                             al_draw_filled_circle(1030, 420, 2, al_map_rgb(255, 0, 0));
  385.                             al_draw_filled_circle(1060, 420, 2, al_map_rgb(255, 0, 0));
  386.                             al_draw_filled_circle(1090, 420, 2, al_map_rgb(255, 0, 0));
  387.                             al_draw_filled_circle(1120, 420, 2, al_map_rgb(255, 0, 0));
  388.                             al_draw_filled_circle(1150, 420, 2, al_map_rgb(255, 0, 0));
  389.                             al_draw_filled_circle(1180, 420, 2, al_map_rgb(255, 0, 0));
  390.                             al_draw_filled_circle(1210, 420, 2, al_map_rgb(255, 0, 0));
  391.                             al_draw_filled_circle(1240, 420, 2, al_map_rgb(255, 0, 0));
  392.                             al_draw_filled_circle(1270, 420, 2, al_map_rgb(255, 0, 0));
  393.                             al_flip_display();
  394.                             al_clear_to_color(al_map_rgb(0, 0, 0));
  395.  
  396.                         }
  397.  
  398.  
  399.                     }
  400.  
  401.                     break;
  402.                 }
  403.  
  404.  
  405.  
  406.             }
  407.  
  408.  
  409.             if (al_key_down(&keystate, ALLEGRO_KEY_DOWN)) {
  410.                 arowY += 100;
  411.                 al_rest(0.2);
  412.             }
  413.  
  414.             if (al_key_down(&keystate, ALLEGRO_KEY_UP)) {
  415.                 arowY -= 100;
  416.                 al_rest(0.2);
  417.             }
  418.             redraw = true;
  419.  
  420.             if (redraw && al_is_event_queue_empty(event_queue)) {
  421.                 redraw = false;
  422.                 al_draw_textf(font, al_map_rgb(0, 255, 0), 500, 350, 0, "Pres enter to start a game");
  423.                 al_draw_textf(font, al_map_rgb(0, 255, 0), 500, 450, 0, "Options");
  424.                 al_draw_textf(font, al_map_rgb(0, 255, 0), 500, 550, 0, "Highscore");
  425.                 al_draw_textf(font, al_map_rgb(0, 255, 0), 500, 650, 0, "Select character");
  426.                 al_draw_textf(font, al_map_rgb(0, 255, 0), 500, 750, 0, "Instructions");
  427.  
  428.                 al_draw_bitmap(arrow, arowX, arowY, 0);
  429.                 al_flip_display();
  430.                 al_clear_to_color(al_map_rgb(0, 0, 0));
  431.             }
  432.  
  433.         }
  434.  
  435.  
  436.  
  437.             if (al_key_down(&keystate, ALLEGRO_KEY_ESCAPE))     //Galima rasyti if be {} jei TIK VIENA EILUTE VYKDYMO
  438.                 done = true;
  439.  
  440.             else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
  441.                 done = true;
  442.  
  443.  
  444.             else if (event.type == ALLEGRO_EVENT_TIMER)
  445.             {
  446.                 updateBullet(bullet, NUM_BULLETS, player);
  447.                 updateBullet(enemBullet, NUM_BULLETS_ENEMY1, player);
  448.                 delayReload(&reloadBool);
  449.                 switch (screenPosition)
  450.                 {
  451.                 case 0:
  452.                     updateEnemy(enemy, NUM_ENEMY, bullet, NUM_BULLETS, player); //Udatina kai nk nepaspausta
  453.                     break;
  454.  
  455.                 case 1:
  456.                     updateEnemy(enemy1, NUM_ENEMY_1, bullet, NUM_BULLETS, player);
  457.                     break;
  458.  
  459.                 default:
  460.                     updateEnemy(enemy, NUM_ENEMY, bullet, NUM_BULLETS, player); //Udatina kai nk nepaspausta
  461.                     break;
  462.                 }
  463.  
  464.  
  465.                 active = true;   //AR naudoju?
  466.  
  467.                 if (al_key_down(&keystate, ALLEGRO_KEY_UP))     //SU SWITCH CASE GALIMA PADARYTI
  468.                     moveUp(&player);
  469.                
  470.                 if (al_key_down(&keystate, ALLEGRO_KEY_DOWN))
  471.                     moveDown(player);
  472.  
  473.                 if (al_key_down(&keystate, ALLEGRO_KEY_RIGHT))
  474.                     moveRight(player);
  475.  
  476.                 if (al_key_down(&keystate, ALLEGRO_KEY_LEFT))
  477.                     moveLeft(player);
  478.  
  479.                 if (al_key_down(&keystate, ALLEGRO_KEY_SPACE) && bulletCount > 0)   //Tikrina ar space paspausta ir ar kulku yra
  480.                 {
  481.                     fireBullet(bullet, NUM_BULLETS, player);
  482.  
  483.                 }
  484.  
  485.                 if (al_key_down(&keystate, ALLEGRO_KEY_R))  // PADARYTI KAD LAIKAS PRAEITU
  486.                 {
  487.                     reloadBool = true;
  488.  
  489.                 }
  490.  
  491.                 else
  492.                 {
  493.                     active = false;     //LYG NENAUDOJU
  494.                 }
  495.  
  496.                 draw = true;
  497.                 updateBullet(bullet, NUM_BULLETS, player);
  498.                 updateBullet(enemBullet, NUM_BULLETS_ENEMY1, player);
  499.                 updatePowerUp(&player, &powerUpBool);
  500.  
  501.                 switch (screenPosition)
  502.                 {
  503.                 case 0:
  504.                     updateEnemy(enemy, NUM_ENEMY, bullet, NUM_BULLETS, player); //Udatina kai kazkas paspausta
  505.                     break;
  506.  
  507.                 case 1:
  508.                     updateEnemy(enemy1, NUM_ENEMY_1, bullet, NUM_BULLETS, player);
  509.                     fireEnemyBullet(enemBullet, NUM_BULLETS_ENEMY1, enemy1, NUM_ENEMY_1);
  510.                     break;
  511.  
  512.                 default:        //AR REIKIA DEFAULTO?????
  513.                     updateEnemy(enemy, NUM_ENEMY, bullet, NUM_BULLETS, player); //Udatina kai kazkas paspausta
  514.                     break;
  515.                 }
  516.  
  517.  
  518.             }
  519.  
  520.             if (draw && al_is_event_queue_empty(event_queue))
  521.             {
  522.                 draw = false;
  523.  
  524.  
  525.  
  526.                 al_draw_bitmap(character, player.x, player.y, 0);
  527.  
  528.                 switch (screenPosition)         //JAUCIU GALIMA PAKEISTI PDARIUS ENYMY MASYVA DIDESNI KAD NEREIKTU SWITCH CASO
  529.                 {
  530.                 case 0:
  531.                     drawEnemy(enemy, NUM_ENEMY, target);
  532.                     break;
  533.  
  534.                 case 1:
  535.                     drawEnemy(enemy1, NUM_ENEMY_1, mushroom);
  536.                     drawBullet(enemBullet, NUM_BULLETS_ENEMY1);
  537.                     break;
  538.  
  539.                 default:
  540.                     drawEnemy(enemy, NUM_ENEMY, target);
  541.                     break;
  542.                 }
  543.  
  544.  
  545.                 drawBullet(bullet, NUM_BULLETS);
  546.  
  547.                 drawPowerUp(timeCount, powerUpBool, healthPotion);
  548.  
  549.                 al_draw_textf(font, al_map_rgb(44, 117, 255), 10, 800, 0, "Bullets: %d", bulletCount); //Kulku skaiciu
  550.                 al_draw_textf(font, al_map_rgb(255, 0, 0), 200, 800, 0, "Score: %d", player.score); //Scora
  551.                 al_draw_textf(font, al_map_rgb(0, 100, 0), 350, 800, 0, "Lives %d", player.lives);              //REIKIAPADARYTI KAD KAI MAZAI GYVYBIU RAUDONETU SPAVLA
  552.                 al_draw_textf(font, al_map_rgb(0, 255, 0), 400, 100, 0, "BOOOL %d", reloadBool);
  553.                 if (reloadBool)
  554.                     al_draw_textf(font, al_map_rgb(0, 255, 0), 10, 850, 0, "RELOADING");
  555.  
  556.                 //al_draw_textf(font, al_map_rgb(255, 117, 255), 400, 800, 0, "Priesu: %d", NUM_ENEMY); //bandom
  557.                 //al_draw_textf(font, al_map_rgb(255, 117, 255), 400, 600, 0, "Priesu y: %d", enemy[0].y); //bandom
  558.                 //al_draw_textf(font, al_map_rgb(255, 117, 255), 400, 400, 0, "Screen: %d", screenPosition); //bandom
  559.  
  560.                 //--------------------------------------------------------------------//
  561.                 timeCount--;
  562.                 al_draw_textf(font, al_map_rgb(255, 0, 255), 400, 50, 0, "Time left: %d", (timeCount / 60 + 1)); //bandom
  563.                 if (timeCount == 0) {
  564.                     writeHighScore(player);
  565.                     done = true;
  566.                 };
  567.                 //--------------------------------------------------------------------//
  568.  
  569.  
  570.                 al_flip_display();
  571.                 al_clear_to_color(al_map_rgb(255, 255, 255));
  572.  
  573.                 updateScreen(player, screenPosition);
  574.  
  575.  
  576.  
  577.  
  578.             }
  579.         }
  580.  
  581.         al_destroy_sample(sample);
  582.         al_destroy_sample(judesys);
  583.         al_destroy_display(display);
  584.         al_uninstall_keyboard();
  585.         al_destroy_bitmap(character);
  586.         al_destroy_bitmap(target);
  587.         al_destroy_bitmap(arrow);
  588.         al_destroy_bitmap(healthPotion);
  589.         al_destroy_font(font);
  590.         al_destroy_timer(timer);
  591.         al_destroy_event_queue(event_queue);
  592.  
  593.  
  594.         return 0;
  595.     }
  596.  
  597.  
  598. void initPlayer(Player *player)
  599. {
  600.     //foo->bar is equivalent to(*foo).bar, i.e.it gets the member called bar from the struct that foo points to.
  601.  
  602.     player->x = 10;
  603.     player->y = 10;
  604.     player->ID = PLAYER;
  605.     player->lives = 100;
  606.     player->speed = 5;
  607.     player->score = 0;
  608. }
  609.  
  610.  
  611. void initEnemy(const char* file_name, Enemy enemy[], int size) //Padaryt kad is failo
  612. {
  613.     FILE* file = fopen(file_name, "r");
  614.     int count_enem;
  615.     fscanf(file, "%d", &count_enem);
  616.     int i = 0;
  617.     for (i; i < count_enem; i++)
  618.     {
  619.         fscanf(file, "%d", &enemy[i].x);
  620.         fscanf(file, "%d", &enemy[i].y);
  621.         fscanf(file, "%d", &enemy[i].lives);
  622.         fscanf(file, "%f", &enemy[i].speed);
  623.         fscanf(file, "%d", &enemy[i].alive);
  624.         fscanf(file, "%d", &enemy[i].sizeX);
  625.         fscanf(file, "%d", &enemy[i].sizeY);
  626.     }
  627.  
  628.  
  629.     fclose(file);
  630.  
  631. }
  632.  
  633.  
  634. void moveUp(Player *player)
  635. {
  636.     player->y -= player->speed;
  637.     dir = UP;   //TAI AR REIKIA TOKIO VISUOSE VOID JUDESIO????
  638. }
  639.  
  640. void moveDown(Player &player)
  641. {
  642.     player.y += player.speed;
  643.     dir = DOWN;
  644. }
  645.  
  646. void moveLeft(Player &player)
  647. {
  648.     player.x -= player.speed;
  649.     dir = LEFT;
  650. }
  651.  
  652. void moveRight(Player &player)
  653. {
  654.     player.x += player.speed;
  655.     dir = RIGHT;
  656. }
  657.  
  658. void initBullet(Bullet bullet[], int size)
  659. {
  660.     int i = 0;
  661.     for (i; i < size; i++)
  662.     {
  663.         bullet[i].ID = BULLET; //NX reikia???
  664.         bullet[i].speed = 3.5;
  665.         bullet[i].live = false;
  666.         bullet[i].radius = 2;
  667.        
  668.     }
  669. }
  670.  
  671. void initEnemyBullet(Bullet bullet[], int size)
  672. {
  673.     int i;
  674.     for ( i = 0; i < size; i++)
  675.     {
  676.         bullet[i].ID = ENEMY;
  677.         bullet[i].speed = 1.5;
  678.         bullet[i].live = false;
  679.         bullet[i].radius = 4;
  680.     }
  681.  
  682. }
  683.  
  684. void drawBullet(Bullet bullet[], int size)
  685. {
  686.     int i = 0;
  687.     for (i; i < size; i++)
  688.     {
  689.         if (bullet[i].live)
  690.         {
  691.             al_draw_filled_circle(bullet[i].x, bullet[i].y, bullet[i].radius, al_map_rgb(255, 0, 0));
  692.         }
  693.     }
  694. }
  695.  
  696. void drawEnemy(Enemy enemy[], int size, ALLEGRO_BITMAP *picture) {
  697.     int i = 0;
  698.     for (i = 0; i < size; i++)
  699.     {
  700.         if (enemy[i].alive)
  701.         {
  702.             al_draw_bitmap(picture, enemy[i].x, enemy[i].y, 0);
  703.         }
  704.     }
  705. }
  706.  
  707. void updateEnemy(Enemy enemy[], int size, Bullet bullet[], int bulletSize, Player &player) {  //GALIMA SUTRUMPTINTI KAD NECEKINTU TU KUR NERA GYVI PRIESAI (KAI ENEMY.ALIVE=0)
  708.     int i = 0;
  709.     for (i; i < size; i++)
  710.     {
  711.         int j = 0;
  712.         for (j; j < bulletSize; j++)
  713.         {
  714.             if (bullet[j].x > enemy[i].x && bullet[j].x < (enemy[i].x + 5) && bullet[j].y > enemy[i].y && bullet[j].y < (enemy[i].y + enemy[i].sizeY) && enemy[i].alive)
  715.             {
  716.                 enemy[i].lives--;
  717.                 player.score++;
  718.                 bullet[j].live = false;
  719.                 bullet[j].x = 0;
  720.                 bullet[j].y = 0;
  721.             }
  722.  
  723.         }
  724.  
  725.         if (enemy[i].lives <= 0)
  726.         {
  727.             enemy[i].alive = 0;
  728.  
  729.         }
  730.     }
  731. }
  732.  
  733. void fireBullet(Bullet bullet[], int size, Player &player)
  734. {
  735.     int i = 0;
  736.     for (i; i < size; i++)
  737.     {
  738.         if (!bullet[i].live)
  739.         {
  740.             --bulletCount;
  741.             bullet[i].x = player.x + 35;  //Kad atrodytu lyg is sautuvo saudo
  742.             bullet[i].y = player.y + 20;
  743.             bullet[i].live = true;
  744.             break;
  745.         }
  746.     }
  747. }
  748.  
  749. void fireEnemyBullet(Bullet bullet[], int size, Enemy enemy[], int sizeEnemy) {
  750.  
  751.     int i;
  752.     for (i = 0; i < size; i++)
  753.     {
  754.         if (!bullet[i].live) {
  755.             int j;
  756.             for (j = 0; j < sizeEnemy; j++)
  757.             {
  758.                 if (rand() % 5000 == 0 && enemy[j].alive)
  759.                 {
  760.  
  761.                     bullet[i].live = true;
  762.                     bullet[i].x = enemy[j].x;
  763.                     bullet[i].y = enemy[j].y+35; // CIA REIKIA PAKEISTI KAD IVAIRIEM PRIESAM TIKTU FUMKCIJA!!!
  764.  
  765.                     break;
  766.                 }
  767.  
  768.             }
  769.         }
  770.  
  771.  
  772.     }
  773.  
  774. }
  775.  
  776.  
  777. void updateBullet(Bullet bullet[], int size, Player &player)
  778. {
  779.     int i = 0;
  780.     for (i; i < size; i++)
  781.     {
  782.         /*if (bullet[i].x > 1400 && bullet[i].x < 1405 && bullet[i].y >200 && bullet[i].y < 250 ||
  783.             bullet[i].x > 1400 && bullet[i].x < 1450 && bullet[i].y >400 && bullet[i].y < 450 ||
  784.             bullet[i].x > 1400 && bullet[i].x < 1450 && bullet[i].y >600 && bullet[i].y < 650) {   //Galima graziau surasytiGAL I NAUJA VOIDA SUDETI?!!
  785.             player.score++;  //Geriau nauja voida sukur update playeriui tipo arba bent scorui
  786.             bullet[i].live = false;
  787.             bullet[i].x = 0;
  788.             bullet[i].y = 0;
  789.  
  790.         }
  791.         */
  792.  
  793.         if (bullet[i].live)
  794.         {
  795.             if(bullet[i].ID ==1)
  796.             bullet[i].x += bullet[i].speed;
  797.  
  798.             else if (bullet[i].ID == 2) {
  799.                 bullet[i].x -= bullet[i].speed;
  800.                
  801.                 if ((bullet[i].x>=player.x && bullet[i].x + ) && bullet[i].y==player.y)         //TAISYTI KAD I VISA KUNA PATAIKYTU
  802.                 {
  803.                     player.lives--;
  804.                     bullet[i].live = false;
  805.                     al_play_sample(judesys, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
  806.                 }
  807.            
  808.             }
  809.  
  810.             if (bullet[i].x > width || bullet[i].x < 0)
  811.             {
  812.                 bullet[i].live = false;
  813.             }
  814.  
  815.         }
  816.  
  817.     }
  818. }
  819.  
  820. void updateScreen(Player &player, int &screenNumb) {    //SU STITH CASU PERDARYT
  821.  
  822.     if (player.score >= 150)
  823.     {
  824.         screenNumb = 1;
  825.     }
  826.  
  827. }
  828.  
  829. void writeHighScore(Player &player) {
  830.  
  831.     //Atidarom faila
  832.     FILE *failas = fopen("high.txt", "r");
  833.    
  834.     //ziurim ar ji egsistuoja, jei ne exitina
  835.     if (failas == NULL)
  836.     {
  837.         printf("Error opening file!\n");
  838.         exit(1);
  839.     }
  840.  
  841.     // sukuriam  penkiu nariu rezultatu masyva
  842.     int results[5], i;
  843.  
  844.     //I masyva sudedam narius
  845.     for (i = 0; i < 5; i++)
  846.     {
  847.         fscanf(failas, "%d", &results[i]);
  848.     }
  849.    
  850.  
  851. //Ziurima ar esamas scoras didesnis paskutini buvusi, jei taip tada naujas scoras rasomas i 5 masyvo vieta
  852.     if (player.score > results[4]) {
  853.         results[4] = player.score;
  854.        
  855.     }
  856.  
  857.  
  858.     //TADA REIKIA SURIKIUOI MASYVA results[5] didejimo tvarka
  859.     for (i = 4; i >0; i--)
  860.     {
  861.         if (results[i] > results[i - 1]) {
  862.             int a= results[i - 1];
  863.             results[i - 1] = results[i];
  864.             results[i] = a;
  865.  
  866.         }
  867.  
  868.     }
  869.  
  870.  
  871.  
  872.  
  873.     //
  874.  
  875.  
  876.     //uzdarom faila
  877.     fclose(failas);
  878.    
  879.     //Atidarome rasymui (Taip yra istrinami visi buve elementai)
  880.     failas = fopen("high.txt", "w");
  881.  
  882.     if (failas == NULL)
  883.     {
  884.         printf("Error opening file!\n");
  885.         exit(1);
  886.     }
  887.  
  888.     //Surasomi i faila elementai
  889.     for (i = 0; i < 5; i++)
  890.     {
  891.         fprintf(failas, "%d\n", results[i]);
  892.     }
  893.  
  894.  
  895.     fclose(failas);
  896.  
  897.  
  898. }
  899.  
  900. void delayReload(bool *reload) {
  901.     if (*reload) {
  902.         reloadTime--;
  903.  
  904.         if (reloadTime == 0) {
  905.             *reload = false;
  906.             bulletCount = NUM_BULLETS;
  907.             reloadTime = 60;
  908.         }
  909.  
  910.     }
  911. }
  912.  
  913. void drawPowerUp(int time, bool notUsed, ALLEGRO_BITMAP *picture) {
  914.     if (time <8000 && notUsed)
  915.     {
  916.         al_draw_bitmap(picture, 500, 500, 0);
  917.     }
  918.  
  919.  
  920. }
  921.  
  922. void updatePowerUp(Player *player, bool *notUsed) {
  923.  
  924.     if (player->x==500 && player->y ==500) {
  925.    
  926.         *notUsed = false;
  927.         player->lives += 15;
  928.     }
  929.  
  930. }
  931.  
  932.  
  933. int *initHighscore() {
  934.     //Atidarom faila
  935.     FILE* failas = fopen("high.txt", "r");
  936.  
  937.     //ziurim ar ji egsistuoja, jei ne exitina
  938.     if (failas == NULL)
  939.     {
  940.         printf("Error opening file!\n");
  941.         exit(1);
  942.     }
  943.  
  944.     // sukuriam  penkiu nariu rezultatu masyva
  945.     static  int results[5], i;
  946.  
  947.     //I masyva sudedam narius
  948.     for (i = 0; i < 5; i++)
  949.     {
  950.         fscanf(failas, "%d", &results[i]);
  951.     }
  952.  
  953.     return results;
  954. }
  955.  
  956.  
  957.  
  958. //void drawHighScore(int *a, ALLEGRO_FONT *font){
  959.     //int i, aukstis=0;
  960.     //int *variable;
  961.     //for ( i = 0; i < 5; i++)
  962.     //{
  963.        
  964.     //  variable = a[i];
  965.     //  al_draw_textf(font, al_map_rgb(0, 0, 255), width / 2, aukstis, "%d", variable);
  966.        
  967.     //  aukstis += 100;
  968.     //}
  969.    
  970.  
  971.    
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.    
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.  
  1003. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  1004. // Debug program: F5 or Debug > Start Debugging menu
  1005.  
  1006. // Tips for Getting Started:
  1007. //   1. Use the Solution Explorer window to add/manage files
  1008. //   2. Use the Team Explorer window to connect to source control
  1009. //   3. Use the Output window to see build output and other messages
  1010. //   4. Use the Error List window to view errors
  1011. //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
  1012. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement