-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.go
323 lines (273 loc) Β· 7.66 KB
/
game.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
import (
"context"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"time"
"github.com/gdamore/tcell"
pb "github.com/qsymmachus/netpong/netpong"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
// Models a game of pong. Implements the `NetPongServer` interface.
type Game struct {
pb.UnimplementedNetPongServer
Screen tcell.Screen
Ball Ball
LocalPlayer Player
RemotePlayer Player
MaxScore int
Connected bool
ServerMode bool
Port int
ServerAddress string
Errors chan (error)
DebugMode bool
}
// I created this interface so I could write a version of the `Play` function
// that accepts either a `NetPong_PlayServer` or `NetPong_PlayClient`.
type PlayStream interface {
Send(*pb.MovePaddle) error
Recv() (*pb.MovePaddle, error)
}
// Implementation of streamed gameplay between a local and remote player. The
// function is identical for clients and servers.
func (g *Game) PlayWithStream(stream PlayStream) error {
g.Connected = true
defer func() { g.Connected = false }()
_, height := g.Screen.Size()
go func() {
// Poll local key events and turn them into paddle movement commands to
// send them to the remote game.
for {
switch event := g.Screen.PollEvent().(type) {
case *tcell.EventKey:
if event.Key() == tcell.KeyUp {
err := stream.Send(&pb.MovePaddle{Direction: pb.Direction_UP})
if err != nil {
g.Errors <- err
}
} else if event.Key() == tcell.KeyDown {
err := stream.Send(&pb.MovePaddle{Direction: pb.Direction_DOWN})
if err != nil {
g.Errors <- err
}
}
}
}
}()
for {
// Receive paddle movement commands from the remote game and use
// them to update local game state.
remoteMove, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
g.Errors <- err
return err
}
if remoteMove.Direction == pb.Direction_UP {
g.RemotePlayer.Paddle.MoveUp()
} else if remoteMove.Direction == pb.Direction_DOWN {
g.RemotePlayer.Paddle.MoveDown(height)
}
}
}
// Server-side handling of a stream of paddle movement commands between
// the local and remote games.
func (g *Game) Play(stream pb.NetPong_PlayServer) error {
return g.PlayWithStream(stream)
}
// Starts the game.
func (g *Game) Run() {
style := DefaultGameStyle()
paddleStyle := DefaultPaddleStyle()
// Continually poll for events (game inputs and outputs) in the background.
// Events update game state.
go func() {
for {
g.PollEvents()
}
}()
if g.ServerMode {
// If the game is running in server mode, listen and wait for a connection.
listen, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", g.Port))
if err != nil {
log.Fatalf("Failed to start game server: %v\n", err)
}
grpcServer := grpc.NewServer()
pb.RegisterNetPongServer(grpcServer, g)
go grpcServer.Serve(listen)
for !g.Connected {
g.DrawWaitScreen(style)
}
} else {
// Otherwise, the game is in client mode, and will attempt to connect to a server.
connOpts := grpc.WithTransportCredentials(insecure.NewCredentials())
conn, err := grpc.Dial(*serverAddress, connOpts)
if err != nil {
log.Fatalf("Failed to connect to remote game: %v\n", err)
}
defer conn.Close()
client := pb.NewNetPongClient(conn)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.Play(ctx)
if err != nil {
log.Fatalf("Failed to connect to remote game: %v\n", err)
}
go g.PlayWithStream(stream)
}
// Control loop that continually checks game state and redraws the screen based
// on that state.
for {
g.Screen.Clear()
if g.GameOver() {
g.DrawEndGame(style)
break
}
g.DrawPaddles(paddleStyle)
g.DrawBall(style)
g.DrawScores(style)
pause(40)
g.Screen.Show()
}
}
// Draw the player paddles on the game screen.
func (g *Game) DrawPaddles(paddleStyle tcell.Style) {
drawSprite(
g.Screen,
g.LocalPlayer.Paddle.X,
g.LocalPlayer.Paddle.Y,
g.LocalPlayer.Paddle.X+g.LocalPlayer.Paddle.Width,
g.LocalPlayer.Paddle.Y+g.LocalPlayer.Paddle.Height,
paddleStyle,
g.LocalPlayer.Paddle.Display(),
)
drawSprite(
g.Screen,
g.RemotePlayer.Paddle.X,
g.RemotePlayer.Paddle.Y,
g.RemotePlayer.Paddle.X+g.RemotePlayer.Paddle.Width,
g.RemotePlayer.Paddle.Y+g.RemotePlayer.Paddle.Height,
paddleStyle,
g.RemotePlayer.Paddle.Display(),
)
}
// Draw the ball on the game screen.
func (g *Game) DrawBall(style tcell.Style) {
width, height := g.Screen.Size()
g.Ball.CheckEdges(width, height)
g.Ball.CheckCollisions(g.LocalPlayer.Paddle, g.RemotePlayer.Paddle)
if g.Ball.HasHitLeft() {
g.RemotePlayer.Score += 1
pause(1000)
g.Ball.ResetLeft(width)
}
if g.Ball.HasHitRight(width) {
g.LocalPlayer.Score += 1
pause(1000)
g.Ball.ResetRight(width)
}
g.Ball.Update()
drawSprite(g.Screen, g.Ball.X, g.Ball.Y, g.Ball.X, g.Ball.Y, style, g.Ball.Display())
}
// Draws the player scores on the game screen.
func (g *Game) DrawScores(style tcell.Style) {
width, _ := g.Screen.Size()
leftScore := fmt.Sprintf("β %s", strconv.Itoa(g.LocalPlayer.Score))
rightScore := fmt.Sprintf("%s β", strconv.Itoa(g.RemotePlayer.Score))
drawSprite(g.Screen, (width/2)-10, 1, (width/2)-7, 1, style, leftScore)
drawSprite(g.Screen, (width/2)+10, 1, (width/2)+13, 1, style, rightScore)
}
// Checks if the game is over.
func (g *Game) GameOver() bool {
return g.LocalPlayer.Score == g.MaxScore || g.RemotePlayer.Score == g.MaxScore
}
// Declares the winner of the game.
func (g *Game) DeclareWinner() string {
if g.LocalPlayer.Score > g.RemotePlayer.Score {
return "β Winner"
} else {
return "Winner β"
}
}
// Listens for user input events, like keyboard input.
func (g *Game) PollEvents() {
screen := g.Screen
_, height := screen.Size()
switch event := screen.PollEvent().(type) {
case *tcell.EventResize:
screen.Sync()
case *tcell.EventKey:
if isExitKey(event.Key()) {
g.End()
} else if event.Key() == tcell.KeyUp {
g.LocalPlayer.Paddle.MoveUp()
} else if event.Key() == tcell.KeyDown {
g.LocalPlayer.Paddle.MoveDown(height)
}
}
}
// Draw a wait screen while waiting for a connection in server mode.
func (g *Game) DrawWaitScreen(style tcell.Style) {
width, _ := g.Screen.Size()
waitMessage := fmt.Sprintf("Waiting for a player to connect on port %d...", g.Port)
drawSprite(g.Screen, (width/2)-25, 7, (width/2)+25, 7, style, waitMessage)
g.Screen.Show()
pause(500)
}
// Draws the end game screen.
func (g *Game) DrawEndGame(style tcell.Style) {
width, _ := g.Screen.Size()
drawSprite(g.Screen, (width/2)-4, 7, (width/2)+5, 7, style, g.DeclareWinner())
drawSprite(g.Screen, (width/2)-8, 10, (width/2)+8, 10, style, "(CTRL+C to exit)")
for {
g.Screen.Show()
pause(500)
}
}
// Ends the game.
func (g *Game) End() {
g.Screen.Fini()
if g.DebugMode {
g.PrintErrors()
}
close(g.Errors)
os.Exit(0)
}
// Prints all errors that were sent to the `Errors` channel during the game.
func (g *Game) PrintErrors() {
for err := range g.Errors {
fmt.Printf("Encountered error: %v\n", err)
}
}
// Draws a sprite on the screen, a group of runes with rectangular boundaries set
// by `xStart`, `yStart`, `xEnd`, and `yStart`.
func drawSprite(s tcell.Screen, xStart, yStart, xEnd, yEnd int, style tcell.Style, text string) {
row := yStart
col := xStart
for _, r := range []rune(text) {
s.SetContent(col, row, r, nil, style)
col++
// If we've reached the vertical edge (last column), move down a row and
// start from the first column.
if col >= xEnd {
row++
col = xStart
}
// If we've reach the horizontal edge (last row), we've finished drawing
// the sprite.
if row > yEnd {
break
}
}
}
func pause(milliseconds time.Duration) {
time.Sleep(milliseconds * time.Millisecond)
}