Advertisement
Kostiggig

Untitled

Apr 27th, 2023
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.65 KB | None | 0 0
  1. #include "task_1.h"
  2.  
  3. struct Ball{
  4.     double radius;
  5.     int x;
  6.     int y;
  7.     char weight[10];
  8.     int weight_as_int;
  9. };
  10.  
  11. vector<Ball> balls;
  12. vector<SDL_Rect> rect_balls;
  13. int curr_score = 0;
  14. int count_of_balls = 0;
  15.  
  16. SDL_Renderer* renderer;
  17. void init_background() {
  18.     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  19.     SDL_RenderClear(renderer);
  20.     SDL_RenderPresent(renderer);
  21. }
  22.  
  23. void init_music(Mix_Chunk* hit_the_ball) {
  24.     Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
  25.     hit_the_ball = Mix_LoadWAV("baket_hit_the_ball.wav");
  26.     Mix_Music* music = Mix_LoadMUS("background.mp3");
  27.     Mix_VolumeMusic(BACKGROUND_MIX_VOLUME);
  28.     Mix_VolumeChunk(hit_the_ball, BALL_TAP_MIX_VOLUME);
  29.     Mix_PlayMusic(music, -1);
  30. }
  31.  
  32. TTF_Font* my_font;
  33. TTF_Font* font_on_balls;
  34.  
  35. void init_font() {
  36.     TTF_Init();
  37.     my_font = TTF_OpenFont("score_font.ttf", 100);
  38.     font_on_balls = TTF_OpenFont("score_font.ttf", 20);
  39. }
  40.  
  41. SDL_Texture* get_text_for_ball(char* points, TTF_Font* font_on_balls, SDL_Color text_color)
  42. {
  43.     SDL_Surface*  textOnBallSurface = TTF_RenderText_Blended(font_on_balls, points, text_color);
  44.     SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textOnBallSurface);
  45.     SDL_FreeSurface(textOnBallSurface);
  46.     return texture;
  47. }
  48.  
  49. SDL_Texture* get_text_texture(
  50.     char* text,
  51.     TTF_Font* font,
  52.     SDL_Color text_color,
  53.     SDL_Color background_color
  54.     )
  55. {
  56.     SDL_Surface* textSurface = TTF_RenderText_Shaded(font, text, text_color, background_color);
  57.     SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);
  58.     SDL_FreeSurface(textSurface);
  59.     return texture;
  60. }
  61.  
  62. void draw_balls(SDL_Texture* texture)
  63. {
  64.     for (int i = 0; i < balls.size(); i++)
  65.     {
  66.         if (rect_balls[i].w == 0) continue;
  67.         SDL_RenderCopy(renderer, texture, NULL, &rect_balls[i]);
  68.         SDL_Texture* ball_texture = get_text_for_ball(balls[i].weight, font_on_balls, { 0, 0, 0 });
  69.         SDL_RenderCopy(renderer, ball_texture, NULL, &rect_balls[i]);
  70.     }
  71. }
  72.  
  73. void draw_text(SDL_Texture* texture, SDL_Rect rect)
  74. {
  75.     SDL_RenderCopy(renderer, texture, NULL, &rect);
  76. }
  77.  
  78. bool intersected(Ball ball) {
  79.     for (int i = 0; i < balls.size(); i++) {
  80.         double dx = balls[i].x - ball.x;
  81.         double dy = balls[i].y - ball.y;
  82.         double distance = sqrt(dx * dx + dy * dy);
  83.         if (distance < balls[i].radius + ball.radius) return true;
  84.     }
  85.     return false;
  86. }
  87.  
  88. SDL_Texture* textTexture, * text_on_balls, * ballTexture;
  89.  
  90. void update_score() {
  91.     char text[10];
  92.     _itoa_s(curr_score, text, 10);
  93.     textTexture = get_text_texture(text, my_font, { 0, 0, 0 }, { 255, 255, 255 });
  94. }
  95.  
  96. void init_texture() {
  97.     update_score();
  98.     ballTexture = IMG_LoadTexture(renderer, "ball.png");
  99. }
  100.  
  101. void rerender() {
  102.     SDL_RenderClear(renderer);
  103.  
  104.     draw_balls(ballTexture);
  105.     SDL_Rect score_rect = { SCORE_X, SCORE_Y, SCORE_WIDTH, SCORE_HEIGHT};
  106.     update_score();
  107.     draw_text(textTexture, score_rect);
  108.     SDL_RenderPresent(renderer);
  109. }
  110.  
  111. void generate_random_ball() {
  112.     int radius = rand() % (BALL_RADIUS_MAX - BALL_RADIUS_MIN + 1) + BALL_RADIUS_MIN;
  113.  
  114.     int min_x = SCORE_X + 2 * radius;
  115.     int max_x = SCREEN_WIDTH - (2 * radius) - DEVIATION_FROM_EDGE_IN_PX;
  116.  
  117.     int min_y = SCORE_Y + 2 * radius;
  118.     int max_y = SCREEN_HEIGHT - (2 * radius) - DEVIATION_FROM_EDGE_IN_PX;
  119.  
  120.     int rand_x = rand() % (max_x - min_x + 1) + min_x;
  121.     int rand_y = rand() % (max_y - min_y + 1) + min_y;
  122.  
  123.    
  124.     Ball ball;
  125.     ball.radius = radius;
  126.     ball.x = rand_x;
  127.     ball.y = rand_y;
  128.     ball.weight_as_int = (int) radius / 10;
  129.     _itoa_s((int)radius / 10, ball.weight, 10, 10);
  130.    
  131.     SDL_Rect rect = { rand_x, rand_y, radius, radius };
  132.  
  133.     if (!intersected(ball)) {
  134.         balls.push_back(ball);
  135.         rect_balls.push_back(rect);
  136.     }
  137. }
  138.  
  139. bool hits_any_ball (int mouse_x, int mouse_y, int *index) {
  140.     for (int i = 0; i < balls.size(); i++) {
  141.         int ball_x = balls[i].x;
  142.         int ball_y = balls[i].y;
  143.         int ball_radius = balls[i].radius;
  144.         int distance_squared = (mouse_x - ball_x) * (mouse_x - ball_x) + (mouse_y - ball_y) * (mouse_y - ball_y);
  145.         int radius_squared = ball_radius * ball_radius;
  146.         if (distance_squared <= radius_squared) {
  147.             (*index) = i;
  148.             return true;
  149.         }
  150.     }
  151.    
  152.     return false;
  153. }
  154.  
  155. void update_coordinates(int index) {
  156.     Ball ball = balls[index];
  157.     int min_x = SCORE_X + 2 * ball.radius;
  158.     int max_x = SCREEN_WIDTH - (2 * ball.radius) - DEVIATION_FROM_EDGE_IN_PX;
  159.  
  160.     int min_y = SCORE_Y + 2 * ball.radius;
  161.     int max_y = SCREEN_HEIGHT - (2 * ball.radius) - DEVIATION_FROM_EDGE_IN_PX;
  162.  
  163.     int rand_x = rand() % (max_x - min_x + 1) + min_x;
  164.     int rand_y = rand() % (max_y - min_y + 1) + min_y;
  165.     ball.x = rand_x;
  166.     ball.y= rand_y;
  167.     if (!intersected(ball)) {
  168.         balls[index] = ball;
  169.         SDL_Rect rect = rect_balls[index];
  170.         rect.x = rand_x;
  171.         rect.y = rand_y;
  172.         rect_balls[index] = rect;
  173.         return;
  174.     }
  175.     else {
  176.         update_coordinates(index);
  177.     }
  178. }
  179.  
  180. Uint32 timerCallback(Uint32 interval, void* param) {
  181.     Uint32 randomInterval = rand() % (MAX_DELAY_BALLS_VISIBILITY - MIN_DELAY_BALLS_VISIBILITY + 1) + MIN_DELAY_BALLS_VISIBILITY;
  182.  
  183.     if (!balls.empty()) {
  184.         int max_index_ball = balls.size() - 1;
  185.         int rand_ball_index = rand() % (max_index_ball - 0 + 1) + 0;
  186.         update_coordinates(rand_ball_index);
  187.         rerender();
  188.     }
  189.  
  190.     return randomInterval;
  191. }
  192.  
  193. void task_1(int count) {
  194.     srand(time(0));
  195.     count_of_balls = count;
  196.     SDL_Init(SDL_INIT_EVERYTHING);
  197.     SDL_Window* window = SDL_CreateWindow(
  198.         "Hit the balls",
  199.         SDL_WINDOWPOS_CENTERED,
  200.         SDL_WINDOWPOS_CENTERED,
  201.         SCREEN_WIDTH,
  202.         SCREEN_HEIGHT,
  203.         SDL_WINDOW_SHOWN
  204.     );
  205.  
  206.     Mix_Chunk* hit_the_ball = NULL;
  207.    
  208.     renderer = SDL_CreateRenderer(window, -1, 0);
  209.  
  210.     init_background();
  211.     init_music(hit_the_ball);
  212.     init_font();
  213.     init_texture();
  214.    
  215.     SDL_TimerID timerID = SDL_AddTimer(0, timerCallback, NULL);
  216.  
  217.     while (balls.size() < count_of_balls) {
  218.         generate_random_ball();
  219.     }
  220.  
  221.     rerender();
  222.  
  223.    
  224.     bool exit = false;
  225.     SDL_Event event;
  226.     while (!exit && !balls.empty()) {
  227.         SDL_PollEvent(&event);
  228.         if (event.type == SDL_QUIT) exit = true;
  229.         if (event.type == SDL_MOUSEBUTTONDOWN) {
  230.             if (event.button.button == SDL_BUTTON_LEFT) {
  231.                 int x, y;
  232.                 SDL_GetMouseState(&x, &y);
  233.                 int index_hit_ball = -1;
  234.                 if (hits_any_ball(x, y, &index_hit_ball)) {
  235.                     Mix_PlayChannel(-1, hit_the_ball, 0);
  236.                     Ball hit_ball = balls[index_hit_ball];
  237.                     balls.erase(balls.begin() + index_hit_ball);
  238.                     rect_balls.erase(rect_balls.begin() + index_hit_ball);
  239.                     curr_score += hit_ball.weight_as_int;
  240.                     rerender();
  241.                 }
  242.             }
  243.         }
  244.     }
  245.  
  246.     SDL_Delay(2000);
  247.     SDL_RemoveTimer(timerID);
  248.     SDL_DestroyTexture(textTexture);
  249.     SDL_DestroyTexture(text_on_balls);
  250.     SDL_DestroyTexture(ballTexture);
  251.     TTF_CloseFont(my_font);
  252.     TTF_CloseFont(font_on_balls);
  253.     Mix_CloseAudio();
  254.     TTF_Quit();
  255.     SDL_DestroyRenderer(renderer);
  256.     SDL_DestroyWindow(window);
  257.     SDL_Quit();
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement