-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.go
293 lines (261 loc) · 8.45 KB
/
pong.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"fmt"
"image/color"
"math"
"math/rand"
"strconv"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"golang.org/x/image/font/basicfont"
)
func main() {
pixelgl.Run(run)
}
const (
friction = 0.65
fps = 60
paddleAccel = 0.7
windowWidth = 420
windowHeight = 600
maxBounceAngle = math.Pi * 5 / 12
)
type paddle struct {
xPos float64
yPos float64
yVel float64
maxVel float64
upAccel bool
downAccel bool
width int
height int
}
type ball struct {
xPos float64
yPos float64
radius float64
xVel float64
yVel float64
}
type pongGame struct {
player1 paddle
player2 paddle
gameBall ball
paused bool
p1Score int
p2Score int
}
func createPaddle(player int) paddle {
outputPaddle := paddle{
yPos: 260,
width: 10,
height: 80,
yVel: 0,
}
if player == 1 {
outputPaddle.xPos = 20
outputPaddle.maxVel = 10
} else {
outputPaddle.xPos = 390
outputPaddle.maxVel = 4
}
return outputPaddle
}
func (b ball) draw(win *pixelgl.Window) {
imd := imdraw.New(nil)
imd.Color = pixel.RGB(1, 1, 1)
imd.Push(pixel.V(b.xPos, b.yPos))
imd.Circle(b.radius, 0)
imd.Draw(win)
}
func (p paddle) draw(win *pixelgl.Window) {
imd := imdraw.New(nil)
imd.Color = pixel.RGB(1, 1, 1)
imd.Push(pixel.V(p.xPos, p.yPos), pixel.V(p.xPos+float64(p.width), p.yPos+float64(p.height)))
imd.Rectangle(0)
imd.Draw(win)
}
func run() {
cfg := pixelgl.WindowConfig{
Title: "Pong",
Bounds: pixel.R(0, 0, windowWidth, windowHeight),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
for !win.Closed() {
game := pongGame{}
game.p1Score = 0
game.p2Score = 0
resetGame(&game)
for game.p1Score < 10 && game.p2Score < 10 {
prevTime := time.Now()
if win.Closed() {
break
}
handleIO(win, &game)
if !game.paused {
updateGame(win, &game)
}
win.Clear(color.Black)
atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
if game.p1Score == 0 && game.p2Score == 0 && game.paused {
txt := text.New(pixel.V(windowWidth/2, windowHeight), atlas)
txt.Dot.Y -= txt.BoundsOf("To play use the up and down arrow keys").H()
txt.Dot.X -= txt.BoundsOf("To play use the up and down arrow keys").W() / 2
fmt.Fprintf(txt, "To play use the up and down arrow keys")
txt.Dot.Y -= txt.BoundsOf("First player to 10 point wins").H()
txt.Dot.X = windowWidth/2 - txt.BoundsOf("First player to 10 point wins").W()/2
fmt.Fprintf(txt, "First player to 10 point wins")
txt.Draw(win, pixel.IM.Scaled(txt.Orig, 1.5))
}
// Draws the player scores onto the window.
basicTxt := text.New(pixel.V(0, 0), atlas)
fmt.Fprintln(basicTxt, "P1", game.p1Score)
basicTxt.Draw(win, pixel.IM.Scaled(basicTxt.Orig, 4))
basicTxt = text.New(pixel.V(windowWidth, 0), atlas)
basicTxt.Dot.X -= basicTxt.BoundsOf("P2 " + strconv.Itoa(game.p2Score)).W()
fmt.Fprintln(basicTxt, "P2", game.p2Score)
basicTxt.Draw(win, pixel.IM.Scaled(basicTxt.Orig, 4))
// Draws the game onto the window.
game.gameBall.draw(win)
game.player1.draw(win)
game.player2.draw(win)
if game.p1Score == 10 {
txt := text.New(pixel.V(windowWidth/2, windowHeight), atlas)
txt.Dot.Y -= txt.BoundsOf("Player 1 wins").H()
txt.Dot.X -= txt.BoundsOf("Player 1 wins").W() / 2
fmt.Fprintf(txt, "Player 1 wins")
txt.Draw(win, pixel.IM.Scaled(txt.Orig, 4))
win.Update()
time.Sleep(time.Second * 2)
} else if game.p2Score == 10 {
fmt.Println("nani")
txt := text.New(pixel.V(windowWidth/2, windowHeight), atlas)
txt.Dot.Y -= txt.BoundsOf("Player 2 wins").H()
txt.Dot.X -= txt.BoundsOf("Player 2 wins").W() / 2
fmt.Fprintf(txt, "Player 2 wins")
txt.Draw(win, pixel.IM.Scaled(txt.Orig, 4))
win.Update()
time.Sleep(time.Second * 2)
} else {
win.Update()
}
dt := time.Now().Sub(prevTime)
delay := time.Second/fps - dt
if delay > 0 {
gameTimer := time.NewTicker(delay)
<-gameTimer.C
}
}
}
}
func handleIO(win *pixelgl.Window, game *pongGame) {
if win.JustPressed(pixelgl.KeyUp) {
game.player1.upAccel = true
game.paused = false
} else if win.JustPressed(pixelgl.KeyDown) {
game.player1.downAccel = true
game.paused = false
}
if win.JustReleased(pixelgl.KeyUp) {
game.player1.upAccel = false
} else if win.JustReleased(pixelgl.KeyDown) {
game.player1.downAccel = false
}
}
func updateGame(win *pixelgl.Window, game *pongGame) {
// Handles player 1 paddle movements.
if game.player1.upAccel {
game.player1.yVel += paddleAccel
} else if game.player1.downAccel {
game.player1.yVel -= paddleAccel
} else if (!game.player1.upAccel) && (!game.player1.downAccel) {
game.player1.yVel *= friction
}
// Stops player 1 paddle from going over its max velocity.
if game.player1.yVel > game.player1.maxVel {
game.player1.yVel = game.player1.maxVel
} else if game.player1.yVel < -game.player1.maxVel {
game.player1.yVel = -game.player1.maxVel
}
// Stops player 1 paddle from going off screen.
if game.player1.yPos < float64(-game.player1.height)/2 {
game.player1.yPos = float64(-game.player1.height) / 2
} else if game.player1.yPos > windowHeight-(float64(game.player1.height)/2) {
game.player1.yPos = windowHeight - (float64(game.player1.height) / 2)
}
game.player1.yPos += game.player1.yVel
// Handles compmuter player 2 paddle movements.
if math.Abs(game.player2.yPos+float64(game.player2.height/2)-game.gameBall.yPos) < 15 {
game.player2.yPos += (game.gameBall.yPos - (game.player2.yPos + float64(game.player2.height/2))) / float64(game.player2.height/2) * 4
} else if game.player2.yPos+float64(game.player2.height/2) < game.gameBall.yPos {
game.player2.yPos += game.player2.maxVel
} else if game.player2.yPos+float64(game.player2.height/2) > game.gameBall.yPos {
game.player2.yPos += -game.player2.maxVel
}
// Paddle collision with the ball.
p1Collision := game.player1.xPos + float64(game.player1.width) + game.gameBall.radius
p2Collision := game.player2.xPos - game.gameBall.radius
if game.gameBall.xPos <= p1Collision && game.gameBall.xPos >= p1Collision-math.Abs(game.gameBall.xVel) &&
game.gameBall.yPos <= game.player1.yPos+float64(game.player1.height) &&
game.gameBall.yPos >= game.player1.yPos {
game.gameBall.xPos = p1Collision
relativeYIntersect := game.gameBall.yPos - (game.player1.yPos + float64(game.player1.height)/2)
normalizedYIntersect := relativeYIntersect / (float64(game.player1.height) / 2)
bounceAngle := normalizedYIntersect * maxBounceAngle
speed := math.Pow(math.Pow(game.gameBall.xVel, 2)+math.Pow(game.gameBall.yVel, 2), 0.5)
game.gameBall.xVel = math.Cos(bounceAngle) * speed
game.gameBall.yVel = math.Sin(bounceAngle) * speed
} else if game.gameBall.xPos >= p2Collision && game.gameBall.xPos <= p2Collision+game.gameBall.xVel &&
game.gameBall.yPos <= game.player2.yPos+float64(game.player2.height) &&
game.gameBall.yPos >= game.player2.yPos {
game.gameBall.xPos = p2Collision
relativeYIntersect := game.gameBall.yPos - (game.player2.yPos + float64(game.player2.height)/2)
normalizedYIntersect := relativeYIntersect / (float64(game.player2.height) / 2)
bounceAngle := normalizedYIntersect * maxBounceAngle
speed := math.Pow(math.Pow(game.gameBall.xVel, 2)+math.Pow(game.gameBall.yVel, 2), 0.5)
game.gameBall.xVel = -math.Cos(bounceAngle) * speed
game.gameBall.yVel = math.Sin(bounceAngle) * speed
}
// Ball bouncing off the top and bottom walls respectively.
if game.gameBall.yPos <= game.gameBall.radius || game.gameBall.yPos >= windowHeight-game.gameBall.radius {
game.gameBall.yVel = -game.gameBall.yVel
}
game.gameBall.xPos += game.gameBall.xVel
game.gameBall.yPos += game.gameBall.yVel
// Checks if the game has ended and increases the score of the winner.
if game.gameBall.xPos < -20 {
time.Sleep(500 * time.Millisecond)
game.p2Score++
resetGame(game)
} else if game.gameBall.xPos > 450 {
time.Sleep(500 * time.Millisecond)
game.p1Score++
resetGame(game)
}
}
func resetGame(g *pongGame) {
g.player1 = createPaddle(1)
g.player2 = createPaddle(2)
g.gameBall = ball{xPos: 210, yPos: 300, radius: 5, xVel: randVelocity() * 2, yVel: randVelocity() * 0.5}
g.paused = true
if g.gameBall.xVel < 0 {
g.gameBall.xVel = -g.gameBall.xVel
}
}
func randVelocity() float64 {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
output := (rng.Float64() * 1.5) + 3.5
if rng.Intn(2) == 1 {
output *= -1
}
return output
}
func array(xs [3]int) {
}