/
gopong.go
188 lines (154 loc) · 5.78 KB
/
gopong.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
package main
import (
sf "bitbucket.org/krepa098/gosfml2"
"github.com/bwilkins/gopong/ball"
"github.com/bwilkins/gopong/paddle"
"math"
"math/rand"
"time"
)
type Collider interface {
TopLeft() sf.Vector2f
BottomRight() sf.Vector2f
Center() sf.Vector2f
}
func main() {
//Define some variables for the game
//This block mostly will not change
paddleMaxSpeed := float32(400.0)
paddleDefaultSize := sf.Vector2f{25, 100}
isPlaying := false
gameWidth := uint(800)
gameHeight := uint(600)
bitDepth := uint(32)
ballMaxSpeed := float32(400.0)
ballRadius := float32(10.0)
//These are a little more special... guess what they do!
ticker := time.NewTicker(time.Second / 60)
aiTicker := time.NewTicker(time.Second / 10)
rand.Seed(time.Now().UnixNano())
//Instantiate the render window for SFML
renderWindow := sf.NewRenderWindow(sf.VideoMode{gameWidth, gameHeight, bitDepth}, "Pong (Brett's Go test)", sf.StyleDefault, nil)
//Create the left paddle
leftPaddle := paddle.NewPaddle(paddleMaxSpeed, paddleMaxSpeed, paddleDefaultSize, sf.Color{100, 100, 200, 255})
//Create the right paddle
rightPaddle := paddle.NewPaddle(0, paddleMaxSpeed, paddleDefaultSize, sf.Color{200, 100, 100, 255})
//Create the ball
ball := ball.NewBall(ballMaxSpeed, ballMaxSpeed, ballRadius, "resources/ball.wav")
//Load font
font, _ := sf.NewFontFromFile("resources/sansation.ttf")
//Init the pause message
pauseMessage := sf.NewText(font)
pauseMessage.SetCharacterSize(40)
pauseMessage.SetPosition(sf.Vector2f{170, 150})
pauseMessage.SetColor(sf.ColorWhite())
pauseMessage.SetString("Welcome to Brett's SFML Pong!\nPress space to start the game.")
for renderWindow.IsOpen() {
select {
case <-ticker.C:
//poll for events
for event := renderWindow.PollEvent(); event != nil; event = renderWindow.PollEvent() {
switch ev := event.(type) {
case sf.EventKeyReleased:
switch ev.Code {
case sf.KeyEscape:
renderWindow.Close()
case sf.KeySpace:
if !isPlaying {
//restart the game
isPlaying = true
leftPaddle.Shape.SetPosition(sf.Vector2f{10 + leftPaddle.Size.X/2, float32(gameHeight) / 2})
rightPaddle.Shape.SetPosition(sf.Vector2f{float32(gameWidth) - 10 - rightPaddle.Size.X/2, float32(gameHeight) / 2})
ball.Shape.SetPosition(sf.Vector2f{float32(gameWidth) / 2, float32(gameHeight) / 2})
//ensure the ball angle isn't too vertical
for {
ball.Angle = rand.Float32() * math.Pi * 2
if math.Abs(math.Cos(float64(ball.Angle))) > 0.7 {
break
}
}
}
}
case sf.EventClosed:
renderWindow.Close()
}
}
if isPlaying {
deltaTime := time.Second / 60
//Move the player's paddle
if sf.KeyboardIsKeyPressed(sf.KeyUp) && leftPaddle.TopLeft().Y > 5 {
leftPaddle.Shape.Move(sf.Vector2f{0, -leftPaddle.Speed * float32(deltaTime.Seconds())})
}
if sf.KeyboardIsKeyPressed(sf.KeyDown) && leftPaddle.BottomRight().Y < float32(gameHeight)-5 {
leftPaddle.Shape.Move(sf.Vector2f{0, leftPaddle.Speed * float32(deltaTime.Seconds())})
}
//Move the ai's paddle
if (rightPaddle.Speed < 0 && rightPaddle.TopLeft().Y > 5) || (rightPaddle.Speed > 0 && rightPaddle.BottomRight().Y < float32(gameHeight)-5) {
rightPaddle.Shape.Move(sf.Vector2f{0, rightPaddle.Speed * float32(deltaTime.Seconds())})
}
//Move ze ball
factor := ball.Speed * float32(deltaTime.Seconds())
ball.Shape.Move(sf.Vector2f{float32(math.Cos(float64(ball.Angle))) * factor, float32(math.Sin(float64(ball.Angle))) * factor})
//Check collisions between ball and screen edge
if ball.TopLeft().X < 0 {
isPlaying = false
pauseMessage.SetString("You lost!\nPress space to restart or\nescape to quit")
}
if ball.BottomRight().X > float32(gameWidth) {
isPlaying = false
pauseMessage.SetString("You won!\nPress space to play again or\nescape to quit")
}
if ball.TopLeft().Y < 0 {
ball.Angle = -ball.Angle
ball.Shape.SetPosition(sf.Vector2f{ball.Center().X, ball.Radius + 0.1})
ball.Sound.Play()
}
if ball.BottomRight().Y > float32(gameHeight) {
ball.Angle = -ball.Angle
ball.Shape.SetPosition(sf.Vector2f{ball.Center().X, float32(gameHeight) - ball.Radius - 0.1})
ball.Sound.Play()
}
//Check collisions between the ball and the left paddle
if leftPaddle.CollideRight(ball) {
if ball.Center().Y > leftPaddle.Center().Y {
ball.Angle = math.Pi - ball.Angle + rand.Float32()*math.Pi*0.2
} else {
ball.Angle = math.Pi - ball.Angle - rand.Float32()*math.Pi*0.2
}
ball.Shape.SetPosition(sf.Vector2f{leftPaddle.Center().X + ball.Radius + leftPaddle.Size.X/2 + 0.1, ball.Center().Y})
ball.Sound.Play()
}
//Check collisions between the ball and the right paddle
if rightPaddle.CollideLeft(ball) {
if ball.Center().Y > rightPaddle.Center().Y {
ball.Angle = math.Pi - ball.Angle + rand.Float32()*math.Pi*0.2
} else {
ball.Angle = math.Pi - ball.Angle - rand.Float32()*math.Pi*0.2
}
ball.Shape.SetPosition(sf.Vector2f{rightPaddle.Center().X - ball.Radius - rightPaddle.Size.X/2 - 0.1, ball.Center().Y})
ball.Sound.Play()
}
}
//Clear the window
renderWindow.Clear(sf.Color{50, 200, 50, 0})
//Draw some shit
if isPlaying {
renderWindow.Draw(leftPaddle.Shape, nil)
renderWindow.Draw(rightPaddle.Shape, nil)
renderWindow.Draw(ball.Shape, nil)
} else {
renderWindow.Draw(pauseMessage, nil)
}
//Draw everything to the screen
renderWindow.Display()
case <-aiTicker.C:
if ball.BottomRight().Y > rightPaddle.BottomRight().Y {
rightPaddle.Speed = rightPaddle.MaxSpeed
} else if ball.TopLeft().Y < rightPaddle.TopLeft().Y {
rightPaddle.Speed = -rightPaddle.MaxSpeed
} else {
rightPaddle.Speed = 0
}
}
}
}