Advertisement
Guest User

Untitled

a guest
Apr 5th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 30.33 KB | Source Code | 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;
  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.  
  518. //create enemy
  519. void _enemy()
  520. {
  521.         enemy.frame = 0; //enemy frame currently
  522.         int frame_total = 4; //total number of frames
  523.  
  524.         Uint32 start_t = SDL_GetTicks(); //time count down before transition to next sprite
  525.         int current_t = 0;
  526.  
  527.         float end_t = start_t - current_t / 1000.0f; //time passed
  528.  
  529.         int duration = 3000; //duration before frame change
  530.  
  531.         if(end_t > duration)
  532.         {
  533.                 enemy.frame++;
  534.                 enemy.frame = enemy.frame % frame_total;
  535.                 end_t = start_t;
  536.                 if(enemy.frame > 1)
  537.                         enemy.frame = 0;
  538.         }
  539.  
  540.         enemy.e_src.x = enemy.frame * 100;
  541.         enemy.e_src.y = 0;
  542.         enemy.e_src.w = 100;
  543.         enemy.e_src.h = 67;
  544.  
  545.         enemy.e_dst.x = door.dst.x - 100;
  546.         enemy.e_dst.y = door.dst.y + 100;
  547.         enemy.e_dst.w = 100;
  548.         enemy.e_dst.h = 100;
  549.  
  550.         if(enemy.active == 1)
  551.                 SDL_RenderCopy(renderer, enemy_tex, &enemy.e_src, &enemy.e_dst);
  552. }
  553.  
  554. //create player tiles
  555. void draw_player(){
  556.         //character tiles
  557.         player.src.x;
  558.         player.src.y;
  559.         player.src.w = 100;
  560.         player.src.h = 100;
  561.  
  562.         player.dst.x;
  563.         player.dst.y;
  564.         player.dst.w = 100;
  565.         player.dst.h = 100;
  566.  
  567.         //copy textures of player sprite to rectangle
  568.         SDL_RenderCopy(renderer, player_tex, &player.src, &player.dst);
  569. }
  570.  
  571. //create a window
  572. void Window(){
  573.         window = SDL_CreateWindow("Call A Exterminator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,Width,Height,0);
  574.         renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
  575. }
  576.  
  577. //check for rectangle collisions
  578. bool collideRect()
  579. {
  580.         if(current_level < 1 && SDL_HasIntersection(&player.dst, &door.dst))
  581.         {
  582.                 printf("Collision with target detected!\n");
  583.                 printf("initializing level %d\n",current_level+1);
  584.                 current_level += 1;
  585.                 player.dst.x = 0;
  586.                 return true;
  587.         }
  588.         if(current_level < 2 && SDL_HasIntersection(&player.dst, &collide_box))
  589.         {
  590.                 printf("Collision with target detected!\n");
  591.                 printf("initializing next level %d\n",current_level+1);
  592.                 current_level += 1;
  593.                 player.dst.x = 0;
  594.                 return true;
  595.         }
  596.         if(current_level < 3 && SDL_HasIntersection(&player.dst, &collide_box2))
  597.         {
  598.                 printf("Collision with target detected!\n");
  599.                 printf("initializing next level %d \n",current_level+1);
  600.                 current_level += 1;
  601.                 player.src.y = 100;
  602.                 return true;
  603.         }
  604.         if(current_level < 4 && SDL_HasIntersection(&player.dst, &collide_box3))
  605.         {
  606.                 printf("Collision with target detected!\n");
  607.                 printf("initializing final level, level %d\n",current_level+1);
  608.                 current_level += 1;
  609.                 return true;
  610.         }
  611.  
  612. }
  613.  
  614. void gravity(){
  615.         int g_force = 10;
  616.         if(player.dst.y  + player.dst.h <= 591){
  617.                 player.dst.y += g_force;
  618.         }
  619. }
  620.  
  621. void clear(){
  622.         SDL_RenderClear(renderer);
  623. }
  624.  
  625. int main(int args, char** argv){
  626.         //initialize SDL2 and SDL_image
  627.         SDL_Init(SDL_INIT_VIDEO);
  628.         if(SDL_Init(SDL_INIT_VIDEO)<0){
  629.                 printf("SDL failed to initialize\n",SDL_GetError());
  630.                 exit(-1);
  631.         }
  632.         IMG_Init(IMG_INIT_PNG & IMG_INIT_JPG);
  633.         if(IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG)<0){                                                  
  634.                 printf("SDL_image failed to initialize\n",SDL_GetError());
  635.                 exit(-1);
  636.         }
  637.  
  638.         Window();
  639.  
  640.         image6 = IMG_Load("assets/bug_0_spritesheet.png");
  641.         image5 = IMG_Load("assets/background2.jpg");
  642.         image4 = IMG_Load("assets/interior_sprites.png");
  643.         image3 = IMG_Load("assets/house_texture.png");
  644.         image2 = IMG_Load("assets/door.png");
  645.         image = IMG_Load("assets/charRight_spritesheet.png");
  646.  
  647.         //house textures                                  
  648.         texture = SDL_CreateTextureFromSurface(renderer,image3);              
  649.         door_texture = SDL_CreateTextureFromSurface(renderer,image2);
  650.         interior_textures = SDL_CreateTextureFromSurface(renderer,image4);
  651.  
  652.         //2nd background photo texture
  653.         background_tex = SDL_CreateTextureFromSurface(renderer,image5);
  654.  
  655.         //player texture creation
  656.         player_tex = SDL_CreateTextureFromSurface(renderer,image);
  657.  
  658.         //enemy texture creation
  659.         enemy_tex = SDL_CreateTextureFromSurface(renderer, image6);
  660.  
  661.         bool quit = false;
  662.         while(!quit){
  663.                 //clear frames
  664.                 clear();
  665.                 //clear print output
  666.                 collideRect();
  667.                 //gravity
  668.                 gravity();
  669.                 //draws player and world sprites
  670.                 load_level(current_level);
  671.                 draw_player();
  672.                 //draw enemy
  673.                 _enemy();
  674.                 //draw particles
  675.                 spray_pixels();
  676.                 update_spray();
  677.                 render_spray();
  678.         //shows image on screen
  679.                 SDL_RenderPresent(renderer);
  680.                 while(SDL_PollEvent(&event)!=0){
  681.  
  682.                         //controls
  683.                         if(event.type == SDL_KEYDOWN){
  684.                                 switch(event.key.keysym.sym)
  685.                                 {
  686.                                 case SDLK_a:
  687.                                         player.dir = 0;
  688.  
  689.                                         player.src.y = 100;
  690.                                         player.dst.x -= 10;
  691.                                         player.src.x -= 100;
  692.                                         break;
  693.                                 case SDLK_d:
  694.                                         player.dir = 1;
  695.  
  696.                                         player.src.y = 0;
  697.                                         player.dst.x += 10;
  698.                                         player.src.x += 100;
  699.                                         break;
  700.                                 case SDLK_SPACE:
  701.                                         spray = true;
  702.                                         break;
  703.                                 }
  704.                         }
  705.                         if(event.type == SDL_KEYUP){
  706.                                 switch(event.key.keysym.sym)
  707.                                 {
  708.                                 case SDLK_SPACE:
  709.                                         spray = false;
  710.                                         break;
  711.                                 }
  712.                         }
  713.  
  714.                         //corrects position of player destination square coordinates
  715.                         if(player.src.x > 100)
  716.                                 player.src.x = 0;
  717.                         if(player.src.x < 0)
  718.                                 player.src.x = 100;
  719.                         //checks & limits player position within boundary
  720.                         if(player.dst.x < 0)
  721.                                 player.dst.x += 10;
  722.                         if(player.dst.x > Width - 100)
  723.                                 player.dst.x -= 10;
  724.                         //check for collisions between player and enemy
  725.                         if(enemy.active && player.dst.x >= enemy.e_dst.x - 82.9)
  726.                                 player.dst.x -= 15;
  727.  
  728.                         //exits game
  729.                         if(event.type == SDL_QUIT)
  730.                                 quit = true;
  731.                 }
  732.         }
  733.  
  734.         //clean up
  735.         SDL_FreeSurface(image);SDL_FreeSurface(image2);SDL_FreeSurface(image3);SDL_FreeSurface(image4);SDL_FreeSurface(image5);SDL_FreeSurface(image6);
  736.  
  737.         SDL_DestroyTexture(player_tex);SDL_DestroyTexture(texture);SDL_DestroyTexture(door_texture);SDL_DestroyTexture(interior_textures);SDL_DestroyTexture(background_tex);SDL_DestroyTexture(enemy_tex);
  738.  
  739.         //end of game loop
  740.         SDL_DestroyRenderer(renderer);
  741.         SDL_DestroyWindow(window);
  742.         IMG_Quit();
  743.         SDL_Quit();
  744.  
  745.         return 0;
  746. }
  747.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement