Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,116 @@
|
|
1 |
import streamlit as st
|
2 |
-
import
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import time
|
4 |
+
|
5 |
+
# Define game board size
|
6 |
+
BOARD_HEIGHT = 20
|
7 |
+
BOARD_WIDTH = 10
|
8 |
+
|
9 |
+
# Define shapes of tetrominoes
|
10 |
+
SHAPES = [
|
11 |
+
np.array([
|
12 |
+
[1, 1],
|
13 |
+
[1, 1]
|
14 |
+
]),
|
15 |
+
|
16 |
+
np.array([
|
17 |
+
[1, 1, 1],
|
18 |
+
[0, 1, 0]
|
19 |
+
]),
|
20 |
+
|
21 |
+
np.array([
|
22 |
+
[1, 1, 1],
|
23 |
+
[1, 0, 0]
|
24 |
+
]),
|
25 |
+
|
26 |
+
np.array([
|
27 |
+
[1, 1, 1],
|
28 |
+
[0, 0, 1]
|
29 |
+
]),
|
30 |
+
|
31 |
+
np.array([
|
32 |
+
[1, 1, 0],
|
33 |
+
[0, 1, 1]
|
34 |
+
]),
|
35 |
+
|
36 |
+
np.array([
|
37 |
+
[0, 1, 1],
|
38 |
+
[1, 1, 0]
|
39 |
+
]),
|
40 |
+
|
41 |
+
np.array([
|
42 |
+
[1, 1, 1, 1]
|
43 |
+
])
|
44 |
+
]
|
45 |
+
|
46 |
+
# Define colors for each tetromino
|
47 |
+
COLORS = [
|
48 |
+
"yellow",
|
49 |
+
"orange",
|
50 |
+
"cyan",
|
51 |
+
"blue",
|
52 |
+
"purple",
|
53 |
+
"green",
|
54 |
+
"red"
|
55 |
+
]
|
56 |
+
|
57 |
+
# Define initial game state
|
58 |
+
board = np.zeros((BOARD_HEIGHT, BOARD_WIDTH))
|
59 |
+
current_shape = None
|
60 |
+
current_shape_pos = (0, 0)
|
61 |
+
current_shape_color = None
|
62 |
+
score = 0
|
63 |
+
game_over = False
|
64 |
+
|
65 |
+
def get_random_shape():
|
66 |
+
shape_idx = np.random.randint(len(SHAPES))
|
67 |
+
shape = SHAPES[shape_idx]
|
68 |
+
color = COLORS[shape_idx]
|
69 |
+
return shape, color
|
70 |
+
|
71 |
+
def place_shape_on_board(board, shape, position, color):
|
72 |
+
shape_height, shape_width = shape.shape
|
73 |
+
board_height, board_width = board.shape
|
74 |
+
|
75 |
+
# Check if shape can fit on the board at the given position
|
76 |
+
if position[0] + shape_height > board_height or position[1] + shape_width > board_width:
|
77 |
+
return False
|
78 |
+
|
79 |
+
# Check if there are any overlapping blocks
|
80 |
+
if np.any(board[position[0]:position[0]+shape_height, position[1]:position[1]+shape_width] * shape):
|
81 |
+
return False
|
82 |
+
|
83 |
+
# Place shape on the board
|
84 |
+
board[position[0]:position[0]+shape_height, position[1]:position[1]+shape_width] += shape * color
|
85 |
+
|
86 |
+
return True
|
87 |
+
|
88 |
+
def check_if_row_is_complete(board, row):
|
89 |
+
return np.all(board[row, :])
|
90 |
+
|
91 |
+
def remove_row(board, row):
|
92 |
+
board[row+1:,:] = board[row:-1,:]
|
93 |
+
board[0,:] = 0
|
94 |
+
|
95 |
+
def remove_completed_rows(board):
|
96 |
+
for row in range(board.shape[0]):
|
97 |
+
if check_if_row_is_complete(board, row):
|
98 |
+
remove_row(board, row)
|
99 |
+
|
100 |
+
def update_game_state():
|
101 |
+
global board, current_shape, current_shape_pos, current_shape_color, score, game_over
|
102 |
+
|
103 |
+
# Move shape down by one position
|
104 |
+
new_shape_pos = (current_shape_pos[0] + 1, current_shape_pos[1])
|
105 |
+
|
106 |
+
# Check if shape can be placed on the board
|
107 |
+
if not place_shape_on_board(board, current_shape, new_shape_pos, current_shape_color):
|
108 |
+
# Shape cannot be placed, lock it in place
|
109 |
+
place_shape_on_board(board, current_shape, current_shape_pos, current_shape_color)
|
110 |
+
|
111 |
+
# Check if any rows have been completed
|
112 |
+
remove_completed_rows(board)
|
113 |
+
|
114 |
+
# Check if game is over
|
115 |
+
if np.any(board[0,:]):
|
116 |
+
game_over = True
|