-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1755702000.py
More file actions
198 lines (172 loc) · 7.73 KB
/
Copy path1755702000.py
File metadata and controls
198 lines (172 loc) · 7.73 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import pyxel
class App:
def __init__(self):
self.BOARD_WIDTH = 10
self.BOARD_HEIGHT = 20
self.BLOCK_SIZE = 8
self.WINDOW_WIDTH = self.BOARD_WIDTH * self.BLOCK_SIZE + 80
self.WINDOW_HEIGHT = self.BOARD_HEIGHT * self.BLOCK_SIZE
self.COLORS = [0, 3, 1, 2, 4, 5, 6, 7]
self.TETROMINOS = [
{"shape": [[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], "color": 1},
{"shape": [[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]], "color": 2},
{"shape": [[0, 0, 0, 0], [1, 1, 1, 0], [0, 1, 0, 0], [0, 0, 0, 0]], "color": 3},
{"shape": [[0, 0, 0, 0], [0, 1, 1, 0], [1, 1, 0, 0], [0, 0, 0, 0]], "color": 4},
{"shape": [[0, 0, 0, 0], [1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 0]], "color": 5},
{"shape": [[0, 0, 0, 0], [1, 0, 0, 0], [1, 1, 1, 0], [0, 0, 0, 0]], "color": 6},
{"shape": [[0, 0, 0, 0], [0, 0, 1, 0], [1, 1, 1, 0], [0, 0, 0, 0]], "color": 7},
]
pyxel.init(self.WINDOW_WIDTH, self.WINDOW_HEIGHT, title="Pyxel Tetromino")
self.reset_game()
pyxel.run(self.update, self.draw)
def reset_game(self):
self.board = [[0 for _ in range(self.BOARD_WIDTH)] for _ in range(self.BOARD_HEIGHT)]
self.current_piece = self.new_piece()
self.next_piece = self.new_piece()
self.piece_x = self.BOARD_WIDTH // 2 - 2
self.piece_y = 0
self.score = 0
self.game_over = False
self.fall_speed = 30
self.fall_timer = 0
def new_piece(self):
return self.TETROMINOS[pyxel.rndi(0, len(self.TETROMINOS) - 1)].copy()
def rotate_piece(self, piece):
new_shape = [[0 for _ in range(4)] for _ in range(4)]
for y in range(4):
for x in range(4):
new_shape[x][3 - y] = piece["shape"][y][x]
return {"shape": new_shape, "color": piece["color"]}
def check_collision(self, piece, x_offset, y_offset):
for y in range(4):
for x in range(4):
if piece["shape"][y][x] != 0:
board_x = self.piece_x + x + x_offset
board_y = self.piece_y + y + y_offset
if not (0 <= board_x < self.BOARD_WIDTH and 0 <= board_y < self.BOARD_HEIGHT):
return True
if self.board[board_y][board_x] != 0:
return True
return False
def merge_piece(self):
for y in range(4):
for x in range(4):
if self.current_piece["shape"][y][x] != 0:
self.board[self.piece_y + y][self.piece_x + x] = self.current_piece["color"]
def clear_lines(self):
lines_cleared = 0
new_board = []
for r in range(self.BOARD_HEIGHT):
if 0 not in self.board[r]:
lines_cleared += 1
else:
new_board.append(self.board[r])
for _ in range(lines_cleared):
new_board.insert(0, [0 for _ in range(self.BOARD_WIDTH)])
self.board = new_board
if lines_cleared > 0:
if lines_cleared == 1:
self.score += 100
elif lines_cleared == 2:
self.score += 300
elif lines_cleared == 3:
self.score += 500
elif lines_cleared == 4:
self.score += 800
def update(self):
if self.game_over:
if pyxel.btnp(pyxel.KEY_RETURN):
self.reset_game()
return
self.fall_timer += 1
if self.fall_timer >= self.fall_speed:
if not self.check_collision(self.current_piece, 0, 1):
self.piece_y += 1
else:
self.merge_piece()
self.clear_lines()
self.current_piece = self.next_piece
self.next_piece = self.new_piece()
self.piece_x = self.BOARD_WIDTH // 2 - 2
self.piece_y = 0
if self.check_collision(self.current_piece, 0, 0):
self.game_over = True
self.fall_timer = 0
if pyxel.btnp(pyxel.KEY_LEFT, 0, 5):
if not self.check_collision(self.current_piece, -1, 0):
self.piece_x -= 1
if pyxel.btnp(pyxel.KEY_RIGHT, 0, 5):
if not self.check_collision(self.current_piece, 1, 0):
self.piece_x += 1
if pyxel.btnp(pyxel.KEY_DOWN, 0, 2):
if not self.check_collision(self.current_piece, 0, 1):
self.piece_y += 1
self.fall_timer = 0
if pyxel.btnp(pyxel.KEY_UP) or pyxel.btnp(pyxel.KEY_X):
rotated_piece = self.rotate_piece(self.current_piece)
if not self.check_collision(rotated_piece, 0, 0):
self.current_piece = rotated_piece
if pyxel.btnp(pyxel.KEY_SPACE):
while not self.check_collision(self.current_piece, 0, 1):
self.piece_y += 1
self.merge_piece()
self.clear_lines()
self.current_piece = self.next_piece
self.next_piece = self.new_piece()
self.piece_x = self.BOARD_WIDTH // 2 - 2
self.piece_y = 0
if self.check_collision(self.current_piece, 0, 0):
self.game_over = True
def draw(self):
pyxel.cls(0)
for y in range(self.BOARD_HEIGHT):
for x in range(self.BOARD_WIDTH):
color = self.board[y][x]
if color != 0:
pyxel.rect(
x * self.BLOCK_SIZE, y * self.BLOCK_SIZE, self.BLOCK_SIZE, self.BLOCK_SIZE, self.COLORS[color]
)
pyxel.rectb(x * self.BLOCK_SIZE, y * self.BLOCK_SIZE, self.BLOCK_SIZE, self.BLOCK_SIZE, 0)
for y in range(4):
for x in range(4):
if self.current_piece["shape"][y][x] != 0:
color = self.current_piece["color"]
pyxel.rect(
(self.piece_x + x) * self.BLOCK_SIZE,
(self.piece_y + y) * self.BLOCK_SIZE,
self.BLOCK_SIZE,
self.BLOCK_SIZE,
self.COLORS[color],
)
pyxel.rectb(
(self.piece_x + x) * self.BLOCK_SIZE,
(self.piece_y + y) * self.BLOCK_SIZE,
self.BLOCK_SIZE,
self.BLOCK_SIZE,
0,
)
pyxel.text(self.BOARD_WIDTH * self.BLOCK_SIZE + 10, 10, "NEXT:", 7)
for y in range(4):
for x in range(4):
if self.next_piece["shape"][y][x] != 0:
color = self.next_piece["color"]
pyxel.rect(
self.BOARD_WIDTH * self.BLOCK_SIZE + 10 + x * self.BLOCK_SIZE,
30 + y * self.BLOCK_SIZE,
self.BLOCK_SIZE,
self.BLOCK_SIZE,
self.COLORS[color],
)
pyxel.rectb(
self.BOARD_WIDTH * self.BLOCK_SIZE + 10 + x * self.BLOCK_SIZE,
30 + y * self.BLOCK_SIZE,
self.BLOCK_SIZE,
self.BLOCK_SIZE,
0,
)
pyxel.text(self.BOARD_WIDTH * self.BLOCK_SIZE + 10, 80, f"SCORE: {self.score}", 7)
if self.game_over:
pyxel.rect(0, self.WINDOW_HEIGHT // 2 - 20, self.WINDOW_WIDTH, 40, 0)
pyxel.text(self.WINDOW_WIDTH // 2 - 30, self.WINDOW_HEIGHT // 2 - 10, "GAME OVER", 7)
pyxel.text(self.WINDOW_WIDTH // 2 - 65, self.WINDOW_HEIGHT // 2 + 5, "Press 'Enter' to Restart", 7)
App()