I'll create a Tetris clone using Python and Pygame. Here's a complete implementation with score, next piece preview, and level display:
import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Constants
GRID_WIDTH = 10
GRID_HEIGHT = 20
CELL_SIZE = 30
GRID_X_OFFSET = 50
GRID_Y_OFFSET = 50
# Colors
BLACK = (0, 0, 0)
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)
GRAY = (128, 128, 128)
# Tetris pieces (tetrominoes)
PIECES = {
'I': [['.....',
'..#..',
'..#..',
'..#..',
'..#..'],
['.....',
'.....',
'####.',
'.....',
'.....']],
'O': [['.....',
'.....',
'.##..',
'.##..',
'.....']],
'T': [['.....',
'.....',
'.#...',
'###..',
'.....'],
['.....',
'.....',
'.#...',
'.##..',
'.#...'],
['.....',
'.....',
'.....',
'###..',
'.#...'],
['.....',
'.....',
'.#...',
'##...',
'.#...']],
'S': [['.....',
'.....',
'.##..',
'##...',
'.....'],
['.....',
'.#...',
'.##..',
'..#..',
'.....']],
'Z': [['.....',
'.....',
'##...',
'.##..',
'.....'],
['.....',
'..#..',
'.##..',
'.#...',
'.....']],
'J': [['.....',
'.#...',
'.#...',
'##...',
'.....'],
['.....',
'.....',
'#....',
'###..',
'.....'],
['.....',
'.##..',
'.#...',
'.#...',
'.....'],
['.....',
'.....',
'###..',
'..#..',
'.....']],
'L': [['.....',
'..#..',
'..#..',
'.##..',
'.....'],
['.....',
'.....',
'###..',
'#....',
'.....'],
['.....',
'##...',
'.#...',
'.#...',
'.....'],
['.....',
'.....',
'..#..',
'###..',
'.....']]
}
PIECE_COLORS = {
'I': CYAN,
'O': YELLOW,
'T': PURPLE,
'S': GREEN,
'Z': RED,
'J': BLUE,
'L': ORANGE
}
class Piece:
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
self.color = PIECE_COLORS[shape]
self.rotation = 0
def get_rotated_piece(self):
return PIECES[self.shape][self.rotation % len(PIECES[self.shape])]
class Tetris:
def __init__(self):
self.grid = [[BLACK for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
self.current_piece = self.get_new_piece()
self.next_piece = self.get_new_piece()
self.score = 0
self.level = 1
self.lines_cleared = 0
self.fall_time = 0
self.fall_speed = 500 # milliseconds
# Set up display
self.screen_width = GRID_WIDTH * CELL_SIZE + 300
self.screen_height = GRID_HEIGHT * CELL_SIZE + 100
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
pygame.display.set_caption("Tetris")
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(None, 36)
def get_new_piece(self):
shape = random.choice(list(PIECES.keys()))
return Piece(GRID_WIDTH // 2 - 2, 0, shape)
def is_valid_position(self, piece, dx=0, dy=0, rotation=None):
if rotation is None:
rotation = piece.rotation
piece_pattern = PIECES[piece.shape][rotation % len(PIECES[piece.shape])]
for y, row in enumerate(piece_pattern):
for x, cell in enumerate(row):
if cell == '#':
new_x = piece.x + x + dx
new_y = piece.y + y + dy
if (new_x < 0 or new_x >= GRID_WIDTH or
new_y >= GRID_HEIGHT or
(new_y >= 0 and self.grid[new_y][new_x] != BLACK)):
return False
return True
def place_piece(self, piece):
piece_pattern = piece.get_rotated_piece()
for y, row in enumerate(piece_pattern):
for x, cell in enumerate(row):
if cell == '#':
grid_x = piece.x + x
grid_y = piece.y + y
if grid_y >= 0:
self.grid[grid_y][grid_x] = piece.color
def clear_lines(self):
lines_to_clear = []
for y in range(GRID_HEIGHT):
if all(cell != BLACK for cell in self.grid[y]):
lines_to_clear.append(y)
for y in lines_to_clear:
del self.grid[y]
self.grid.insert(0, [BLACK for _ in range(GRID_WIDTH)])
lines_cleared = len(lines_to_clear)
if lines_cleared > 0:
self.lines_cleared += lines_cleared
self.score += lines_cleared * 100 * self.level
self.level = self.lines_cleared // 10 + 1
self.fall_speed = max(50, 500 - (self.level - 1) * 50)
def move_piece(self, dx, dy):
if self.is_valid_position(self.current_piece, dx, dy):
self.current_piece.x += dx
self.current_piece.y += dy
return True
return False
def rotate_piece(self):
new_rotation = (self.current_piece.rotation + 1) % len(PIECES[self.current_piece.shape])
if self.is_valid_position(self.current_piece, rotation=new_rotation):
self.current_piece.rotation = new_rotation
def drop_piece(self):
if not self.move_piece(0, 1):
self.place_piece(self.current_piece)
self.clear_lines()
self.current_piece = self.next_piece
self.next_piece = self.get_new_piece()
if not self.is_valid_position(self.current_piece):
return False # Game over
return True
def draw_grid(self):
for y in range(GRID_HEIGHT):
for x in range(GRID_WIDTH):
rect = pygame.Rect(
GRID_X_OFFSET + x * CELL_SIZE,
GRID_Y_OFFSET + y * CELL_SIZE,
CELL_SIZE,
CELL_SIZE
)
pygame.draw.rect(self.screen, self.grid[y][x], rect)
pygame.draw.rect(self.screen, WHITE, rect, 1)
def draw_piece(self, piece, offset_x=0, offset_y=0):
piece_pattern = piece.get_rotated_piece()
for y, row in enumerate(piece_pattern):
for x, cell in enumerate(row):
if cell == '#':
rect = pygame.Rect(
GRID_X_OFFSET + (piece.x + x) * CELL_SIZE + offset_x,
GRID_Y_OFFSET + (piece.y + y) * CELL_SIZE + offset_y,
CELL_SIZE,
CELL_SIZE
)
pygame.draw.rect(self.screen, piece.color, rect)
pygame.draw.rect(self.screen, WHITE, rect, 1)
def draw_next_piece(self):
# Draw next piece preview
next_x = GRID_WIDTH * CELL_SIZE + GRID_X_OFFSET + 20
next_y = GRID_Y_OFFSET + 50
text = self.font.render("Next:", True, WHITE)
self.screen.blit(text, (next_x, next_y - 30))
piece_pattern = self.next_piece.get_rotated_piece()
for y, row in enumerate(piece_pattern):
for x, cell in enumerate(row):
if cell == '#':
rect = pygame.Rect(
next_x + x * CELL_SIZE,
next_y + y * CELL_SIZE,
CELL_SIZE,
CELL_SIZE
)
pygame.draw.rect(self.screen, self.next_piece.color, rect)
pygame.draw.rect(self.screen, WHITE, rect, 1)
def draw_info(self):
info_x = GRID_WIDTH * CELL_SIZE + GRID_X_OFFSET + 20
info_y = GRID_Y_OFFSET + 200
score_text = self.font.render(f"Score: {self.score}", True, WHITE)
level_text = self.font.render(f"Level: {self.level}", True, WHITE)
lines_text = self.font.render(f"Lines: {self.lines_cleared}", True, WHITE)
self.screen.blit(score_text, (info_x, info_y))
self.screen.blit(level_text, (info_x, info_y + 40))
self.screen.blit(lines_text, (info_x, info_y + 80))
def run(self):
running = True
while running:
dt = self.clock.tick(60)
self.fall_time += dt
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.move_piece(-1, 0)
elif event.key == pygame.K_RIGHT:
self.move_piece(1, 0)
elif event.key == pygame.K_DOWN:
self.move_piece(0, 1)
elif event.key == pygame.K_UP:
self.rotate_piece()
elif event.key == pygame.K_SPACE:
while self.move_piece(0, 1):
pass
# Auto-drop piece
if self.fall_time >= self.fall_speed:
if not self.drop_piece():
# Game over
print(f"Game Over! Final Score: {self.score}")
running = False
self.fall_time = 0
# Draw everything
self.screen.fill(BLACK)
self.draw_grid()
self.draw_piece(self.current_piece)
self.draw_next_piece()
self.draw_info()
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
game = Tetris()
game.run()
This Tetris implementation includes all the features you requested:
Key Features:
- Complete Tetris gameplay with all 7 standard pieces (I, O, T, S, Z, J, L)
- Score tracking that increases with cleared lines and level
- Level progression (increases every 10 lines cleared)
- Next piece preview window
- Proper piece rotation and collision detection
Controls:
- Arrow keys: Move left/right/down
- Up arrow: Rotate piece
- Spacebar: Hard drop (instant drop to bottom)
Game Mechanics:
- Lines clear when completely filled
- Game speed increases with level
- Score multiplier based on current level
- Game over when pieces reach the top
Display Elements:
- Main game grid with colored pieces
- Next piece preview box
- Score, level, and lines cleared counters
- Clean visual design with grid lines
To run this, you'll need to install Pygame:
pip install pygame
The game automatically handles piece generation, line clearing, level progression, and maintains a smooth 60 FPS gameplay experience.