def rotate(self):
self.shape = [list(row) for row in zip(*self.shape[::-1])]
# グリッドの初期化
grid = [[BLACK for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
def draw_grid():
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
pygame.draw.rect(screen, grid[y][x], (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 0)
pygame.draw.rect(screen, WHITE, (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 1)
def check_collision(shape, offset):
off_x, off_y = offset
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
if x + off_x < 0 or x + off_x >= GRID_WIDTH or y + off_y >= GRID_HEIGHT or grid[y + off_y][x + off_x] != BLACK:
return True
return False
def merge(shape, offset, color):
off_x, off_y = offset
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
grid[y + off_y][x + off_x] = color
def remove_line():
global grid
lines_removed = 0
new_grid = [row for row in grid if any(cell == BLACK for cell in row)]
lines_removed = len(grid) - len(new_grid)
grid = [[BLACK for _ in range(GRID_WIDTH)] for _ in range(lines_removed)] + new_grid
return lines_removed
def game_over():
for cell in grid[0]:
if cell != BLACK:
return True
return False
if speed_increase_time > speed_reset_interval:
speed_increase_time = 0 # 速度増加時間をリセット
if fall_time > fall_speed:
if not check_collision(current_piece.shape, (current_piece.x, current_piece.y + 1)):
current_piece.y += 1
else:
merge(current_piece.shape, (current_piece.x, current_piece.y), current_piece.color)
lines_removed = remove_line()
score += lines_removed * 100
if game_over():
running = False
current_piece = Tetromino()
fall_time = 0
draw_grid()
for y, row in enumerate(current_piece.shape):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, current_piece.color, ((current_piece.x + x) * GRID_SIZE, (current_piece.y + y) * GRID_SIZE, GRID_SIZE, GRID_SIZE), 0)
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
if grid[y][x] != BLACK:
pygame.draw.rect(screen, grid[y][x], (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 0)
draw_text(f"Score: {score}", 20, WHITE, 10, 10)
pygame.display.flip()