-
Notifications
You must be signed in to change notification settings - Fork 2
/
ai.go
55 lines (43 loc) · 1.22 KB
/
ai.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
package main
func (g *Game) aiMove() {
w, h := g.ui.screen.Size()
bw, bh := g.ui.ball.Coords()
pw, ph := g.players.P2.Coords()
w, bw, pw = w/2, bw/2, pw/2
if bw < w/2 {
return
}
// if the ball has surpassed half of the screen, calculate the spot
// where ball will be and move the platform there
currentW := pw - bw
currentH := bh
_, vely := g.ui.ball.Vels()
if vely > 0 {
vely = 1
} else {
vely = -1
}
// in case double bounce occurs, this will shorten the value without any change
currentW = currentW % (2 * h)
if currentW > h {
currentW = currentW % h
// in this case vertical speed direction of the ball must be switched
vely *= -1
}
currentH = currentH + (currentW * vely)
if currentH < 0 {
currentH *= -1
}
// now move the platform in the direction where the ball will be
if currentH > ph+platformHeight/2 {
g.movePlayerDown(g.players.P2)
}
if currentH < ph+platformHeight/2 {
g.movePlayerUp(g.players.P2)
}
//st := g.theme.GetCurrent().GetTextStyle()
//g.screen.SetContent(10, 10, 'a', []rune(strconv.Itoa(currentW)), st)
//g.screen.SetContent(10, 11, 'a', []rune(strconv.Itoa(currentH)), st)
//g.screen.SetContent(10, 12, 'a', []rune(strconv.Itoa(w)), st)
//g.screen.Show()
}