Sure! Here's a simple implementation of Tetris game in Python:
import pygame
import random
pygame.init()
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
PURPLE = (128, 0, 128)
# Define display dimensions
WIDTH = 800
HEIGHT = 600
# Define block size and play area dimensions
BLOCK_SIZE = 30
PLAY_WIDTH = 300
PLAY_HEIGHT = 600
# Calculate the number of rows and columns in the play area
COLS = PLAY_WIDTH // BLOCK_SIZE
ROWS = PLAY_HEIGHT // BLOCK_SIZE
# Define control keys
LEFT = pygame.K_LEFT
RIGHT = pygame.K_RIGHT
DOWN = pygame.K_DOWN
ROTATE = pygame.K_UP
# Define tetromino shapes
SHAPES = [
[[1, 1, 1, 1]], # I
[[1, 1], [1, 1]], # O
[[1, 1, 1], [0, 1, 0]], # T
[[1, 1, 0], [0, 1, 1]], # S
[[0, 1, 1], [1, 1, 0]], # Z
[[1, 1, 1], [0, 0, 1]], # J
[[1, 1, 1], [1, 0, 0]] # L
]
def create_grid(locked_positions={}):
# Create a 2D grid with locked positions
grid = [[BLACK for _ in range(COLS)] for _ in range(ROWS)]
for row in range(ROWS):
for col in range(COLS):
if (col, row) in locked_positions:
color = locked_positions[(col, row)]
grid[row][col] = color
return grid
def convert_shape_format(shape):
# Convert the shape to a 2D list of positions
positions = []
for row in range(len(shape)):
for col in range(len(shape[row])):
if shape[row][col] == 1:
positions.append((col, row))
return positions
def valid_position(position, grid):
# Check if a position is valid
return all(0 <= col < COLS and 0 <= row < ROWS and grid[row][col] == BLACK for col, row in position)
def game_over(positions):
# Check if the game is over (any locked position in the top row)
return any(row == 0 for (_, row) in positions)
def draw_grid(surface, grid):
for row in range(len(grid)):
for col in range(len(grid[row])):
pygame.draw.rect(surface, grid[row][col], (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
def draw_window(surface, grid):
surface.fill(BLACK)
pygame.draw.rect(surface, WHITE, (0, 0, PLAY_WIDTH, PLAY_HEIGHT))
draw_grid(surface, grid)
pygame.display.update()
def get_shape():
return random.choice(SHAPES)
def main():
locked_positions = {}
grid = create_grid(locked_positions)
clock = pygame.time.Clock()
game_over_flag = False
current_shape = get_shape()
position = [(COLS // 2 - len(current_shape[0]) // 2, 0)]
while not game_over_flag:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
keys = pygame.key.get_pressed()
if keys[LEFT]:
new_position = [(x - 1, y) for x, y in position]
if valid_position(new_position, grid):
position = new_position
if keys[RIGHT]:
new_position = [(x + 1, y) for x, y in position]
if valid_position(new_position, grid):
position = new_position
if keys[DOWN]:
new_position = [(x, y + 1) for x, y in position]
if valid_position(new_position, grid):
position = new_position
if keys[ROTATE]:
new_shape = [[current_shape[y][x] for y in range(len(current_shape))] for x in range(len(current_shape[0])-1, -1, -1)]
new_position = [(x, y) for x, y in position] # Copy the current position
if valid_position(new_position, grid): # Check if the new position is valid
current_shape = new_shape
if not valid_position([(x, y + 1) for x, y in position], grid) and position[0][1] > 0:
# Lock the current shape in its position
for pos in position:
locked_positions[pos] = CYAN
current_shape = get_shape()
position = [(COLS // 2 - len(current_shape[0]) // 2, 0)]
while valid_position([(x, y + 1) for x, y in position], grid):
position = [(x, y + 1) for x, y in position]
if game_over(list(locked_positions.keys())):
game_over_flag = True
grid = create_grid(locked_positions)
draw_window(win, grid)
clock.tick(10) # Adjust the game speed
print("Game Over!")
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Tetris")
main()
You need to have the Pygame library installed to run this code. It creates a basic Tetris game with randomly generated tetromino shapes that fall down and can be moved and rotated using the arrow keys. Once a tetromino reaches the bottom or collides with another block, it locks in place and a new tetromino is generated. The game ends if any block reaches the top row.