-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaddle.go
116 lines (98 loc) · 1.8 KB
/
paddle.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
package main
import . "github.com/gen2brain/raylib-go/raylib"
type paddle struct {
Rectangle
game *game
name string
score int32
color Color
speed float32
}
func(p *paddle) physicsBody() []Vector2 {
pb := make([]Vector2, 0)
for y := p.Y; y <= p.Y+p.Height; y++ {
for x := p.X; x <= p.X+p.Width; x++ {
pb = append(pb, NewVector2(x, y))
}
}
return pb
}
func (p *paddle) isHit(pos Vector2) (bool, string) {
for _, vec := range p.physicsBody() {
if vec.X == pos.X && vec.Y == pos.Y {
middle := p.Y+p.Height/2
middleTop := middle-p.Height/7
middleBottom := middle+p.Height/7
if vec.Y < middleBottom && vec.Y > middleTop {
return true, "straight"
}else if vec.Y < middleTop {
return true, "up"
}else if vec.Y > middleBottom {
return true, "down"
}
}
}
return false, ""
}
func(p *paddle) update(){
switch(p.name){
case "player1":
if IsKeyDown(KeyUp) {
if p.Y > 0 {
p.Y -= p.speed
}
}
if IsKeyDown(KeyDown) {
if p.Y+p.Height < float32(p.game.options.screenHeight) {
p.Y += p.speed
}
}
break
case "computer":
if p.game.ball.center.Y > p.Y+p.Height/2 {
p.Y += p.speed
}else if p.game.ball.center.Y < p.Y+p.Height/2 {
p.Y -= p.speed
}
break
}
}
func (p *paddle) addPoint(){
p.score++
}
func (p *paddle) draw(){
DrawRectangle(int32(p.X), int32(p.Y), int32(p.Width), int32(p.Height), p.color)
}
func newPaddle(n string, g *game)*paddle{
var p *paddle
if n == "player1" {
p = &paddle{
Rectangle{
float32(g.options.screenWidth-45),
float32(g.options.screenHeight/2 - 60),
35,
200,
},
g,
n,
0,
White,
5.0,
}
}else if n == "computer" {
p = &paddle{
Rectangle{
10,
float32(g.options.screenHeight/2 - 60),
35,
200,
},
g,
n,
0,
White,
3.0,
}
}
return p
}