Advertisement
Guest User

Untitled

a guest
Apr 14th, 2024
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 30.61 KB | None | 0 0
  1. #include <SDL2/SDL.h>
  2. #include <SDL2/SDL_image.h>
  3. #include "level_map.h"
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #define Width 700
  9. #define Height 700
  10.  
  11. SDL_Window * window;
  12. SDL_Renderer * renderer;
  13. SDL_Event event;
  14.  
  15. //player variable struct
  16. struct Player
  17. {
  18.         int dir; //direction player is facing.
  19.                  //0 is left. 1 is right
  20.         SDL_Rect src, dst;
  21. }player = {1};
  22.  
  23. struct Enemy
  24. {
  25.         SDL_Rect e_src, e_dst;
  26.         int active;
  27.         int frame,frame_width,frame_height,total_frames;
  28. }enemy = {.active = 1};
  29.  
  30. //level_0 object variable struct
  31. struct level_0
  32. {
  33.         SDL_Rect src, dst;
  34.  
  35. }door,wall,roof;
  36.  
  37. struct floor{
  38.         SDL_Rect src, dst;
  39. }ground;
  40.  
  41. //surface and texture pointers
  42. SDL_Surface* image;
  43. SDL_Surface* image2;
  44. SDL_Surface* image3;
  45. SDL_Surface* image4;
  46. SDL_Surface* image5;
  47. SDL_Surface* image6;
  48.  
  49. SDL_Texture* texture;
  50. SDL_Texture* player_tex;
  51. SDL_Texture* interior_textures;
  52. SDL_Texture* door_texture;
  53. SDL_Texture* background_tex;
  54. SDL_Texture* enemy_tex;
  55.  
  56. //collision box
  57. SDL_Rect collide_box = {500, 400, 200, 200};
  58. SDL_Rect collide_box2;
  59. SDL_Rect collide_box3;
  60.  
  61. #define max_particles 5000
  62.  
  63. struct Particles
  64. {
  65.         float x, y;
  66.         int xv, yv;
  67.         int spread;
  68.         int active;
  69.  
  70. }distance;
  71.  
  72. struct Particles particles[max_particles] = {0};
  73.  
  74. bool spray;
  75.  
  76. //emit and initialize particles
  77. void spray_pixels()
  78. {
  79.  
  80.         int count;
  81.  
  82.         if(spray)
  83.         {
  84.                 for(count = 0;count < max_particles;count++)
  85.                 {
  86.                         if(particles[count].active == 0)
  87.                         {
  88.                                 if(player.dir == 1)
  89.                                 {
  90.  
  91.                                         particles[count].x = player.dst.x + player.dst.w;
  92.                                         particles[count].xv = 1;
  93.                                 }
  94.                                 else
  95.                                 {
  96.                                         particles[count].x = ((player.dst.x + player.dst.w) - 100);
  97.                                         particles[count].xv = -1;
  98.                                 }
  99.  
  100.                                 distance.x = 0;
  101.                                 particles[count].y = player.dst.y + 50;
  102.                                 particles[count].yv = 1;
  103.  
  104.                                 particles[count].active = 1;
  105.                         }
  106.                         if(particles[count].x >= enemy.e_dst.x)
  107.                                 enemy.active = 0;
  108.  
  109.                 }
  110.         }
  111. }
  112.  
  113. //update particle position + velociity & check if active or inactive
  114. void update_spray()
  115. {
  116.         int count;
  117.  
  118.         for(count = 0;count < max_particles;count++)
  119.         {
  120.  
  121.                 if(particles[count].active == 1)
  122.                 {
  123.                         double factor = count - (max_particles - 1.0) / 2.0; //Makes top and bottom particles scatter by index number
  124.                                                                              //Distance.y is divided to control length of scatter
  125.                         particles[count].x += distance.x;
  126.                         particles[count].y = (player.dst.y + 50) - factor * particles[count].spread / (max_particles / 2);
  127.  
  128.                         distance.x += particles[count].xv;
  129.  
  130.                         particles[count].spread += particles[count].yv;
  131.                 }
  132.                 if(particles[count].x > Width || particles[count].x < 0)
  133.                 {
  134.                         particles[count].x = player.dst.x + player.dst.w;
  135.                         particles[count].y = player.dst.y + 50;
  136.  
  137.                         particles[count].xv = 0;
  138.                         particles[count].yv = 0;
  139.                         distance.x = 0;
  140.                         particles[count].spread = 0;
  141.  
  142.                         particles[count].active = 0;
  143.                 }
  144.  
  145.         }
  146. }
  147.  
  148. //render particles if they're active
  149. void render_spray()
  150. {
  151.         int count;
  152.         for(count = 0;count < max_particles;count++)
  153.         {
  154.                 if(particles[count].active == 1)
  155.                 {
  156.                         SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  157.                         SDL_RenderDrawPoint(renderer, particles[count].x, particles[count].y);
  158.                 }
  159.         }
  160. }
  161.  
  162.  
  163. //level loader
  164. void load_level(int current_level)
  165. {
  166.         switch(current_level)
  167.         {
  168.                 case 0:
  169.                         SDL_SetRenderDrawColor(renderer, 127, 0, 255, 255);
  170.                         SDL_RenderClear(renderer);
  171.                         for(int i = 0;i<map_width;i++)
  172.                         {
  173.                                 for(int j = 0;j<map_height;j++)
  174.                                 {
  175.                                 if(level_0[i][j] == _ground_)
  176.                                 {
  177.                                         SDL_Rect grass = {j * tile_size, i * tile_size, tile_size, tile_size};
  178.                                         SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
  179.                                         SDL_RenderFillRect(renderer, &grass);
  180.                                 }
  181.                                 if(level_0[j][i] == Wall)
  182.                                 {
  183.                                         wall.src.x = 304;
  184.                                         wall.src.y = 2;
  185.                                         wall.src.w = 100;
  186.                                         wall.src.h = 100;
  187.                                         wall.dst.x = i * tile_size;
  188.                                         wall.dst.y = j * tile_size;
  189.                                         wall.dst.w = tile_size;
  190.                                         wall.dst.h = tile_size;
  191.                                         SDL_RenderCopy(renderer, texture, &wall.src, &wall.dst);
  192.                                 }
  193.                                 if(level_0[j][i] == Roof)
  194.                                 {
  195.                                         roof.src.x = 0;
  196.                                         roof.src.y = 0;
  197.                                         roof.src.w = 100;
  198.                                         roof.src.h = 100;
  199.                                         roof.dst.x = i * tile_size;
  200.                                         roof.dst.y = j * tile_size;
  201.                                         roof.dst.w = tile_size;
  202.                                         roof.dst.h = tile_size;
  203.                                         SDL_RenderCopy(renderer, texture, &roof.src, &roof.dst);
  204.                                 }
  205.  
  206.                                 }
  207.                         }
  208.                         //door
  209.                         door.src.x;
  210.                         door.src.y;
  211.                         door.src.w = 100;
  212.                         door.src.h = 200;
  213.                         door.dst.x = 400;
  214.                         door.dst.y = 400;
  215.                         door.dst.w = 100;
  216.                         door.dst.h = 200;
  217.  
  218.                         //copy textures to door rect
  219.                         SDL_RenderCopy(renderer, door_texture, &door.src, &door.dst);
  220.                         break;
  221.                 case 1:
  222.                         SDL_RenderCopy(renderer, background_tex, NULL, NULL);
  223.                         for(int i = 0;i<map_width;i++)
  224.                         {
  225.                                 for(int j = 0;j<map_height;j++)
  226.                                 {
  227.                                 if(level_1[j][i] == _ground_)
  228.                                 {
  229.                                         ground.src.x = 100;
  230.                                         ground.src.y = 0;
  231.                                         ground.src.w = 100;
  232.                                         ground.src.h = 100;
  233.                                         ground.dst.x = i * tile_size;
  234.                                         ground.dst.y = j * tile_size;
  235.                                         ground.dst.w = tile_size;
  236.                                         ground.dst.h = tile_size;
  237.                                         SDL_RenderCopy(renderer, texture, &ground.src, &ground.dst);
  238.  
  239.                                 }
  240.                                 if(level_1[j][i] == stairs)
  241.                                 {
  242.                                         SDL_Rect src, dst;
  243.  
  244.                                         src.x = 0;
  245.                                         src.y = 0;
  246.                                         src.w = 200;
  247.                                         src.h = 200;
  248.                                         dst.x = i * tile_size + -100;
  249.                                         dst.y = j * tile_size + -100;
  250.                                         dst.w = tile_size * 2;
  251.                                         dst.h = tile_size * 2;
  252.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  253.  
  254.                                 }
  255.                                 if(level_1[j][i] == painting)
  256.                                 {
  257.                                         SDL_Rect src, dst;
  258.  
  259.                                         src.x = 200;
  260.                                         src.y = 200;
  261.                                         src.w = 200;
  262.                                         src.h = 200;
  263.                                         dst.x = i * tile_size + -100;
  264.                                         dst.y = j * tile_size + -100;
  265.                                         dst.w = tile_size * 2;
  266.                                         dst.h = tile_size * 2;
  267.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  268.  
  269.                                 }
  270.                                 }
  271.                         }
  272.                         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  273.                         SDL_RenderDrawRect(renderer, &collide_box);
  274.                         break;
  275.                 case 2:
  276.                         SDL_SetRenderDrawColor(renderer, 0, 255, 180, 255);
  277.                         SDL_RenderClear(renderer);
  278.                         for(int i = 0;i<map_width;i++)
  279.                         {
  280.                                 for(int j = 0;j<map_height;j++)
  281.                                 {
  282.                                 if(level_2[j][i] == _ground_)
  283.                                 {
  284.                                         ground.src.x = 100;
  285.                                         ground.src.y = 0;
  286.                                         ground.src.w = 100;
  287.                                         ground.src.h = 100;
  288.                                         ground.dst.x = i * tile_size;
  289.                                         ground.dst.y = j * tile_size;
  290.                                         ground.dst.w = tile_size;
  291.                                         ground.dst.h = tile_size;
  292.                                         SDL_RenderCopy(renderer, texture, &ground.src, &ground.dst);
  293.                                 }
  294.                                 if(level_2[j][i] == Door)
  295.                                 {
  296.  
  297.                                         door.dst.x = i * tile_size;
  298.                                         door.dst.y = j * tile_size;
  299.                                         door.dst.w = 100;
  300.                                         door.dst.h = 200;
  301.                                         SDL_RenderCopy(renderer, door_texture, NULL, &door.dst);
  302.  
  303.                                         collide_box2.x = door.dst.x;
  304.                                         collide_box2.y = door.dst.y;
  305.                                         collide_box2.w = door.dst.w;
  306.                                         collide_box2.h = door.dst.h;
  307.  
  308.                                         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  309.                                         SDL_RenderDrawRect(renderer, &collide_box2);
  310.  
  311.  
  312.                                 }
  313.                                 if(level_2[j][i] == glass_window)
  314.                                 {       SDL_Rect src, dst;
  315.  
  316.                                         src.x = 0;
  317.                                         src.y = 400;
  318.                                         src.w = 200;
  319.                                         src.h = 200;
  320.                                         dst.x = i * tile_size;
  321.                                         dst.y = j * tile_size;
  322.                                         dst.w = tile_size;
  323.                                         dst.h = tile_size;
  324.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  325.                                 }
  326.                                 if(level_2[j][i] == painting)
  327.                                 {       SDL_Rect src, dst;
  328.  
  329.                                         src.x = 600;
  330.                                         src.y = 200;
  331.                                         src.w = 200;
  332.                                         src.h = 200;
  333.                                         dst.x = i * tile_size - 50;
  334.                                         dst.y = j * tile_size;
  335.                                         dst.w = tile_size;
  336.                                         dst.h = tile_size;
  337.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  338.                                 }
  339.                                 }
  340.                         }
  341.                         break;
  342.                 case 3:
  343.                         SDL_SetRenderDrawColor(renderer, 245, 245, 220, 255);
  344.                         SDL_RenderClear(renderer);
  345.                         for(int i = 0;i<map_width;i++)
  346.                         {
  347.                                 for(int j = 0;j<map_height;j++)
  348.                                 {
  349.                                         if(level_3[j][i] == '#')
  350.                                         {
  351.                                         ground.src.x = 0;
  352.                                         ground.src.y = 0;
  353.                                         ground.src.w = tile_size;
  354.                                         ground.src.h = tile_size;
  355.                                         ground.dst.x = i * tile_size;
  356.                                         ground.dst.y = j * tile_size;
  357.                                         ground.dst.w = tile_size;
  358.                                         ground.dst.h = tile_size;
  359.                                         SDL_RenderCopy(renderer, texture, &ground.src, &ground.dst);
  360.                                         }
  361.  
  362.                                         if(level_3[j][i] == 's')
  363.                                         {
  364.  
  365.                                         SDL_Rect src, dst;
  366.  
  367.                                         src.x = 0;
  368.                                         src.y = 0;
  369.                                         src.w = 200;
  370.                                         src.h = 200;
  371.                                         dst.x = i * tile_size + -100;
  372.                                         dst.y = j * tile_size + -100;
  373.                                         dst.w = tile_size * 2;
  374.                                         dst.h = tile_size * 2;
  375.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  376.                                         }
  377.  
  378.                                         if(level_3[j][i] == 'c')
  379.                                         {
  380.  
  381.                                         SDL_Rect src, dst;
  382.  
  383.                                         src.x = 0;
  384.                                         src.y = 200;
  385.                                         src.w = 200;
  386.                                         src.h = 200;
  387.                                         dst.x = i * tile_size;
  388.                                         dst.y = j * tile_size;
  389.                                         dst.w = tile_size;
  390.                                         dst.h = tile_size;
  391.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  392.                                         }
  393.  
  394.                                         if(level_3[j][i] == 'v')
  395.                                         {
  396.  
  397.                                         SDL_Rect src, dst;
  398.  
  399.                                         src.x = 200;
  400.                                         src.y = 0;
  401.                                         src.w = 200;
  402.                                         src.h = 200;
  403.                                         dst.x = i * tile_size;
  404.                                         dst.y = j * tile_size;
  405.                                         dst.w = tile_size;
  406.                                         dst.h = tile_size;
  407.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  408.                                         }
  409.  
  410.                                         if(level_3[j][i] == '.')
  411.                                         {
  412.  
  413.                                         SDL_Rect src, dst;
  414.  
  415.                                         src.x = 600;
  416.                                         src.y = 0;
  417.                                         src.w = 200;
  418.                                         src.h = 200;
  419.                                         dst.x = i * tile_size;
  420.                                         dst.y = j * tile_size;
  421.                                         dst.w = tile_size;
  422.                                         dst.h = tile_size;
  423.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  424.                                         }
  425.  
  426.                                         if(level_3[j][i] == 't')
  427.                                         {
  428.  
  429.                                         SDL_Rect src, dst;
  430.  
  431.                                         src.x = 400;
  432.                                         src.y = 0;
  433.                                         src.w = 200;
  434.                                         src.h = 200;
  435.                                         dst.x = i * tile_size;
  436.                                         dst.y = j * tile_size + 21;
  437.                                         dst.w = tile_size;
  438.                                         dst.h = tile_size;
  439.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  440.                                         }
  441.  
  442.                                         if(level_3[j][i] == 'd')
  443.                                         {
  444.                                         door.dst.x = i * tile_size;
  445.                                         door.dst.y = j * tile_size;
  446.                                         door.dst.w = 100;
  447.                                         door.dst.h = 200;
  448.                                         SDL_RenderCopy(renderer, door_texture, NULL, &door.dst);
  449.  
  450.                                         collide_box3.x = door.dst.x;
  451.                                         collide_box3.y = door.dst.y;
  452.                                         collide_box3.w = door.dst.w;
  453.                                         collide_box3.h = door.dst.h;
  454.  
  455.                                         SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  456.                                         SDL_RenderDrawRect(renderer, &collide_box3);
  457.                                         }
  458.  
  459.                                         if(level_3[j][i] == painting)
  460.                                         {
  461.  
  462.                                         SDL_Rect src, dst;
  463.  
  464.                                         src.x = 400;
  465.                                         src.y = 200;
  466.                                         src.w = 200;
  467.                                         src.h = 200;
  468.                                         dst.x = i * tile_size;
  469.                                         dst.y = j * tile_size;
  470.                                         dst.w = tile_size;
  471.                                         dst.h = tile_size;
  472.                                         SDL_RenderCopy(renderer, interior_textures, &src, &dst);
  473.                                         }
  474.                                 }
  475.                         }
  476.                         break;
  477.                 case 4:
  478.                         for(int i = 0;i<map_width;i++)
  479.                         {
  480.                                 for(int j = 0;j<map_height;j++)
  481.                                 {
  482.                                         if(level_4[j][i] == '#')
  483.                                         {
  484.                                         ground.src.x = 0;
  485.                                         ground.src.y = 0;
  486.                                         ground.src.w = tile_size;
  487.                                         ground.src.h = tile_size;
  488.                                         ground.dst.x = i * tile_size;
  489.                                         ground.dst.y = j * tile_size;
  490.                                         ground.dst.w = tile_size;
  491.                                         ground.dst.h = tile_size;
  492.                                         SDL_RenderCopy(renderer, texture, &ground.src, &ground.dst);
  493.                                         }
  494.  
  495.                                         if(level_4[j][i] == ' ')
  496.                                         {
  497.                                         SDL_Rect src, dst;
  498.  
  499.                                         src.x = 200;
  500.                                         src.y = 0;
  501.                                         src.w = tile_size;
  502.                                         src.h = tile_size;
  503.                                         dst.x = i * tile_size;
  504.                                         dst.y = j * tile_size;
  505.                                         dst.w = tile_size;
  506.                                         dst.h = tile_size;
  507.                                         SDL_RenderCopy(renderer, texture, &src, &dst);
  508.                                         }
  509.                                 }
  510.                         }
  511.                         break;
  512.  
  513.                 default:
  514.                         printf("out of range level\n");
  515.         }
  516. }
  517. Uint32 start = 0;
  518. Uint32 current = 0;
  519. //initialize enemy
  520. void init_enemy()
  521. {
  522.         //frame variables
  523.         enemy.total_frames = 4;
  524.         enemy.frame = 0;
  525.         enemy.frame_width = 100;
  526.         enemy.frame_height = 67;
  527.  
  528.         enemy.e_src.x = 0;
  529.         enemy.e_src.y = 0;
  530.         enemy.e_src.w = enemy.frame_width;
  531.         enemy.e_src.h = enemy.frame_height;
  532.  
  533.         enemy.e_dst.x = 300;
  534.         enemy.e_dst.y = 500;
  535.         enemy.e_dst.w = 100;
  536.         enemy.e_dst.h = 100;
  537. }
  538.  
  539.  
  540. //update enemy position, sprite frames, etc.
  541. void update_enemy()
  542. {
  543.         int duration = 10;
  544.         current = SDL_GetTicks();
  545.         float delta_time = (start - current) / 1000.0;
  546.         start = current;
  547.  
  548.         if(delta_time > duration)
  549.         {
  550.                 enemy.frame = (enemy.frame + 1) % enemy.total_frames;
  551.         }
  552.         enemy.e_src.x = enemy.frame * enemy.frame_width;
  553.  
  554. }
  555.  
  556. //render enemy
  557. void Render_Enemy()
  558. {
  559.         if(enemy.active == 1)
  560.                 SDL_RenderCopy(renderer, enemy_tex, &enemy.e_src, &enemy.e_dst);
  561.  
  562. }
  563.  
  564. //create player tiles
  565. void draw_player(){
  566.         //character tiles
  567.         player.src.x;
  568.         player.src.y;
  569.         player.src.w = 100;
  570.         player.src.h = 100;
  571.  
  572.         player.dst.x;
  573.         player.dst.y;
  574.         player.dst.w = 100;
  575.         player.dst.h = 100;
  576.  
  577.         //copy textures of player sprite to rectangle
  578.         SDL_RenderCopy(renderer, player_tex, &player.src, &player.dst);
  579. }
  580.  
  581. //create a window
  582. void Window(){
  583.         window = SDL_CreateWindow("Call A Exterminator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,Width,Height,0);
  584.         renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
  585. }
  586.  
  587. //check for rectangle collisions
  588. bool collideRect()
  589. {
  590.         if(current_level < 1 && SDL_HasIntersection(&player.dst, &door.dst))
  591.         {
  592.                 printf("Collision with target detected!\n");
  593.                 printf("initializing level %d\n",current_level+1);
  594.                 current_level += 1;
  595.                 player.dst.x = 0;
  596.                 return true;
  597.         }
  598.         if(current_level < 2 && SDL_HasIntersection(&player.dst, &collide_box))
  599.         {
  600.                 printf("Collision with target detected!\n");
  601.                 printf("initializing next level %d\n",current_level+1);
  602.                 current_level += 1;
  603.                 player.dst.x = 0;
  604.                 return true;
  605.         }
  606.         if(current_level < 3 && SDL_HasIntersection(&player.dst, &collide_box2))
  607.         {
  608.                 printf("Collision with target detected!\n");
  609.                 printf("initializing next level %d \n",current_level+1);
  610.                 current_level += 1;
  611.                 player.src.y = 100;
  612.                 return true;
  613.         }
  614.         if(current_level < 4 && SDL_HasIntersection(&player.dst, &collide_box3))
  615.         {
  616.                 printf("Collision with target detected!\n");
  617.                 printf("initializing final level, level %d\n",current_level+1);
  618.                 current_level += 1;
  619.                 return true;
  620.         }
  621.  
  622. }
  623.  
  624. void gravity(){
  625.         int g_force = 10;
  626.         if(player.dst.y  + player.dst.h <= 591){
  627.                 player.dst.y += g_force;
  628.         }
  629. }
  630.  
  631. void clear(){
  632.         SDL_RenderClear(renderer);
  633. }
  634.  
  635. int main(int args, char** argv){
  636.         //initialize SDL2 and SDL_image
  637.         SDL_Init(SDL_INIT_VIDEO);
  638.         if(SDL_Init(SDL_INIT_VIDEO)<0){
  639.                 printf("SDL failed to initialize\n",SDL_GetError());
  640.                 exit(-1);
  641.         }
  642.         IMG_Init(IMG_INIT_PNG & IMG_INIT_JPG);
  643.         if(IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG)<0){                                                  
  644.                 printf("SDL_image failed to initialize\n",SDL_GetError());
  645.                 exit(-1);
  646.         }
  647.  
  648.         Window();
  649.  
  650.         image6 = IMG_Load("assets/bug_0_spritesheet.png");
  651.         image5 = IMG_Load("assets/background2.jpg");
  652.         image4 = IMG_Load("assets/interior_sprites.png");
  653.         image3 = IMG_Load("assets/house_texture.png");
  654.         image2 = IMG_Load("assets/door.png");
  655.         image = IMG_Load("assets/charRight_spritesheet.png");
  656.  
  657.         //house textures                                  
  658.         texture = SDL_CreateTextureFromSurface(renderer,image3);              
  659.         door_texture = SDL_CreateTextureFromSurface(renderer,image2);
  660.         interior_textures = SDL_CreateTextureFromSurface(renderer,image4);
  661.  
  662.         //2nd background photo texture
  663.         background_tex = SDL_CreateTextureFromSurface(renderer,image5);
  664.  
  665.         //player texture creation
  666.         player_tex = SDL_CreateTextureFromSurface(renderer,image);
  667.  
  668.         //enemy texture creation
  669.         enemy_tex = SDL_CreateTextureFromSurface(renderer, image6);
  670.  
  671.         init_enemy();//initialize enemy
  672.  
  673.         bool quit = false;
  674.         while(!quit){
  675.                 start = SDL_GetTicks();
  676.                 //clear frames
  677.                 clear();
  678.                 //clear print output
  679.                 collideRect();
  680.                 //gravity
  681.                 gravity();
  682.                 //draws player and world sprites
  683.                 load_level(current_level);
  684.                 draw_player();
  685.                 //draw enemy
  686.                 update_enemy();
  687.                 Render_Enemy();
  688.                 //draw particles
  689.                 spray_pixels();
  690.                 update_spray();
  691.                 render_spray();
  692.         //shows image on screen
  693.                 SDL_RenderPresent(renderer);
  694.                 while(SDL_PollEvent(&event)!=0){
  695.  
  696.                         //controls
  697.                         if(event.type == SDL_KEYDOWN){
  698.                                 switch(event.key.keysym.sym)
  699.                                 {
  700.                                 case SDLK_a:
  701.                                         player.dir = 0;
  702.  
  703.                                         player.src.y = 100;
  704.                                         player.dst.x -= 10;
  705.                                         player.src.x -= 100;
  706.                                         break;
  707.                                 case SDLK_d:
  708.                                         player.dir = 1;
  709.  
  710.                                         player.src.y = 0;
  711.                                         player.dst.x += 10;
  712.                                         player.src.x += 100;
  713.                                         break;
  714.                                 case SDLK_SPACE:
  715.                                         spray = true;
  716.                                         break;
  717.                                 }
  718.                         }
  719.                         if(event.type == SDL_KEYUP){
  720.                                 switch(event.key.keysym.sym)
  721.                                 {
  722.                                 case SDLK_SPACE:
  723.                                         spray = false;
  724.                                         break;
  725.                                 }
  726.                         }
  727.  
  728.                         //corrects position of player destination square coordinates
  729.                         if(player.src.x > 100)
  730.                                 player.src.x = 0;
  731.                         if(player.src.x < 0)
  732.                                 player.src.x = 100;
  733.                         //checks & limits player position within boundary
  734.                         if(player.dst.x < 0)
  735.                                 player.dst.x += 10;
  736.                         if(player.dst.x > Width - 100)
  737.                                 player.dst.x -= 10;
  738.                         //check for collisions between player and enemy
  739.                         if(enemy.active && player.dst.x >= enemy.e_dst.x - 82.9)
  740.                                 player.dst.x -= 15;
  741.  
  742.                         //exits game
  743.                         if(event.type == SDL_QUIT)
  744.                                 quit = true;
  745.                 }
  746.         }
  747.  
  748.         //clean up
  749.         SDL_FreeSurface(image);SDL_FreeSurface(image2);SDL_FreeSurface(image3);SDL_FreeSurface(image4);SDL_FreeSurface(image5);SDL_FreeSurface(image6);
  750.  
  751.         SDL_DestroyTexture(player_tex);SDL_DestroyTexture(texture);SDL_DestroyTexture(door_texture);SDL_DestroyTexture(interior_textures);SDL_DestroyTexture(background_tex);SDL_DestroyTexture(enemy_tex);
  752.  
  753.         //end of game loop
  754.         SDL_DestroyRenderer(renderer);
  755.         SDL_DestroyWindow(window);
  756.         IMG_Quit();
  757.         SDL_Quit();
  758.  
  759.         return 0;
  760. }
  761.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement