-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.go
104 lines (84 loc) · 1.86 KB
/
player.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
package pong
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
)
type Player struct {
Number int
X int
Y int
Size int
ButtonUp pixelgl.Button
ButtonDown pixelgl.Button
ButtonLeft pixelgl.Button
ButtonRight pixelgl.Button
Board *Board
}
func (p *Player) Draw(imd *imdraw.IMDraw) {
scale := GetConfig().Scale
imd.Color = pixel.RGB(1, 0, 1)
imd.Push(pixel.V(float64(p.X)*scale, float64(p.Y)*scale))
imd.Push(pixel.V(float64(p.X)*scale, float64(p.Y+p.Size)*scale))
imd.Line(1 * scale)
}
func NewPlayer(playerNumber int, b *Board) *Player {
player := new(Player)
player.Number = playerNumber
player.Size = 20
player.ButtonUp = pixelgl.KeyW
player.ButtonDown = pixelgl.KeyS
player.ButtonLeft = pixelgl.KeyA
player.ButtonRight = pixelgl.KeyD
if playerNumber == 0 {
player.X = 1
player.Y = 1
} else if playerNumber == 1 {
//player.ButtonUp = pixelgl.KeyUp
//player.ButtonDown = pixelgl.KeyDown
//player.ButtonLeft = pixelgl.KeyLeft
//player.ButtonRight = pixelgl.KeyRight
player.X = 150 - 1
player.Y = 100 - player.Size - 1
}
player.Board = b
return player
}
func (p *Player) SetPosition(x, y int) {
p.X = x
p.Y = y
}
func (p *Player) GetPosition() (int, int) {
return p.X, p.Y
}
func (p *Player) Move(offsetX, offsetY int) {
newX := p.X + offsetX
newY := p.Y + offsetY
if p.Number == 0 {
if newX < p.Board.SizeX/2 && newX > 0 {
p.X = newX
}
}
if p.Number == 1 {
if newX > p.Board.SizeX/2 && newX < p.Board.SizeX {
p.X = newX
}
}
if newY+p.Size <= p.Board.SizeY && newY >= 0 {
p.Y = newY
}
}
func (p *Player) HandleWindowEvents(win *pixelgl.Window) {
if win.Pressed(p.ButtonUp) {
p.Move(0, 1)
}
if win.Pressed(p.ButtonDown) {
p.Move(0, -1)
}
if win.Pressed(p.ButtonLeft) {
p.Move(-1, 0)
}
if win.Pressed(p.ButtonRight) {
p.Move(1, 0)
}
}