-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.go
86 lines (69 loc) · 1.43 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
package main
type Player struct {
tag string
score int
xpos int
ypos int
width int
height int
initx int
inity int
}
// get a new player instance
func newPlayer(tag string, initialPos int, padx int) *Player {
p := Player{tag, 0, padx, initialPos, platformWidth, platformHeight, padx, initialPos}
return &p
}
func (p *Player) Coords() (int, int) {
return p.xpos, p.ypos
}
func (p *Player) GoUp() {
p.ypos--
}
func (p *Player) GoDown() {
p.ypos++
}
func (p *Player) AddPoint() {
p.score++
}
func (p *Player) GetScore() int {
return p.score
}
func (p *Player) GetWidth() int {
return p.width
}
func (p *Player) GetHeight() int {
return p.height
}
func (p *Player) GetTag() string {
return p.tag
}
func (p *Player) Reset() {
p.ypos = p.initx
p.ypos = p.inity
}
// Players struct includes both players individually and a method that returns them both
type Players struct {
P1 *Player
P2 *Player
}
func (p *Players) GetAll() []*Player {
return []*Player{p.P1, p.P2}
}
// Get a pair of players ready and initialised
func newPlayers(w int, h int, padding int) *Players {
initialPos := (h - platformHeight) / 2
p1Pad := padding
p2Pad := w - padding
// left platform must be on even pixel
if p1Pad%2 == 0 {
p1Pad += 1
}
// right platform must be on odd pixel
if p2Pad%2 == 1 {
p2Pad += 1
}
p1 := newPlayer(playerP1, initialPos, p1Pad)
p2 := newPlayer(playerP2, initialPos, p2Pad)
return &Players{p1, p2}
}