-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (72 loc) · 1.77 KB
/
main.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
package main
import (
"time"
client "github.com/PonGoLan/game/client/network"
pong "github.com/PonGoLan/game/shared"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
var (
frames = 0
second = time.Tick(time.Second)
)
func run() {
win, err := pixelgl.NewWindow(pong.GetConfig().WindoWConfig)
if err != nil {
panic(err)
}
win.Clear(colornames.Skyblue)
imd := imdraw.New(nil)
game := pong.NewGame()
board := pong.NewBoard()
game.Players[0] = pong.NewPlayer(0, board)
game.Players[1] = pong.NewPlayer(1, board)
ball := pong.NewBall(board)
aTick := time.Tick(time.Second / 128)
// [network] identify current client
playerNumber := client.Get().Identify()
player := game.Players[playerNumber]
for !win.Closed() {
pong.ApplyMatrixToWindow(win)
board.Draw(imd)
game.DrawPlayers(imd)
ball.Draw(imd)
win.Clear(colornames.Black)
imd.Draw(win)
game.DrawScore(win)
win.Update()
imd.Clear()
frames++
select {
case <-second:
win.SetTitle(pong.GetConfig().GetTitle(frames))
client.Get().GetScore(game)
frames = 0
case <-aTick:
// pong.BallPlayerCollision(ball, player1)
// pong.BallPlayerCollision(ball, player2)
// ball.Move(game)
player.HandleWindowEvents(win)
// Update current player position
pX, pY, err := client.Get().SendPlayerPosition(player)
if err == nil {
player.SetPosition(pX, pY)
}
// Update ball position
ballX, ballY, err := client.Get().GetBallPosition()
if err == nil {
ball.SetPosition(ballX, ballY)
}
// update opponent position
oppNumber, oppX, oppY, err := client.Get().GetOpponent(player)
if err == nil {
game.Players[oppNumber].SetPosition(oppX, oppY)
}
default:
}
}
}
func main() {
pixelgl.Run(run)
}