This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
95 lines (74 loc) · 3.66 KB
/
mouredev.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
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
from enum import Enum
import keyboard
class Movement(Enum):
DOWN = 1
RIGHT = 2
LEFT = 3
ROTATE = 4
def tetris():
screen = [["🔳", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔳", "🔳", "🔳", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"],
["🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲", "🔲"]]
print_screen(screen)
rotation = 0
while(True):
event = keyboard.read_event()
if event.name == "esc":
break
elif event.event_type == keyboard.KEY_DOWN:
if event.name == "down":
(screen, rotation) = move_piece(screen, Movement.DOWN, rotation)
elif event.name == "right":
(screen, rotation) = move_piece(screen, Movement.RIGHT, rotation)
elif event.name == "left":
(screen, rotation) = move_piece(screen, Movement.LEFT, rotation)
elif event.name == "space":
(screen, rotation) = move_piece(screen, Movement.ROTATE, rotation)
def move_piece(screen: list, movement: Movement, rotation: int) -> (list, int):
new_screen = [["🔲"] * 10 for _ in range(10)]
rotation_item = 0
rotations = [[(1, 1), (0, 0), (-2, 0), (-1, -1)],
[(0, 1), (-1, 0), (0, -1), (1, -2)],
[(0, 2), (1, 1), (-1, 1), (-2, 0)],
[(0, 1), (1, 0), (2, -1), (1, -2)]]
new_rotation = rotation
if movement is Movement.ROTATE:
new_rotation = 0 if rotation == 3 else rotation + 1
for row_index, row in enumerate(screen):
for column_index, item in enumerate(row):
if item == "🔳":
new_row_index = 0
new_column_index = 0
match movement:
case Movement.DOWN:
new_row_index = row_index + 1
new_column_index = column_index
case Movement.RIGHT:
new_row_index = row_index
new_column_index = column_index + 1
case Movement.LEFT:
new_row_index = row_index
new_column_index = column_index - 1
case Movement.ROTATE:
new_row_index = row_index + rotations[new_rotation][rotation_item][0]
new_column_index = column_index + rotations[new_rotation][rotation_item][1]
rotation_item += 1
if new_row_index > 9 or new_column_index > 9 or new_column_index < 0:
print("\nNo se puede realizar el movimiento")
return (screen, rotation)
else:
new_screen[new_row_index][new_column_index] = "🔳"
print_screen(new_screen)
return (new_screen, new_rotation)
def print_screen(screen: list):
print("\nPantalla Tetris:\n")
for row in screen:
print("".join(map(str, row)))
tetris()