This repository has been archived by the owner on Dec 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.odin
182 lines (149 loc) · 4.9 KB
/
day5.odin
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import "core:fmt"
import "core:math"
import "core:math/linalg"
import "core:math/rand"
import rl "vendor:raylib"
Game_State :: struct {
window_size: rl.Vector2,
paddle: rl.Rectangle,
ai_paddle: rl.Rectangle,
paddle_speed: f32,
ball: rl.Rectangle,
ball_dir: rl.Vector2,
ball_speed: f32,
ai_target_y: f32,
ai_reaction_delay: f32,
ai_reaction_timer: f32,
score_player: int,
score_cpu: int,
boost_timer: f32,
}
main :: proc() {
gs := Game_State {
window_size = {1280, 720},
paddle = {width = 30, height = 80},
ai_paddle = {width = 30, height = 80},
paddle_speed = 10,
ball = {width = 30, height = 30},
ball_speed = 10,
ai_reaction_delay = 0.1,
}
reset(&gs)
using gs
rl.InitWindow(i32(window_size.x), i32(window_size.y), "Pong")
rl.SetTargetFPS(60)
rl.InitAudioDevice()
defer rl.CloseAudioDevice()
sfx_hit := rl.LoadSound("hit.wav")
sfx_win := rl.LoadSound("win.wav")
sfx_lose := rl.LoadSound("lose.wav")
for !rl.WindowShouldClose() {
delta := rl.GetFrameTime()
boost_timer -= delta
// Player movement
if rl.IsKeyDown(.UP) {
paddle.y -= paddle_speed
}
if rl.IsKeyDown(.DOWN) {
paddle.y += paddle_speed
}
if rl.IsKeyPressed(.SPACE) {
if boost_timer < 0 {
boost_timer = 0.2
}
}
paddle.y = linalg.clamp(paddle.y, 0, window_size.y - paddle.height)
// AI movement
// increase timer by time between last frame and this one
ai_reaction_timer += delta
// if the timer is done:
if ai_reaction_timer >= ai_reaction_delay {
// reset the timer
ai_reaction_timer = 0
// use ball from last frame for extra delay
ball_mid := ball.y + ball.height / 2
// if the ball is heading left
if ball_dir.x < 0 {
// set the target to the ball
ai_target_y = ball_mid - ai_paddle.height / 2
// add or subtract 0-20 to add inaccuracy
ai_target_y += rand.float32_range(-20, 20)
} else {
// set the target to screen middle
ai_target_y = window_size.y / 2 - ai_paddle.height / 2
}
}
// calculate the distance between paddle and target
target_diff := ai_target_y - ai_paddle.y
// move either paddle_speed distance or less
// won't bounce around so much
ai_paddle.y += linalg.clamp(target_diff, -paddle_speed, paddle_speed) * 0.65
// clamp to window_size
ai_paddle.y = linalg.clamp(ai_paddle.y, 0, window_size.y - ai_paddle.height)
next_ball_rect := ball
next_ball_rect.x += ball_speed * ball_dir.x
next_ball_rect.y += ball_speed * ball_dir.y
if next_ball_rect.y >= 720 - ball.height || next_ball_rect.y <= 0 {
ball_dir.y *= -1
}
if next_ball_rect.x >= window_size.x - ball.width {
score_cpu += 1
rl.PlaySound(sfx_lose)
reset(&gs)
}
if next_ball_rect.x < 0 {
score_player += 1
rl.PlaySound(sfx_win)
reset(&gs)
}
if new_dir, ok := ball_dir_calculate(next_ball_rect, paddle); ok {
if boost_timer > 0 {
d := 1 + boost_timer / 0.2
new_dir *= d
}
ball_dir = new_dir
rl.PlaySound(sfx_hit)
} else if new_dir, ok := ball_dir_calculate(next_ball_rect, ai_paddle); ok {
ball_dir = new_dir
rl.PlaySound(sfx_hit)
}
ball.x += ball_speed * ball_dir.x
ball.y += ball_speed * ball_dir.y
rl.BeginDrawing()
rl.ClearBackground(rl.BLACK)
if boost_timer > 0 {
rl.DrawRectangleRec(paddle, {u8(255 * (0.2 / boost_timer)), 255, 255, 255})
} else {
rl.DrawRectangleRec(paddle, rl.WHITE)
}
rl.DrawRectangleRec(ai_paddle, rl.WHITE)
rl.DrawRectangleRec(ball, {255, u8(255 - 255 / linalg.length(ball_dir)), 0, 255})
rl.DrawText(fmt.ctprintf("{}", score_cpu), 12, 12, 32, rl.WHITE)
rl.DrawText(fmt.ctprintf("{}", score_player), i32(window_size.x) - 28, 12, 32, rl.WHITE)
rl.EndDrawing()
free_all(context.temp_allocator)
}
}
ball_dir_calculate :: proc(ball: rl.Rectangle, paddle: rl.Rectangle) -> (rl.Vector2, bool) {
if rl.CheckCollisionRecs(ball, paddle) {
ball_center := rl.Vector2{ball.x + ball.width / 2, ball.y + ball.height / 2}
paddle_center := rl.Vector2{paddle.x + paddle.width / 2, paddle.y + paddle.height / 2}
return linalg.normalize0(ball_center - paddle_center), true
}
return {}, false
}
reset :: proc(using gs: ^Game_State) {
angle := rand.float32_range(-45, 46)
if rand.int_max(100) % 2 == 0 do angle += 180
r := math.to_radians(angle)
ball_dir.x = math.cos(r)
ball_dir.y = math.sin(r)
ball.x = window_size.x / 2 - ball.width / 2
ball.y = window_size.y / 2 - ball.height / 2
paddle_margin: f32 = 50
paddle.x = window_size.x - (paddle.width + paddle_margin)
paddle.y = window_size.y / 2 - paddle.height / 2
ai_paddle.x = paddle_margin
ai_paddle.y = window_size.y / 2 - ai_paddle.height / 2
}