# Load images for each tetromino (designed at 120x120 pixels) image_filenames = { 'I': 'I.png', 'J': 'J.png', 'L': 'L.png', 'O': 'O.png', 'S': 'S.png', 'T': 'T.png', 'Z': 'Z.png' }
# Scale images to the desired block size images = {key: pygame.transform.scale(pygame.image.load(filename), (block_size, block_size)) for key, filename in image_filenames.items()}
defrotate(self): self.shape = [list(row) for row in zip(*self.shape[::-1])]
defdraw_grid(): for y in range(grid_height): for x in range(grid_width): if grid[y][x] != 0: screen.blit(images[grid[y][x]], (x * block_size, y * block_size)) pygame.draw.rect(screen, (255, 255, 255), (x * block_size, y * block_size, block_size, block_size), 1)
defcheck_collision(shape, offset): off_x, off_y = offset for y, row in enumerate(shape): for x, cell in enumerate(row): if cell and (x + off_x 0 or x + off_x >= grid_width or y + off_y >= grid_height or grid[y + off_y][x + off_x]): returnTrue returnFalse
defmerge(shape, offset, image_key): 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] = image_key
defclear_lines(): global grid lines_cleared = 0 new_grid = [] for row in grid: if all(cell != 0for cell in row): lines_cleared += 1 else: new_grid.append(row) new_grid = [[0for _ in range(grid_width)] for _ in range(lines_cleared)] + new_grid grid = new_grid return lines_cleared
defdraw_tetromino(tetromino): for y, row in enumerate(tetromino.shape): for x, cell in enumerate(row): if cell: screen.blit(images[tetromino.image_key], ((tetromino.x + x) * block_size, (tetromino.y + y) * block_size))
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.MOUSEBUTTONDOWN: if start_button_rect.collidepoint(event.pos): return
defmain(): global grid grid = [[0for _ in range(grid_width)] for _ in range(grid_height)] current_tetromino = Tetromino(*random.choice(shapes)) next_tetromino = Tetromino(*random.choice(shapes)) fall_time = 0 fall_speed = 500# Initial fall speed in milliseconds score = 0 level = 1 lines_cleared = 0
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: current_tetromino.x -= 1 if check_collision(current_tetromino.shape, (current_tetromino.x, current_tetromino.y)): current_tetromino.x += 1 if event.key == pygame.K_RIGHT: current_tetromino.x += 1 if check_collision(current_tetromino.shape, (current_tetromino.x, current_tetromino.y)): current_tetromino.x -= 1 if event.key == pygame.K_DOWN: current_tetromino.y += 1 if check_collision(current_tetromino.shape, (current_tetromino.x, current_tetromino.y)): current_tetromino.y -= 1 if event.key == pygame.K_UP: current_tetromino.rotate() if check_collision(current_tetromino.shape, (current_tetromino.x, current_tetromino.y)): current_tetromino.rotate() current_tetromino.rotate() current_tetromino.rotate()
keys = pygame.key.get_pressed() if keys[pygame.K_DOWN]: fall_speed = 50# Increase fall speed when down key is pressed else: fall_speed = 500# Default fall speed