o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)
Please provide a Python script for the following request: write the tetris game in python.
- User
Please provide a Python script for the following request: write the tetris game in python.
- Assistant
Sure! Below is a complete Python implementation of the classic Tetris game using the
pygamelibrary. This script includes all the essential features of Tetris, such as tetromino rotation, movement, collision detection, line clearing, and scoring.Prerequisites:
- Python 3.x installed on your computer.
pygamelibrary installed. You can install it using pip:pip install pygame
Tetris Python Script:
import pygame import random # Initialize Pygame pygame.init() # Screen dimensions SCREEN_WIDTH = 300 SCREEN_HEIGHT = 600 BLOCK_SIZE = 30 # Playfield dimensions GRID_WIDTH = 10 GRID_HEIGHT = 20 # Colors BLACK = (0, 0, 0) GRAY = (128, 128, 128) WHITE = (255, 255, 255) CYAN = (0, 255, 255) BLUE = (0, 0, 255) ORANGE = (255, 165, 0) YELLOW = (255, 255, 0) GREEN = (0, 255, 0) PURPLE = (128, 0, 128) RED = (255, 0, 0) COLORS = [CYAN, BLUE, ORANGE, YELLOW, GREEN, PURPLE, RED] # Tetromino shapes TETROMINOS = { 'I': [[(0, -1), (0, 0), (0, 1), (0, 2)], [(-1, 0), (0, 0), (1, 0), (2, 0)]], 'J': [[(-1, -1), (-1, 0), (0, 0), (1, 0)], [(0, -1), (1, -1), (0, 0), (0, 1)], [(-1, 0), (0, 0), (1, 0), (1, 1)], [(0, -1), (0, 0), (-1, 1), (0, 1)]], 'L': [[(1, -1), (-1, 0), (0, 0), (1, 0)], [(0, -1), (0, 0), (0, 1), (1, 1)], [(-1, 0), (0, 0), (1, 0), (-1, 1)], [(0, -1), (0, 0), (0, 1), (-1, 1)]], 'O': [[(0, 0), (1, 0), (0, 1), (1, 1)]], 'S': [[(0, 0), (1, 0), (-1, 1), (0, 1)], [(0, -1), (0, 0), (1, 0), (1, 1)]], 'T': [[(0, -1), (-1, 0), (0, 0), (1, 0)], [(0, -1), (0, 0), (1, 0), (0, 1)], [(-1, 0), (0, 0), (1, 0), (0, 1)], [(0, -1), (-1, 0), (0, 0), (0, 1)]], 'Z': [[(-1, 0), (0, 0), (0, 1), (1, 1)], [(1, -1), (0, 0), (1, 0), (0, 1)]] } # Define the Tetromino class class Tetromino: def __init__(self, shape): self.shape = shape self.rotations = TETROMINOS[shape] self.rotation = 0 self.x = GRID_WIDTH // 2 self.y = 0 self.color = COLORS[list(TETROMINOS.keys()).index(shape)] def get_cells(self): return [(self.x + dx, self.y + dy) for dx, dy in self.rotations[self.rotation]] def rotate(self, grid): new_rotation = (self.rotation + 1) % len(self.rotations) new_cells = [(self.x + dx, self.y + dy) for dx, dy in self.rotations[new_rotation]] if not check_collision(new_cells, grid): self.rotation = new_rotation def move(self, dx, dy, grid): new_x = self.x + dx new_y = self.y + dy new_cells = [(new_x + dx_cell, new_y + dy_cell) for dx_cell, dy_cell in self.rotations[self.rotation]] if not check_collision(new_cells, grid): self.x = new_x self.y = new_y return True return False # Check for collision def check_collision(cells, grid): for x, y in cells: if x < 0 or x >= GRID_WIDTH or y < 0 or y >= GRID_HEIGHT: return True if grid[y][x] != BLACK: return True return False # Clear completed lines def clear_lines(grid): lines_cleared = 0 new_grid = [row for row in grid if any(cell == BLACK for cell in row)] lines_cleared = GRID_HEIGHT - len(new_grid) for _ in range(lines_cleared): new_grid.insert(0, [BLACK for _ in range(GRID_WIDTH)]) return new_grid, lines_cleared # Draw the grid and tetrominoes def draw_grid(screen, grid): for y in range(GRID_HEIGHT): for x in range(GRID_WIDTH): pygame.draw.rect(screen, grid[y][x], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0) pygame.draw.rect(screen, GRAY, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1) # Main game function def main(): screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Tetris") clock = pygame.time.Clock() font = pygame.font.SysFont("Arial", 24) grid = [[BLACK for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)] current_tetromino = Tetromino(random.choice(list(TETROMINOS.keys()))) next_tetromino = Tetromino(random.choice(list(TETROMINOS.keys()))) fall_time = 0 fall_speed = 0.5 # Seconds per block score = 0 game_over = False while True: dt = clock.tick(60) / 1000 # Delta time in seconds fall_time += dt for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return if event.type == pygame.KEYDOWN and not game_over: if event.key == pygame.K_LEFT: current_tetromino.move(-1, 0, grid) elif event.key == pygame.K_RIGHT: current_tetromino.move(1, 0, grid) elif event.key == pygame.K_DOWN: if not current_tetromino.move(0, 1, grid): # Lock the tetromino for x, y in current_tetromino.get_cells(): if y >= 0: grid[y][x] = current_tetromino.color grid, lines = clear_lines(grid) score += lines * 100 current_tetromino = next_tetromino next_tetromino = Tetromino(random.choice(list(TETROMINOS.keys()))) if check_collision(current_tetromino.get_cells(), grid): game_over = True elif event.key == pygame.K_UP: current_tetromino.rotate(grid) if fall_time >= fall_speed and not game_over: if not current_tetromino.move(0, 1, grid): # Lock the tetromino for x, y in current_tetromino.get_cells(): if y >= 0: grid[y][x] = current_tetromino.color grid, lines = clear_lines(grid) score += lines * 100 current_tetromino = next_tetromino next_tetromino = Tetromino(random.choice(list(TETROMINOS.keys()))) if check_collision(current_tetromino.get_cells(), grid): game_over = True fall_time = 0 # Draw everything screen.fill(BLACK) draw_grid(screen, grid) # Draw current tetromino if not game_over: for x, y in current_tetromino.get_cells(): if y >= 0: pygame.draw.rect(screen, current_tetromino.color, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(screen, GRAY, (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1) # Draw score score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (10, 10)) # Draw game over if game_over: over_text = font.render("GAME OVER", True, RED) screen.blit(over_text, (SCREEN_WIDTH // 2 - over_text.get_width() // 2, SCREEN_HEIGHT // 2)) pygame.display.flip() if __name__ == "__main__": main()How to Run the Game:
-
Save the Script: Save the above script in a file named
tetris.py. -
Install Pygame: If you haven't installed
pygameyet, open your terminal or command prompt and run:pip install pygame -
Run the Game: Navigate to the directory containing
tetris.pyand run:python tetris.py
Game Controls:
- Left Arrow: Move tetromino left.
- Right Arrow: Move tetromino right.
- Down Arrow: Move tetromino down faster.
- Up Arrow: Rotate tetromino.
Features:
- Tetromino Rotation: Tetrominoes can rotate clockwise.
- Line Clearing: Complete lines are cleared, and score is updated.
- Scoring System: Each cleared line adds 100 points to the score.
- Game Over Detection: The game ends when new tetrominoes cannot be placed.
Customization:
- Speed Adjustment: Modify the
fall_speedvariable to increase or decrease the game's speed. - Colors and Block Size: Adjust the color definitions and
BLOCK_SIZEto change the appearance.
Enjoy playing your own Tetris game!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.