-
Notifications
You must be signed in to change notification settings - Fork 0
/
bouncing_ball.c
65 lines (51 loc) · 1.53 KB
/
bouncing_ball.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
52
53
54
55
56
57
58
59
60
61
62
63
#include <simple2d.h>
#include <stdio.h>
//constants to use for ball direction and boolean
#define LEFT -1
#define RIGHT 1
#define UP -1
#define DOWN 1
#define FALSE 0
#define TRUE 1
const int screen_width = 640;
const int screen_height = 480;
const int ball_r = 30;
const int speed = 5;
int ball_y = 250;
int ball_x = 250;
int dir_y = 1;
int dir_x = 1;
bool hit_x,hit_y = FALSE;
S2D_Sound *snd = NULL;
void update() {
if(ball_x == (screen_width-ball_r)){ //hit right side
dir_x = LEFT; //bounce back by going left
hit_x = TRUE;
} else if(ball_x == ball_r) { //hit left side
dir_x = RIGHT; //bounce back right
hit_x = TRUE;
} else {hit_x = FALSE;}
ball_x = ball_x + (speed * dir_x); //position ball x pos
if(ball_y == (screen_height-ball_r)){ //hit bottom
dir_y = UP; //bounce up
hit_y = TRUE;
} else if(ball_y == ball_r){ //hit top
dir_y = DOWN; //bounce down
hit_y = TRUE;
} else {hit_y = FALSE;}
ball_y = ball_y + (speed * dir_y); //position ball y pos
}
void render() {
S2D_DrawCircle(ball_x, ball_y, ball_r, 50, 1, 0, 0, 1);
if(hit_x||hit_y){
S2D_PlaySound(snd);//play bounce sound
}
}
int main() {
snd = S2D_CreateSound("bounce.wav"); //initialise sound
S2D_Window *window = S2D_CreateWindow(
"Hello Triangle", screen_width, screen_height, update, render, 0);//init the window
S2D_Show(window);//show the window
S2D_FreeSound(snd); //free sound from mem
return 0;
}