forked from scraly/learning-go-by-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.go
96 lines (80 loc) · 2.77 KB
/
states.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
package main
import (
"strconv"
"github.com/scraly/learning-go-by-examples/go-gopher-gba/fonts"
"tinygo.org/x/tinydraw"
"tinygo.org/x/tinyfont"
)
func drawGophers() {
// Display a textual message "Gopher" with Google colors
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 36, 60, 'G', gBlue)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 71, 60, 'o', gRed)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 98, 60, 'p', gYellow)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 126, 60, 'h', gGreen)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 154, 60, 'e', gBlue)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 180, 60, 'r', gRed)
// Display a "press START button" message - center
tinyfont.WriteLine(display, &tinyfont.TomThumb, 85, 90, "Press START button", white)
// Display a textual message "Zuri" with Google colors
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 71, 130, 'Z', gBlue)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 98, 130, 'u', gRed)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 126, 130, 'r', gYellow)
tinyfont.DrawChar(display, &fonts.Bold24pt7b, 144, 130, 'i', gGreen)
// Display two gophers
tinyfont.DrawChar(display, &fonts.Regular58pt, 5, 140, 'J', green)
tinyfont.DrawChar(display, &fonts.Regular58pt, 195, 140, 'X', red)
tinydraw.Rectangle(display, int16(0), int16(0), screenWidth, screenHeight, red)
}
// Start the game and reset global state
func startGame() {
// Clear display
clearScreen()
// Reset Global State
score = 0
active = true
x = 100
y = 100
// Display gopher
tinyfont.DrawChar(display, &fonts.Regular58pt, x, y, 'B', green)
}
// Check for collision with compensation
func checkBorder(x, y int16) (int16, int16) {
if active {
var border int16 = 10
// I think compensation is needed due to the size of the gopher
var x_comp int16 = 40
var y_comp int16 = 40
// if hit border, kill
if (x >= screenWidth-border-x_comp) || (x <= border) || (y <= border+y_comp) || (y >= screenHeight-border) {
killScreen()
}
}
return x, y
}
// Display end screen and pause game
func killScreen() {
for {
clearScreen()
tinyfont.WriteLine(display, &tinyfont.TomThumb, 105, 30, "You DIED!", red)
tinyfont.WriteLine(display, &tinyfont.TomThumb, 110, 45, "Score", red)
tinyfont.WriteLine(display, &tinyfont.TomThumb, 115, 55, strconv.Itoa(int(score)), red)
tinyfont.WriteLine(display, &tinyfont.TomThumb, 80, 130, "Press start to restart", red)
tinyfont.DrawChar(display, &fonts.Regular58pt, 95, 110, 'C', red)
active = false
if regKEYPAD.Get() == keySELECT {
break
}
break
clearScreen()
}
}
// Overwrite the entire screen
func clearScreen() {
tinydraw.FilledRectangle(
display,
int16(0), int16(0),
screenWidth, screenHeight,
black,
)
tinydraw.Rectangle(display, int16(0), int16(0), screenWidth, screenHeight, red)
}