-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.c
More file actions
87 lines (66 loc) · 1.94 KB
/
ball.c
File metadata and controls
87 lines (66 loc) · 1.94 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "ball.h"
//#include "math.h"
#include "rand.h"
#include "gl.h"
#include "printf.h"
#include "sine.h"
extern int SCREENWIDTH; //width of the screen in pixels
extern int SCREENHEIGHT; //height of the screen in pixels
extern color_t BALL; //color of the ball
extern color_t BACKGROUND; //background color
int max_speed =4; //maximum speed of ball in either x or y direction
int radius = 8; //radius of the ball
//need a helper function to geenrate a random number
//figure out a generally non biased way to do this
//In meantime, I'll just use mod
//generates a random number between min and max
static int random_randrange(int min, int max)
{
return (rand() % (max - min + 1)) + min;
}
ball_t ball_init(void)
{
ball_t ball;
//place ball at center
ball.x = SCREENWIDTH / 2;
ball.y = SCREENHEIGHT / 2;
//int angle = random_randrange(0, 45);
ball.vx = max_speed;
ball.vy = max_speed + 1;
ball.radius = radius;
return ball;
}
ball_t reset_ball(void)
{
ball_t ball = ball_init();
draw_ball(ball.x, ball.y);
return ball;
}
void draw_ball(int x, int y)
{
//draws a red ball centered at x, y
//ball is currently a square --> need to figure out circle logic
gl_draw_rect(x - radius, y - radius, radius*2, radius*2, BALL);
}
void clear_ball(int x, int y) {
//draws a ball the same color as backgroung, effectively clearing the ball
gl_draw_rect(x - radius, y - radius, radius*2, radius*2, BACKGROUND);
}
ball_t check_ball_edges(ball_t ball)
{
//if ball hits top, change ball direction
if ((ball.y < ball.radius) || (ball.y >= (SCREENHEIGHT - ball.radius))) {
ball.vy *= -1;
}
return ball;
}
bool ball_check_hit_right(ball_t ball)
{
if (ball.x >= SCREENWIDTH - radius) return true;
return false;
}
bool ball_check_hit_left(ball_t ball)
{
if (ball.x < radius) return true;
return false;
}