-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.py
69 lines (59 loc) · 2.07 KB
/
new.py
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
import random
import keyboard
import os
import time
def create_matrix():
matrix = [[0 for _ in range(3)] for _ in range(3)]
x, y = random.randint(0, 2), random.randint(0, 2)
matrix[x][y] = 1
return matrix, x, y
def display_matrix(matrix):
os.system('cls' if os.name == 'nt' else 'clear')
for row in matrix:
print(' '.join(map(str, row)))
print()
def move(matrix, x, y, direction):
matrix[x][y] = 0
if direction == 'w' and x > 0: # Move up
x -= 1
elif direction == 's' and x < 2: # Move down
x += 1
elif direction == 'a' and y > 0: # Move left
y -= 1
elif direction == 'd' and y < 2: # Move right
y += 1
matrix[x][y] = 1
return x, y
def main():
matrix, x, y = create_matrix()
display_matrix(matrix)
while True:
try:
if keyboard.is_pressed('w'):
x, y = move(matrix, x, y, 'w')
display_matrix(matrix)
time.sleep(0.2) # Prevent too fast movements
while keyboard.is_pressed('w'):
pass # Wait until the key is released
elif keyboard.is_pressed('s'):
x, y = move(matrix, x, y, 's')
display_matrix(matrix)
time.sleep(0.2) # Prevent too fast movements
while keyboard.is_pressed('s'):
pass # Wait until the key is released
elif keyboard.is_pressed('a'):
x, y = move(matrix, x, y, 'a')
display_matrix(matrix)
time.sleep(0.2) # Prevent too fast movements
while keyboard.is_pressed('a'):
pass # Wait until the key is released
elif keyboard.is_pressed('d'):
x, y = move(matrix, x, y, 'd')
display_matrix(matrix)
time.sleep(0.2) # Prevent too fast movements
while keyboard.is_pressed('d'):
pass # Wait until the key is released
except:
break
if __name__ == "__main__":
main()