-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
109 lines (77 loc) · 2.67 KB
/
main.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from random import choice
import keyboard
from Board import Board
from Shape import Shape
from tetris_shapes import SHAPES
def get_random_shape():
return choice(SHAPES)
def main():
# game logic
ROWS = 20
COLS = 10
is_game_over = False
board = Board(w=COLS, h=ROWS)
shape = Shape(get_random_shape())
# keyboard logic
moves = {
"right": (0, 1), # coordenadas (y, x)
"left": (0, -1),
"bottom": (1, 0)
}
is_colliding = False
while (not is_game_over):
board.mark_deaths()
if board.is_game_over():
board.print_game_over_screen()
break
board.print_board(shape)
# check if exist lines completed
while (board.exist_lines_completed()):
board.remove_lines_completed()
y, x = moves["bottom"]
is_colliding = shape.check_if_next_move_is_colliding(y, x, board)
if not is_colliding:
shape.move_bottom()
else:
board.tomb.append(shape.normalized_coordinates)
del shape
shape = Shape(get_random_shape())
try:
event = keyboard.read_event()
if event.name == 'q':
break # Sale del bucle si la tecla "q" está presionada
elif event.name == 'j':
y, x = moves["bottom"]
is_bottom_colliding = shape.check_if_next_move_is_colliding(0, 0, board)
if not is_bottom_colliding:
shape.move_bottom(0, 0)
else:
continue
elif event.name == 'h':
y, x = moves["left"]
is_left_colliding = shape.check_if_next_move_is_colliding(y, x, board)
if not is_left_colliding:
shape.move_left()
else:
continue
elif event.name == 'l':
y, x = moves["right"]
is_colliding = shape.check_if_next_move_is_colliding(y, x, board)
if not is_colliding:
shape.move_right()
else:
continue
elif event.name == 'k':
y, x = moves["left"]
is_left_colliding = shape.check_if_next_move_is_colliding(y, x, board)
if is_left_colliding:
continue
y, x = moves["right"]
is_right_colliding = shape.check_if_next_move_is_colliding(y, x, board)
if is_right_colliding:
continue
shape.rotate_move()
except:
break
if __name__ == "__main__":
main()