Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "task_1.h"
- struct Ball{
- double radius;
- int x;
- int y;
- char weight[10];
- int weight_as_int;
- };
- vector<Ball> balls;
- vector<SDL_Rect> rect_balls;
- int curr_score = 0;
- int count_of_balls = 0;
- SDL_Renderer* renderer;
- void init_background() {
- SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
- SDL_RenderClear(renderer);
- SDL_RenderPresent(renderer);
- }
- void init_music(Mix_Chunk* hit_the_ball) {
- Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
- hit_the_ball = Mix_LoadWAV("baket_hit_the_ball.wav");
- Mix_Music* music = Mix_LoadMUS("background.mp3");
- Mix_VolumeMusic(BACKGROUND_MIX_VOLUME);
- Mix_VolumeChunk(hit_the_ball, BALL_TAP_MIX_VOLUME);
- Mix_PlayMusic(music, -1);
- }
- TTF_Font* my_font;
- TTF_Font* font_on_balls;
- void init_font() {
- TTF_Init();
- my_font = TTF_OpenFont("score_font.ttf", 100);
- font_on_balls = TTF_OpenFont("score_font.ttf", 20);
- }
- SDL_Texture* get_text_for_ball(char* points, TTF_Font* font_on_balls, SDL_Color text_color)
- {
- SDL_Surface* textOnBallSurface = TTF_RenderText_Blended(font_on_balls, points, text_color);
- SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textOnBallSurface);
- SDL_FreeSurface(textOnBallSurface);
- return texture;
- }
- SDL_Texture* get_text_texture(
- char* text,
- TTF_Font* font,
- SDL_Color text_color,
- SDL_Color background_color
- )
- {
- SDL_Surface* textSurface = TTF_RenderText_Shaded(font, text, text_color, background_color);
- SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, textSurface);
- SDL_FreeSurface(textSurface);
- return texture;
- }
- void draw_balls(SDL_Texture* texture)
- {
- for (int i = 0; i < balls.size(); i++)
- {
- if (rect_balls[i].w == 0) continue;
- SDL_RenderCopy(renderer, texture, NULL, &rect_balls[i]);
- SDL_Texture* ball_texture = get_text_for_ball(balls[i].weight, font_on_balls, { 0, 0, 0 });
- SDL_RenderCopy(renderer, ball_texture, NULL, &rect_balls[i]);
- }
- }
- void draw_text(SDL_Texture* texture, SDL_Rect rect)
- {
- SDL_RenderCopy(renderer, texture, NULL, &rect);
- }
- bool intersected(Ball ball) {
- for (int i = 0; i < balls.size(); i++) {
- double dx = balls[i].x - ball.x;
- double dy = balls[i].y - ball.y;
- double distance = sqrt(dx * dx + dy * dy);
- if (distance < balls[i].radius + ball.radius) return true;
- }
- return false;
- }
- SDL_Texture* textTexture, * text_on_balls, * ballTexture;
- void update_score() {
- char text[10];
- _itoa_s(curr_score, text, 10);
- textTexture = get_text_texture(text, my_font, { 0, 0, 0 }, { 255, 255, 255 });
- }
- void init_texture() {
- update_score();
- ballTexture = IMG_LoadTexture(renderer, "ball.png");
- }
- void rerender() {
- SDL_RenderClear(renderer);
- draw_balls(ballTexture);
- SDL_Rect score_rect = { SCORE_X, SCORE_Y, SCORE_WIDTH, SCORE_HEIGHT};
- update_score();
- draw_text(textTexture, score_rect);
- SDL_RenderPresent(renderer);
- }
- void generate_random_ball() {
- int radius = rand() % (BALL_RADIUS_MAX - BALL_RADIUS_MIN + 1) + BALL_RADIUS_MIN;
- int min_x = SCORE_X + 2 * radius;
- int max_x = SCREEN_WIDTH - (2 * radius) - DEVIATION_FROM_EDGE_IN_PX;
- int min_y = SCORE_Y + 2 * radius;
- int max_y = SCREEN_HEIGHT - (2 * radius) - DEVIATION_FROM_EDGE_IN_PX;
- int rand_x = rand() % (max_x - min_x + 1) + min_x;
- int rand_y = rand() % (max_y - min_y + 1) + min_y;
- Ball ball;
- ball.radius = radius;
- ball.x = rand_x;
- ball.y = rand_y;
- ball.weight_as_int = (int) radius / 10;
- _itoa_s((int)radius / 10, ball.weight, 10, 10);
- SDL_Rect rect = { rand_x, rand_y, radius, radius };
- if (!intersected(ball)) {
- balls.push_back(ball);
- rect_balls.push_back(rect);
- }
- }
- bool hits_any_ball (int mouse_x, int mouse_y, int *index) {
- for (int i = 0; i < balls.size(); i++) {
- int ball_x = balls[i].x;
- int ball_y = balls[i].y;
- int ball_radius = balls[i].radius;
- int distance_squared = (mouse_x - ball_x) * (mouse_x - ball_x) + (mouse_y - ball_y) * (mouse_y - ball_y);
- int radius_squared = ball_radius * ball_radius;
- if (distance_squared <= radius_squared) {
- (*index) = i;
- return true;
- }
- }
- return false;
- }
- void update_coordinates(int index) {
- Ball ball = balls[index];
- int min_x = SCORE_X + 2 * ball.radius;
- int max_x = SCREEN_WIDTH - (2 * ball.radius) - DEVIATION_FROM_EDGE_IN_PX;
- int min_y = SCORE_Y + 2 * ball.radius;
- int max_y = SCREEN_HEIGHT - (2 * ball.radius) - DEVIATION_FROM_EDGE_IN_PX;
- int rand_x = rand() % (max_x - min_x + 1) + min_x;
- int rand_y = rand() % (max_y - min_y + 1) + min_y;
- ball.x = rand_x;
- ball.y= rand_y;
- if (!intersected(ball)) {
- balls[index] = ball;
- SDL_Rect rect = rect_balls[index];
- rect.x = rand_x;
- rect.y = rand_y;
- rect_balls[index] = rect;
- return;
- }
- else {
- update_coordinates(index);
- }
- }
- Uint32 timerCallback(Uint32 interval, void* param) {
- Uint32 randomInterval = rand() % (MAX_DELAY_BALLS_VISIBILITY - MIN_DELAY_BALLS_VISIBILITY + 1) + MIN_DELAY_BALLS_VISIBILITY;
- if (!balls.empty()) {
- int max_index_ball = balls.size() - 1;
- int rand_ball_index = rand() % (max_index_ball - 0 + 1) + 0;
- update_coordinates(rand_ball_index);
- rerender();
- }
- return randomInterval;
- }
- void task_1(int count) {
- srand(time(0));
- count_of_balls = count;
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_Window* window = SDL_CreateWindow(
- "Hit the balls",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- SCREEN_WIDTH,
- SCREEN_HEIGHT,
- SDL_WINDOW_SHOWN
- );
- Mix_Chunk* hit_the_ball = NULL;
- renderer = SDL_CreateRenderer(window, -1, 0);
- init_background();
- init_music(hit_the_ball);
- init_font();
- init_texture();
- SDL_TimerID timerID = SDL_AddTimer(0, timerCallback, NULL);
- while (balls.size() < count_of_balls) {
- generate_random_ball();
- }
- rerender();
- bool exit = false;
- SDL_Event event;
- while (!exit && !balls.empty()) {
- SDL_PollEvent(&event);
- if (event.type == SDL_QUIT) exit = true;
- if (event.type == SDL_MOUSEBUTTONDOWN) {
- if (event.button.button == SDL_BUTTON_LEFT) {
- int x, y;
- SDL_GetMouseState(&x, &y);
- int index_hit_ball = -1;
- if (hits_any_ball(x, y, &index_hit_ball)) {
- Mix_PlayChannel(-1, hit_the_ball, 0);
- Ball hit_ball = balls[index_hit_ball];
- balls.erase(balls.begin() + index_hit_ball);
- rect_balls.erase(rect_balls.begin() + index_hit_ball);
- curr_score += hit_ball.weight_as_int;
- rerender();
- }
- }
- }
- }
- SDL_Delay(2000);
- SDL_RemoveTimer(timerID);
- SDL_DestroyTexture(textTexture);
- SDL_DestroyTexture(text_on_balls);
- SDL_DestroyTexture(ballTexture);
- TTF_CloseFont(my_font);
- TTF_CloseFont(font_on_balls);
- Mix_CloseAudio();
- TTF_Quit();
- SDL_DestroyRenderer(renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement