-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcolorgame.go
178 lines (158 loc) · 3.56 KB
/
colorgame.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"math/rand"
"time"
)
const (
CGWIDTH = 20
CGHEIGHT = 15
CGSIZE = 16
CGS = 9
)
var (
colorgame [][]bool
p1x, p1y, p2x, p2y, tx int16
v1x, v1y, v2x, v2y, ty int16
)
func ColorGame() {
display.FillScreen(colors[ORANGE])
colorgame = make([][]bool, CGWIDTH)
p1x = int16(rand.Int31n(100)) + 30
p1y = int16(rand.Int31n(200)) + 20
p2x = int16(rand.Int31n(100)) + 190
p2y = int16(rand.Int31n(200)) + 20
v1x = 2
v1y = 3
v2x = -3
v2y = 2
for i := int16(0); i < CGWIDTH; i++ {
colorgame[i] = make([]bool, CGHEIGHT)
if i >= CGWIDTH/2 {
for j := int16(0); j < CGHEIGHT; j++ {
colorgame[i][j] = true
display.FillRectangle(i*CGSIZE, j*CGSIZE, CGSIZE, CGSIZE, colors[PURPLE])
}
}
}
display.FillRectangle(p1x-CGS, p1y-CGS, CGSIZE, CGSIZE, colors[PURPLE])
display.FillRectangle(p2x-CGS, p2y-CGS, CGSIZE, CGSIZE, colors[ORANGE])
for {
display.FillRectangle(p1x-CGS, p1y-CGS, CGSIZE, CGSIZE, colors[ORANGE])
display.FillRectangle(p2x-CGS, p2y-CGS, CGSIZE, CGSIZE, colors[PURPLE])
p1x += v1x
p1y += v1y
p2x += v2x
p2y += v2y
if (p1x < CGS && v1x < 0) || (p1x > (320-CGS) && v1x > 0) {
v1x = -v1x
}
if (p2x < CGS && v2x < 0) || (p2x > (320-CGS) && v2x > 0) {
v2x = -v2x
}
if (p1y < CGS && v1y < 0) || (p1y > (240-CGS) && v1y > 0) {
v1y = -v1y
}
if (p2y < CGS && v2y < 0) || (p2y > (240-CGS) && v2y > 0) {
v2y = -v2y
}
tx = (p1x - CGS) / CGSIZE
ty = p1y / CGSIZE
if tx < 0 {
tx = 0
}
if colorgame[tx][ty] {
colorgame[tx][ty] = false
if v1x < 0 {
v1x = -v1x
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[ORANGE])
}
tx = (p1x + CGS) / CGSIZE
if tx >= CGWIDTH {
tx = CGWIDTH - 1
}
if colorgame[tx][ty] {
colorgame[tx][ty] = false
if v1x > 0 {
v1x = -v1x
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[ORANGE])
}
tx = p1x / CGSIZE
ty = (p1y - CGS) / CGSIZE
if ty < 0 {
ty = 0
}
if colorgame[tx][ty] {
colorgame[tx][ty] = false
if v1y < 0 {
v1y = -v1y
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[ORANGE])
}
ty = (p1y + CGS) / CGSIZE
if ty >= CGHEIGHT {
ty = CGHEIGHT - 1
}
if colorgame[tx][ty] {
colorgame[tx][ty] = false
if v1y > 0 {
v1y = -v1y
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[ORANGE])
}
// P2
tx = (p2x - CGS) / CGSIZE
ty = p2y / CGSIZE
if tx < 0 {
tx = 0
}
if !colorgame[tx][ty] {
colorgame[tx][ty] = true
if v2x < 0 {
v2x = -v2x
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[PURPLE])
}
tx = (p2x + CGS) / CGSIZE
if tx >= CGWIDTH {
tx = CGWIDTH - 2
}
if !colorgame[tx][ty] {
colorgame[tx][ty] = true
if v2x > 0 {
v2x = -v2x
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[PURPLE])
}
tx = p2x / CGSIZE
ty = (p2y - CGS) / CGSIZE
if ty < 0 {
ty = 0
}
if !colorgame[tx][ty] {
colorgame[tx][ty] = true
if v2y < 0 {
v2y = -v2y
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[PURPLE])
}
ty = (p2y + CGS) / CGSIZE
if ty >= CGHEIGHT {
ty = CGHEIGHT - 2
}
if !colorgame[tx][ty] {
colorgame[tx][ty] = true
if v2y > 0 {
v2y = -v2y
}
display.FillRectangle(tx*CGSIZE, ty*CGSIZE, CGSIZE, CGSIZE, colors[PURPLE])
}
display.FillRectangle(p1x-CGS, p1y-CGS, CGSIZE, CGSIZE, colors[PURPLE])
display.FillRectangle(p2x-CGS, p2y-CGS, CGSIZE, CGSIZE, colors[ORANGE])
if !btnB.Get() {
break
}
time.Sleep(10 * time.Millisecond)
}
}