MCPcopy Index your code
hub / github.com/FrameworkComputer/inputmodule-rs / snake

Function snake

python/inputmodule/gui/games.py:127–197  ·  view source on GitHub ↗
(dev)

Source from the content-addressed store, hash-verified

125
126
127def snake(dev):
128 global direction
129 global body
130 head = (0, 0)
131 direction = keys.DOWN
132 food = (0, 0)
133 while food == head:
134 food = (random.randint(0, WIDTH - 1), random.randint(0, HEIGHT - 1))
135
136 # Setting
137 WRAP = False
138
139 thread = threading.Thread(target=snake_keyscan, args=(), daemon=True)
140 thread.start()
141
142 prev = datetime.now()
143 while True:
144 now = datetime.now()
145 delta = (now - prev) / timedelta(milliseconds=1)
146
147 if delta > 200:
148 prev = now
149 else:
150 continue
151
152 # Update position
153 (x, y) = head
154 oldhead = head
155 if direction == keys.RIGHT:
156 head = (x + 1, y)
157 elif direction == keys.LEFT:
158 head = (x - 1, y)
159 elif direction == keys.UP:
160 head = (x, y - 1)
161 elif direction == keys.DOWN:
162 head = (x, y + 1)
163
164 # Detect edge condition
165 (x, y) = head
166 if head in body:
167 return game_over(dev)
168 elif x >= WIDTH or x < 0 or y >= HEIGHT or y < 0:
169 if WRAP:
170 if x >= WIDTH:
171 x = 0
172 elif x < 0:
173 x = WIDTH - 1
174 elif y >= HEIGHT:
175 y = 0
176 elif y < 0:
177 y = HEIGHT - 1
178 head = (x, y)
179 else:
180 return game_over(dev)
181 elif head == food:
182 body.insert(0, oldhead)
183 while food == head:
184 food = (random.randint(0, WIDTH - 1),

Callers 2

main_cliFunction · 0.90
run_guiFunction · 0.90

Calls 2

render_matrixFunction · 0.90
game_overFunction · 0.85

Tested by

no test coverage detected