-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathgames.py
More file actions
219 lines (184 loc) · 5.33 KB
/
Copy pathgames.py
File metadata and controls
219 lines (184 loc) · 5.33 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from getkey import getkey, keys
import random
from datetime import datetime, timedelta
import time
import threading
from inputmodule.inputmodule import (
GameControlVal,
send_command,
CommandVals,
Game,
)
from inputmodule.inputmodule.ledmatrix import (
show_string,
WIDTH,
HEIGHT,
render_matrix,
)
# Constants
ARG_UP = 0
ARG_DOWN = 1
ARG_LEFT = 2
ARG_RIGHT = 3
ARG_QUIT = 4
ARG_2LEFT = 5
ARG_2RIGHT = 6
# Variables
direction = None
body = []
def opposite_direction(direction):
if direction == keys.RIGHT:
return keys.LEFT
elif direction == keys.LEFT:
return keys.RIGHT
elif direction == keys.UP:
return keys.DOWN
elif direction == keys.DOWN:
return keys.UP
return direction
def snake_keyscan():
global direction
global body
while True:
current_dir = direction
key = getkey()
if key in [keys.RIGHT, keys.UP, keys.LEFT, keys.DOWN]:
# Don't allow accidental suicide if we have a body
if key == opposite_direction(current_dir) and body:
continue
direction = key
def snake_embedded_keyscan(dev):
while True:
key_arg = None
key = getkey()
if key == keys.UP:
key_arg = GameControlVal.Up
elif key == keys.DOWN:
key_arg = GameControlVal.Down
elif key == keys.LEFT:
key_arg = GameControlVal.Left
elif key == keys.RIGHT:
key_arg = GameControlVal.Right
elif key == "q":
# Quit
key_arg = GameControlVal.Quit
if key_arg is not None:
send_command(dev, CommandVals.GameControl, [key_arg])
def game_over(dev):
global body
while True:
show_string(dev, "GAME ")
time.sleep(0.75)
show_string(dev, "OVER!")
time.sleep(0.75)
score = len(body)
show_string(dev, f"{score:>3} P")
time.sleep(0.75)
def pong_embedded(dev):
# Start game
send_command(dev, CommandVals.StartGame, [Game.Pong])
while True:
key_arg = None
key = getkey()
if key == keys.LEFT:
key_arg = ARG_LEFT
elif key == keys.RIGHT:
key_arg = ARG_RIGHT
elif key == "a":
key_arg = ARG_2LEFT
elif key == "d":
key_arg = ARG_2RIGHT
elif key == "q":
# Quit
key_arg = ARG_QUIT
if key_arg is not None:
send_command(dev, CommandVals.GameControl, [key_arg])
def game_of_life_embedded(dev, arg):
# Start game
# TODO: Add a way to stop it
print("Game", int(arg))
send_command(dev, CommandVals.StartGame, [Game.GameOfLife, int(arg)])
def snake_embedded(dev):
# Start game
send_command(dev, CommandVals.StartGame, [Game.Snake])
snake_embedded_keyscan(dev)
def snake(dev):
global direction
global body
head = (0, 0)
direction = keys.DOWN
food = (0, 0)
while food == head:
food = (random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1))
# Setting
WRAP = False
thread = threading.Thread(target=snake_keyscan, args=(), daemon=True)
thread.start()
prev = datetime.now()
while True:
now = datetime.now()
delta = (now - prev) / timedelta(milliseconds=1)
if delta > 200:
prev = now
else:
continue
# Update position
(x, y) = head
oldhead = head
if direction == keys.RIGHT:
head = (x + 1, y)
elif direction == keys.LEFT:
head = (x - 1, y)
elif direction == keys.UP:
head = (x, y - 1)
elif direction == keys.DOWN:
head = (x, y + 1)
# Detect edge condition
(x, y) = head
if head in body:
return game_over(dev)
elif x >= WIDTH or x < 0 or y >= HEIGHT or y < 0:
if WRAP:
if x >= WIDTH:
x = 0
elif x < 0:
x = WIDTH - 1
elif y >= HEIGHT:
y = 0
elif y < 0:
y = HEIGHT - 1
head = (x, y)
else:
return game_over(dev)
elif head == food:
body.insert(0, oldhead)
while food == head:
food = (random.randint(0, WIDTH - 1),
random.randint(0, HEIGHT - 1))
elif body:
body.pop()
body.insert(0, oldhead)
# Draw on screen
matrix = [[0 for _ in range(HEIGHT)] for _ in range(WIDTH)]
matrix[x][y] = 1
matrix[food[0]][food[1]] = 1
for bodypart in body:
(x, y) = bodypart
matrix[x][y] = 1
render_matrix(dev, matrix)
def wpm_demo(dev):
"""Capture keypresses and calculate the WPM of the last 10 seconds
TODO: I'm not sure my calculation is right."""
start = datetime.now()
keypresses = []
while True:
_ = getkey()
now = datetime.now()
keypresses = [x for x in keypresses if (now - x).total_seconds() < 10]
keypresses.append(now)
# Word is five letters
wpm = (len(keypresses) / 5) * 6
total_time = (now - start).total_seconds()
if total_time < 10:
wpm = wpm / (total_time / 10)
show_string(dev, " " + str(int(wpm)))