-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpongball.c
51 lines (42 loc) · 1.26 KB
/
pongball.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdlib.h>
#include <math.h>
#include "pongball.h"
#include "racket.h"
#include "util.h"
const float RACKET_HITBACK_MAXANGLE = 85.0f * M_PI / 180.0f;
void pongball_reset(struct PongBall *ball)
{
ball->x = ball->within->width / 2 - (ball->size) / 2.0f;
ball->y = ball->within->height / 2 - (ball->size) / 2.0f;
pongball_toggle(ball, RACKET_HITBACK_MAXANGLE);
}
void pongball_score(struct PongBall *ball, int *benefit)
{
++*benefit;
pongball_reset(ball);
}
void pongball_render(SDL_Renderer *r, const struct PongBall *ball)
{
SDL_Rect ball_rect;
ball_rect = (SDL_Rect){
.x = ball->x,
.y = ball->y,
.w = ball->size,
.h = ball->size};
SDL_RenderFillRect(r, &ball_rect);
}
void pongball_move(struct PongBall *ball, Uint32 delta_ms)
{
ball->x += ball->dx * delta_ms * ball->speed_ms;
ball->y += ball->dy * delta_ms * ball->speed_ms;
ball->x = fclamp0(ball->x, ball->within->width);
ball->y = fclamp0(ball->y, ball->within->height - ball->size);
}
void pongball_toggle(struct PongBall *ball, const float max_angle)
{
float angle;
angle = rand() / (float)RAND_MAX * max_angle * 2.0f -
max_angle;
ball->dy = sinf(angle);
ball->dx = ball->dx < 0 ? cosf(angle) : -cosf(angle);
}