Skip to content

Commit d7ee17e

Browse files
authored
Merge pull request #5579 from CesarCarmona30/main
#33 - Python
2 parents f417bbc + d06497e commit d7ee17e

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from os import system
2+
from pynput.keyboard import Key, Listener
3+
4+
maze = [
5+
["⬜️", "⬜️", "⬜️", "⬜️", "⬜️", "⬛️"],
6+
["⬜️", "⬛️", "⬜️", "⬛️", "⬛️", "⬛️"],
7+
["⬜️", "⬛️", "⬜️", "⬛️", "⬛️", "⬛️"],
8+
["⬜️", "⬛️", "⬜️", "⬜️", "⬜️", "⬜️"],
9+
["🐭", "⬜️", "⬛️", "⬜️", "⬛️", "⬛️"],
10+
["⬜️", "⬛️", "⬛️", "⬜️", "⬜️", "🚪"]
11+
]
12+
13+
mickey_pos = [4, 0]
14+
exit_door = [5, 5]
15+
16+
def draw_maze(maze: list):
17+
system("cls")
18+
for row in maze:
19+
print("".join(row))
20+
21+
def on_press(key: Key):
22+
global mickey_pos
23+
try:
24+
mickey_prev = mickey_pos.copy()
25+
mickey_pos = movements(key, mickey_pos)
26+
27+
if not mickey_pos:
28+
return False
29+
30+
maze[mickey_prev[0]][mickey_prev[1]] = "⬜️"
31+
maze[mickey_pos[0]][mickey_pos[1]] = "🐭"
32+
draw_maze(maze)
33+
34+
if mickey_pos == exit_door:
35+
print("You Won!!")
36+
return False
37+
38+
except AttributeError:
39+
print(f"special key pressed: {key}")
40+
41+
def on_release(key: Key):
42+
if key == Key.esc:
43+
return False
44+
45+
def move_up(current_pos: list) -> list:
46+
y_pos, x_pos = current_pos
47+
if y_pos > 0 and maze[y_pos - 1][x_pos] in ("⬜️", "🚪"):
48+
return [y_pos - 1, x_pos]
49+
return current_pos
50+
51+
def move_down(current_pos: list) -> list:
52+
y_pos, x_pos = current_pos
53+
if y_pos < len(maze) - 1 and maze[y_pos + 1][x_pos] in ("⬜️", "🚪"):
54+
return [y_pos + 1, x_pos]
55+
return current_pos
56+
57+
def move_left(current_pos: list) -> list:
58+
y_pos, x_pos = current_pos
59+
if x_pos > 0 and maze[y_pos][x_pos - 1] in ("⬜️", "🚪"):
60+
return [y_pos, x_pos - 1]
61+
return current_pos
62+
63+
def move_right(current_pos: list) -> list:
64+
y_pos, x_pos = current_pos
65+
if x_pos < len(maze[0]) - 1 and maze[y_pos][x_pos + 1] in ("⬜️", "🚪"):
66+
return [y_pos, x_pos + 1]
67+
return current_pos
68+
69+
def movements(pressed_key: Key, position: list) -> list:
70+
match pressed_key:
71+
case Key.up:
72+
return move_up(position)
73+
case Key.down:
74+
return move_down(position)
75+
case Key.left:
76+
return move_left(position)
77+
case Key.right:
78+
return move_right(position)
79+
case Key.esc:
80+
position.clear()
81+
return position
82+
83+
with Listener(on_press=on_press, on_release=on_release) as listener:
84+
draw_maze(maze)
85+
listener.join()

0 commit comments

Comments
 (0)