Create a tetris application.
- User
Create a tetris application.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Create a tetris application.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's a complete, playable Tetris game with all the core mechanics:
import pygame
import random
import sys
# Initialize pygame
pygame.init()
# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 700
GRID_SIZE = 30
GRID_WIDTH = 10
GRID_HEIGHT = 20
SIDEBAR_WIDTH = 250
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 50, 50)
GREEN = (50, 255, 50)
BLUE = (50, 100, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 150, 0)
GRAY = (40, 40, 40)
LIGHT_GRAY = (100, 100, 100)
DARK_BLUE = (10, 20, 40)
# Tetromino shapes
SHAPES = [
[[1, 1, 1, 1]], # I
[[1, 1], [1, 1]], # O
[[0, 1, 0], [1, 1, 1]], # T
[[0, 1, 1], [1, 1, 0]], # S
[[1, 1, 0], [0, 1, 1]], # Z
[[1, 0, 0], [1, 1, 1]], # J
[[0, 0, 1], [1, 1, 1]] # L
]
# Tetromino colors
COLORS = [CYAN, YELLOW, MAGENTA, GREEN, RED, BLUE, ORANGE]
class Tetromino:
def __init__(self, x, y):
self.x = x
self.y = y
self.shape_idx = random.randint(0, len(SHAPES) - 1)
self.shape = SHAPES[self.shape_idx]
self.color = COLORS[self.shape_idx]
self.rotation = 0
def rotate(self):
# Rotate the shape 90 degrees clockwise
rows = len(self.shape)
cols = len(self.shape[0])
rotated = [[0 for _ in range(rows)] for _ in range(cols)]
for r in range(rows):
for c in range(cols):
rotated[c][rows - 1 - r] = self.shape[r][c]
return rotated
class TetrisGame:
def __init__(self):
self.width = GRID_WIDTH
self.height = GRID_HEIGHT
self.grid = [[0 for _ in range(self.width)] for _ in range(self.height)]
self.current_piece = self.new_piece()
self.next_piece = self.new_piece()
self.game_over = False
self.score = 0
self.level = 1
self.lines_cleared = 0
self.fall_speed = 0.5 # seconds per cell
self.fall_time = 0
self.paused = False
self.font = pygame.font.SysFont(None, 36)
self.small_font = pygame.font.SysFont(None, 28)
def new_piece(self):
# Create a new piece at the top center
return Tetromino(self.width // 2 - 1, 0)
def valid_position(self, shape, x, y):
# Check if the piece can be placed at (x, y)
for r, row in enumerate(shape):
for c, cell in enumerate(row):
if cell:
# Calculate position in grid
pos_x, pos_y = x + c, y + r
# Check if out of bounds or overlapping
if (pos_x < 0 or pos_x >= self.width or
pos_y >= self.height or
(pos_y >= 0 and self.grid[pos_y][pos_x])):
return False
return True
def merge_piece(self):
# Merge the current piece into the grid
for r, row in enumerate(self.current_piece.shape):
for c, cell in enumerate(row):
if cell:
pos_x, pos_y = self.current_piece.x + c, self.current_piece.y + r
if 0 <= pos_y < self.height and 0 <= pos_x < self.width:
self.grid[pos_y][pos_x] = self.current_piece.color
def clear_lines(self):
# Clear completed lines and return the number of lines cleared
lines_to_clear = []
for r in range(self.height):
if all(self.grid[r]):
lines_to_clear.append(r)
for line in lines_to_clear:
del self.grid[line]
self.grid.insert(0, [0 for _ in range(self.width)])
# Update score
cleared = len(lines_to_clear)
if cleared > 0:
self.lines_cleared += cleared
self.score += [100, 300, 500, 800][min(cleared-1, 3)] * self.level
# Level up every 10 lines
self.level = self.lines_cleared // 10 + 1
self.fall_speed = max(0.05, 0.5 - (self.level - 1) * 0.05)
return cleared
def move(self, dx, dy):
# Move the current piece
if not self.paused and not self.game_over:
if self.valid_position(self.current_piece.shape, self.current_piece.x + dx, self.current_piece.y + dy):
self.current_piece.x += dx
self.current_piece.y += dy
return True
return False
def rotate_piece(self):
if not self.paused and not self.game_over:
rotated = self.current_piece.rotate()
if self.valid_position(rotated, self.current_piece.x, self.current_piece.y):
self.current_piece.shape = rotated
return True
return False
def drop(self):
if not self.paused and not self.game_over:
while self.move(0, 1):
pass
self.lock_piece()
def lock_piece(self):
self.merge_piece()
self.clear_lines()
self.current_piece = self.next_piece
self.next_piece = self.new_piece()
# Check if game is over
if not self.valid_position(self.current_piece.shape, self.current_piece.x, self.current_piece.y):
self.game_over = True
def update(self, dt):
if self.paused or self.game_over:
return
self.fall_time += dt
if self.fall_time >= self.fall_speed:
self.fall_time = 0
if not self.move(0, 1):
self.lock_piece()
def draw_grid(screen, game):
# Draw the game grid
grid_x = (SCREEN_WIDTH - SIDEBAR_WIDTH - GRID_WIDTH * GRID_SIZE) // 2
grid_y = (SCREEN_HEIGHT - GRID_HEIGHT * GRID_SIZE) // 2
# Draw background
pygame.draw.rect(screen, DARK_BLUE, (grid_x - 5, grid_y - 5,
GRID_WIDTH * GRID_SIZE + 10,
GRID_HEIGHT * GRID_SIZE + 10))
# Draw grid lines
for x in range(GRID_WIDTH + 1):
pygame.draw.line(screen, GRAY,
(grid_x + x * GRID_SIZE, grid_y),
(grid_x + x * GRID_SIZE, grid_y + GRID_HEIGHT * GRID_SIZE))
for y in range(GRID_HEIGHT + 1):
pygame.draw.line(screen, GRAY,
(grid_x, grid_y + y * GRID_SIZE),
(grid_x + GRID_WIDTH * GRID_SIZE, grid_y + y * GRID_SIZE))
# Draw placed blocks
for y, row in enumerate(game.grid):
for x, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, cell,
(grid_x + x * GRID_SIZE, grid_y + y * GRID_SIZE,
GRID_SIZE - 1, GRID_SIZE - 1))
pygame.draw.rect(screen, WHITE,
(grid_x + x * GRID_SIZE, grid_y + y * GRID_SIZE,
GRID_SIZE - 1, GRID_SIZE - 1), 1)
# Draw current piece
if not game.game_over:
for r, row in enumerate(game.current_piece.shape):
for c, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, game.current_piece.color,
(grid_x + (game.current_piece.x + c) * GRID_SIZE,
grid_y + (game.current_piece.y + r) * GRID_SIZE,
GRID_SIZE - 1, GRID_SIZE - 1))
pygame.draw.rect(screen, WHITE,
(grid_x + (game.current_piece.x + c) * GRID_SIZE,
grid_y + (game.current_piece.y + r) * GRID_SIZE,
GRID_SIZE - 1, GRID_SIZE - 1), 1)
return grid_x, grid_y
def draw_sidebar(screen, game):
sidebar_x = SCREEN_WIDTH - SIDEBAR_WIDTH
# Draw sidebar background
pygame.draw.rect(screen, DARK_BLUE, (sidebar_x, 0, SIDEBAR_WIDTH, SCREEN_HEIGHT))
# Draw title
title_font = pygame.font.SysFont(None, 48)
title = title_font.render("TETRIS", True, CYAN)
screen.blit(title, (sidebar_x + (SIDEBAR_WIDTH - title.get_width()) // 2, 40))
# Draw next piece preview
next_text = game.font.render("NEXT", True, WHITE)
screen.blit(next_text, (sidebar_x + (SIDEBAR_WIDTH - next_text.get_width()) // 2, 120))
# Draw next piece
preview_x = sidebar_x + SIDEBAR_WIDTH // 2
preview_y = 180
for r, row in enumerate(game.next_piece.shape):
for c, cell in enumerate(row):
if cell:
pygame.draw.rect(screen, game.next_piece.color,
(preview_x + c * GRID_SIZE - len(game.next_piece.shape[0]) * GRID_SIZE // 2,
preview_y + r * GRID_SIZE,
GRID_SIZE - 1, GRID_SIZE - 1))
pygame.draw.rect(screen, WHITE,
(preview_x + c * GRID_SIZE - len(game.next_piece.shape[0]) * GRID_SIZE // 2,
preview_y + r * GRID_SIZE,
GRID_SIZE - 1, GRID_SIZE - 1), 1)
# Draw score and level
score_text = game.font.render(f"SCORE: {game.score}", True, YELLOW)
screen.blit(score_text, (sidebar_x + 20, 280))
level_text = game.font.render(f"LEVEL: {game.level}", True, GREEN)
screen.blit(level_text, (sidebar_x + 20, 330))
lines_text = game.font.render(f"LINES: {game.lines_cleared}", True, MAGENTA)
screen.blit(lines_text, (sidebar_x + 20, 380))
# Draw controls
controls_y = 450
controls = [
"CONTROLS:",
"← → : Move",
"↑ : Rotate",
"↓ : Soft Drop",
"SPACE : Hard Drop",
"P : Pause",
"R : Restart"
]
for i, text in enumerate(controls):
ctrl_text = game.small_font.render(text, True, LIGHT_GRAY)
screen.blit(ctrl_text, (sidebar_x + 20, controls_y + i * 35))
# Draw game over or paused message
if game.game_over:
game_over = game.font.render("GAME OVER", True, RED)
restart = game.small_font.render("Press R to restart", True, WHITE)
screen.blit(game_over, (sidebar_x + (SIDEBAR_WIDTH - game_over.get_width()) // 2, 600))
screen.blit(restart, (sidebar_x + (SIDEBAR_WIDTH - restart.get_width()) // 2, 640))
elif game.paused:
paused = game.font.render("PAUSED", True, YELLOW)
screen.blit(paused, (sidebar_x + (SIDEBAR_WIDTH - paused.get_width()) // 2, 600))
def main():
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
game = TetrisGame()
# Main game loop
while True:
dt = clock.tick(60) / 1000.0 # Delta time in seconds
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
game.paused = not game.paused
if event.key == pygame.K_r:
game = TetrisGame() # Restart game
if not game.paused and not game.game_over:
if event.key == pygame.K_LEFT:
game.move(-1, 0)
elif event.key == pygame.K_RIGHT:
game.move(1, 0)
elif event.key == pygame.K_DOWN:
game.move(0, 1)
elif event.key == pygame.K_UP:
game.rotate_piece()
elif event.key == pygame.K_SPACE:
game.drop()
# Update game state
game.update(dt)
# Draw everything
screen.fill(BLACK)
# Draw grid and pieces
grid_x, grid_y = draw_grid(screen, game)
# Draw sidebar
draw_sidebar(screen, game)
# Draw grid border
pygame.draw.rect(screen, WHITE,
(grid_x - 5, grid_y - 5,
GRID_WIDTH * GRID_SIZE + 10,
GRID_HEIGHT * GRID_SIZE + 10), 3)
pygame.display.flip()
if __name__ == "__main__":
main()
This Tetris implementation includes:
Core Game Mechanics:
User Interface:
Controls:
Game Flow:
The game gets progressively faster as you clear more lines, making it challenging to keep up with the falling pieces!
Enjoy playing this classic puzzle game!