forked from scraly/learning-go-by-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moves.go
91 lines (79 loc) · 1.9 KB
/
moves.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
package main
import (
"runtime/interrupt"
"github.com/scraly/learning-go-by-examples/go-gopher-gba/fonts"
"tinygo.org/x/tinyfont"
)
func update(interrupt.Interrupt) {
// check collision
x, y = checkBorder(x, y)
// Read uint16 from register regKEYPAD that represents the state of current buttons pressed
// and compares it against the defined values for each button on the Gameboy Advance
switch keyValue := regKEYPAD.Get(); keyValue {
// Start the "game"
case keySTART:
startGame()
// Go back to Menu and pause
case keySELECT:
active = false
clearScreen()
drawGophers()
// Gopher go to the right
case keyRIGHT:
x, y = move(x, y, 10, false, true)
// Gopher go to the left
case keyLEFT:
x, y = move(x, y, 10, false, false)
// Gopher go to the down
case keyDOWN:
x, y = move(x, y, 10, true, true)
case keyUP:
x, y = move(x, y, 10, true, false)
//Gopher jump
case keyA:
x, y = move(x, y, 20, true, false)
// Clear the display
x, y = move(x, y, 20, true, true)
}
// Add random movement
x, y = wind(x, y)
// Increment Global Counter
score = score + 1
}
// Add random movement to bottom left of screen
func wind(x, y int16) (int16, int16) {
if active {
// increase wind_power as score goes higher
var wind_power int16 = score / 10
x, y = move(x, y, wind_power, random(), random())
}
return x, y
}
// importing math/random overflew my game
func random() bool {
if score%2 == 0 {
return false
} else {
return true
}
}
// move abstraction
func move(current_x, current_y, pixels int16, vertical, positive bool) (int16, int16) {
// Clear display by drawing a gopher in black
tinyfont.DrawChar(display, &fonts.Regular58pt, x, y, 'B', black)
if vertical {
if positive {
y = y + pixels
} else {
y = y - pixels
}
} else {
if positive {
x = x + pixels
} else {
x = x - pixels
}
}
tinyfont.DrawChar(display, &fonts.Regular58pt, x, y, 'B', green)
return x, y
}