Give me the best Tetris clone you can
- User
Give me the best Tetris clone you can
Assistant
Here's a basic Tetris clone implemented in Python using Pygame. This version includes core Tetris mechanics, scoring, and game over conditions:
import pygame import random # Initialize Pygame pygame.init() # Screen dimensions SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 GRID_WIDTH = 10 GRID_HEIGHT = 20 BLOCK_SIZE = 30 # Colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) COLORS = [ (0, 255, 255), # Cyan (I piece) (255, 165, 0), # Orange (L piece) (0, 0, 255), # Blue (J piece) (255, 255, 0), # Yellow (O piece) (0, 255, 0), # Green (S piece) (255, 0, 0), # Red (Z piece) (128, 0, 128) # Purple (T piece) ] # Tetromino shapes SHAPES = [ [[1, 1, 1, 1]], # I [[1, 0, 0], [1, 1, 1]], # L [[0, 0, 1], [1, 1, 1]], # J [[1, 1], [1, 1]], # O [[0, 1, 1], [1, 1, 0]], # S [[1, 1, 0], [0, 1, 1]], # Z [[0, 1, 0], [1, 1, 1]] # T ] class Tetris: def __init__(self): self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Tetris Clone") self.clock = pygame.time.Clock() self.font = pygame.font.Font(None, 36) self.reset_game() def reset_game(self): self.grid = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)] self.current_piece = self.get_new_piece() self.piece_x = GRID_WIDTH // 2 - len(self.current_piece[0]) // 2 self.piece_y = 0 self.score = 0 self.game_over = False self.fall_time = 0 self.fall_speed = 0.5 # seconds per drop def get_new_piece(self): shape = random.choice(SHAPES) color = random.choice(COLORS) return shape, color def draw_grid(self): for y in range(GRID_HEIGHT): for x in range(GRID_WIDTH): rect = pygame.Rect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(self.screen, WHITE, rect, 1) if self.grid[y][x]: pygame.draw.rect(self.screen, self.grid[y][x], rect) def draw_piece(self, piece, color, offset_x, offset_y): for y, row in enumerate(piece): for x, cell in enumerate(row): if cell: rect = pygame.Rect((offset_x + x) * BLOCK_SIZE, (offset_y + y) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(self.screen, color, rect) pygame.draw.rect(self.screen, WHITE, rect, 1) def is_valid_move(self, piece, x, y): for row_idx, row in enumerate(piece): for col_idx, cell in enumerate(row): if cell: grid_x = x + col_idx grid_y = y + row_idx if (grid_x < 0 or grid_x >= GRID_WIDTH or grid_y >= GRID_HEIGHT or (grid_y >= 0 and self.grid[grid_y][grid_x])): return False return True def place_piece(self): for y, row in enumerate(self.current_piece[0]): for x, cell in enumerate(row): if cell: self.grid[self.piece_y + y][self.piece_x + x] = self.current_piece[1] self.clear_lines() self.current_piece = self.get_new_piece() self.piece_x = GRID_WIDTH // 2 - len(self.current_piece[0][0]) // 2 self.piece_y = 0 if not self.is_valid_move(self.current_piece[0], self.piece_x, self.piece_y): self.game_over = True def clear_lines(self): lines_cleared = 0 y = GRID_HEIGHT - 1 while y >= 0: if all(self.grid[y]): del self.grid[y] self.grid.insert(0, [0 for _ in range(GRID_WIDTH)]) lines_cleared += 1 else: y -= 1 # Score calculation 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 rotate_piece(self): rotated = list(zip(*self.current_piece[0][::-1])) if self.is_valid_move(rotated, self.piece_x, self.piece_y): self.current_piece = (rotated, self.current_piece[1]) def run(self): while True: self.fall_time += self.clock.get_rawtime() self.clock.tick() for event in pygame.event.get(): if event.type == pygame.QUIT: return if event.type == pygame.KEYDOWN and not self.game_over: if event.key == pygame.K_LEFT: if self.is_valid_move(self.current_piece[0], self.piece_x - 1, self.piece_y): self.piece_x -= 1 elif event.key == pygame.K_RIGHT: if self.is_valid_move(self.current_piece[0], self.piece_x + 1, self.piece_y): self.piece_x += 1 elif event.key == pygame.K_DOWN: if self.is_valid_move(self.current_piece[0], self.piece_x, self.piece_y + 1): self.piece_y += 1 elif event.key == pygame.K_UP: self.rotate_piece() elif event.key == pygame.K_SPACE: while self.is_valid_move(self.current_piece[0], self.piece_x, self.piece_y + 1): self.piece_y += 1 self.place_piece() elif event.type == pygame.KEYDOWN and self.game_over: if event.key == pygame.K_r: self.reset_game() # Automatic piece drop if self.fall_time/1000 > self.fall_speed and not self.game_over: if self.is_valid_move(self.current_piece[0], self.piece_x, self.piece_y + 1): self.piece_y += 1 else: self.place_piece() self.fall_time = 0 # Drawing self.screen.fill(BLACK) self.draw_grid() self.draw_piece(self.current_piece[0], self.current_piece[1], self.piece_x, self.piece_y) # Score display score_text = self.font.render(f"Score: {self.score}", True, WHITE) self.screen.blit(score_text, (SCREEN_WIDTH - 150, 20)) # Game over display if self.game_over: game_over_text = self.font.render("GAME OVER (Press R to Restart)", True, WHITE) self.screen.blit(game_over_text, (SCREEN_WIDTH//2 - 200, SCREEN_HEIGHT//2)) pygame.display.flip() # Run the game if __name__ == "__main__": game = Tetris() game.run() pygame.quit()This Tetris clone includes:
- All 7 standard Tetris pieces
- Piece rotation
- Line clearing
- Scoring system
- Game over detection
- Restart functionality
Controls:
- Left/Right Arrow: Move piece
- Up Arrow: Rotate piece
- Down Arrow: Soft drop
- Space: Hard drop
- R (when game over): Restart game
Features:
- Randomized piece selection
- Colored pieces
- Basic grid and piece rendering
- Score tracking
Potential improvements could include:
- Next piece preview
- Difficulty progression
- High score tracking
- Sound effects
Enjoy playing!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.