Create the tetris game in python.
- User
Create the tetris game in python.
- 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 the tetris game in python.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a Tetris game in Python is a fun and educational project that involves a good understanding of game loops, graphical user interfaces, and handling events. One popular library for creating simple 2D games in Python is Pygame. Below is a complete example of a simple Tetris game implemented using Pygame. Make sure you have Pygame installed in your environment (pip install pygame) before running the code.
import pygame
import random
pygame.init()
# Screen dimensions
SCREEN_WIDTH = 300
SCREEN_HEIGHT = 600
GRID_SIZE = 30
GRID_WIDTH = SCREEN_WIDTH // GRID_SIZE
GRID_HEIGHT = SCREEN_HEIGHT // GRID_SIZE
# Colors
COLORS = [
(0, 0, 0), # Black
(255, 0, 0), # Red
(0, 255, 0), # Green
(0, 0, 255), # Blue
(255, 255, 0), # Yellow
(0, 255, 255), # Cyan
(255, 0, 255), # Magenta
]
# Tetromino shapes
SHAPES = [
[[1, 1, 1], [0, 1, 0]], # T
[[1, 1, 1, 1]], # I
[[1, 1], [1, 1]], # O
[[1, 1, 0], [0, 1, 1]], # S
[[0, 1, 1], [1, 1, 0]], # Z
[[1, 0, 0], [1, 1, 1]], # J
[[0, 0, 1], [1, 1, 1]], # L
]
# Tetromino class
class Tetromino:
def __init__(self, x, y):
self.x = x
self.y = y
self.shape = random.choice(SHAPES)
self.color = random.randint(1, len(COLORS) - 1)
self.rotation = 0
def image(self):
return self.shape[self.rotation]
def rotate(self):
self.rotation = (self.rotation + 1) % len(self.shape)
# Check if the current position is valid
def is_valid(grid, shape, offset):
offset_x, offset_y = offset
for y, row in enumerate(shape):
for x, cell in enumerate(row):
if cell:
try:
if x + offset_x < 0 or y + offset_y < 0:
return False
if grid[y + offset_y][x + offset_x]:
return False
except IndexError:
return False
return True
# Remove full lines from the grid
def clear_lines(grid, locked):
index = 0
for i in range(len(grid) - 1, -1, -1):
if 0 not in grid[i]:
del grid[i]
grid.insert(0, [0 for _ in range(GRID_WIDTH)])
index += 1
return index
# Draw the grid
def draw_grid(surface, grid):
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
pygame.draw.rect(surface, COLORS[grid[y][x]], (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE), 0)
for x in range(GRID_WIDTH):
pygame.draw.line(surface, (128, 128, 128), (x * GRID_SIZE, 0), (x * GRID_SIZE, SCREEN_HEIGHT))
for y in range(GRID_HEIGHT):
pygame.draw.line(surface, (128, 128, 128), (0, y * GRID_SIZE), (SCREEN_WIDTH, y * GRID_SIZE))
# Main function
def main():
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Tetris')
clock = pygame.time.Clock()
grid = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
change_piece = False
run = True
current_piece = Tetromino(GRID_WIDTH // 2, 0)
next_piece = Tetromino(GRID_WIDTH // 2, 0)
fall_time = 0
score = 0
while run:
grid_copy = [row[:] for row in grid]
fall_speed = 0.27
fall_time += clock.get_rawtime()
clock.tick()
if fall_time / 1000 > fall_speed:
fall_time = 0
current_piece.y += 1
if not is_valid(grid_copy, current_piece.image(), (current_piece.x, current_piece.y)):
current_piece.y -= 1
change_piece = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
current_piece.x -= 1
if not is_valid(grid_copy, current_piece.image(), (current_piece.x, current_piece.y)):
current_piece.x += 1
if event.key == pygame.K_RIGHT:
current_piece.x += 1
if not is_valid(grid_copy, current_piece.image(), (current_piece.x, current_piece.y)):
current_piece.x -= 1
if event.key == pygame.K_DOWN:
current_piece.y += 1
if not is_valid(grid_copy, current_piece.image(), (current_piece.x, current_piece.y)):
current_piece.y -= 1
if event.key == pygame.K_UP:
current_piece.rotate()
if not is_valid(grid_copy, current_piece.image(), (current_piece.x, current_piece.y)):
current_piece.rotation = (current_piece.rotation - 1) % len(current_piece.shape)
shape_pos = [(y + current_piece.y, x + current_piece.x)
for y, row in enumerate(current_piece.image())
for x, cell in enumerate(row) if cell]
for pos in shape_pos:
p = (pos[0], pos[1])
if p[0] > -1:
grid_copy[p[0]][p[1]] = current_piece.color
if change_piece:
for pos in shape_pos:
p = (pos[0], pos[1])
grid[p[0]][p[1]] = current_piece.color
current_piece = next_piece
next_piece = Tetromino(GRID_WIDTH // 2, 0)
change_piece = False
score += clear_lines(grid, grid_copy) * 10
draw_grid(screen, grid_copy)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()
This code sets up a basic Tetris game. It defines tetromino shapes, colors, and checks for valid moves. The main game loop handles game progression, input events, and rendering the grid and shapes. You can expand upon this by implementing features such as displaying the next piece, increasing speed over time, or adding a scoring system.
This example should work directly by copying it into a Python script and running it. Adjustments may be needed for enhancements or modifications.