Create a game of tetris in pygame and make it look classic arcade style.
- User
Create a game of tetris in pygame and make it look classic arcade style.
- Assistant
Below is a Python script using Pygame to create a classic arcade-style Tetris game. This implementation includes falling tetrominoes, line clearing, scoring, and a retro aesthetic with simple colors and a grid layout reminiscent of early arcade games. I'll explain the key components after the code. python Collapse Wrap Copy import pygame import random
Initialize Pygame
pygame.init()
Constants
GRID_WIDTH = 10 GRID_HEIGHT = 20 BLOCK_SIZE = 30 SCREEN_WIDTH = GRID_WIDTH * BLOCK_SIZE + 200 # Extra space for score SCREEN_HEIGHT = GRID_HEIGHT * BLOCK_SIZE FPS = 60
Colors (classic arcade style)
BLACK = (0, 0, 0) WHITE = (255, 255, 255) GRAY = (128, 128, 128) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) CYAN = (0, 255, 255) YELLOW = (255, 255, 0) MAGENTA = (255, 0, 255) ORANGE = (255, 165, 0)
Tetromino shapes (each is a list of rotations)
SHAPES = [ [[(0, 0), (0, 1), (1, 0), (1, 1)],], # O [[(0, 0), (0, 1), (0, 2), (0, 3)], [(0, 0), (1, 0), (2, 0), (3, 0)]], # I [[(0, 0), (0, 1), (0, 2), (1, 1)], [(0, 0), (1, 0), (2, 0), (1, -1)], [(0, 0), (0, -1), (0, -2), (-1, -1)], [(0, 0), (-1, 0), (-2, 0), (-1, 1)]], # T [[(0, 0), (0, 1), (1, 1), (1, 2)], [(0, 0), (1, 0), (1, -1), (2, -1)]], # S [[(0, 0), (0, 1), (1, 0), (1, -1)], [(0, 0), (1, 0), (1, 1), (2, 1)]], # Z [[(0, 0), (0, 1), (0, 2), (1, 0)], [(0, 0), (1, 0), (2, 0), (2, 1)], [(0, 0), (0, 1), (0, 2), (-1, 2)], [(0, 0), (-1, 0), (-2, 0), (-2, -1)]], # L [[(0, 0), (0, 1), (0, 2), (1, 2)], [(0, 0), (1, 0), (2, 0), (0, 1)], [(0, 0), (0, 1), (0, 2), (-1, 0)], [(0, 0), (1, 0), (2, 0), (2, -1)]] # J ]
COLORS = [YELLOW, CYAN, MAGENTA, GREEN, RED, BLUE, ORANGE]
Game setup
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Classic Arcade Tetris") clock = pygame.time.Clock() font = pygame.font.SysFont("monospace", 30, bold=True)
Game state
grid = [[BLACK for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)] current_piece = None piece_pos = [0, 0] fall_time = 0 fall_speed = 500 # milliseconds score = 0 game_over = False
def new_piece(): global current_piece, piece_pos piece_idx = random.randint(0, len(SHAPES) - 1) current_piece = {'shape': SHAPES[piece_idx], 'color': COLORS[piece_idx], 'rotation': 0} piece_pos = [GRID_WIDTH // 2 - 1, 0] if not valid_position(piece_pos, current_piece['shape'][current_piece['rotation']]): return False return True
def valid_position(pos, shape): for x, y in shape: new_x, new_y = pos[0] + x, pos[1] + y if (new_x < 0 or new_x >= GRID_WIDTH or new_y >= GRID_HEIGHT or (new_y >= 0 and grid[new_y][new_x] != BLACK)): return False return True
def merge_piece(): global grid shape = current_piece['shape'][current_piece['rotation']] for x, y in shape: grid_x, grid_y = piece_pos[0] + x, piece_pos[1] + y if grid_y >= 0: grid[grid_y][grid_x] = current_piece['color']
- User
I ran the code but got a SyntaxError: File "tetris.py", line 112 score_text = font.render(f"Score: {score}", True, WHITE) ^ SyntaxError: invalid syntax
- Assistant
- User
Another error: Traceback (most recent call last): File "tetris.py", line 1, in import pygame ImportError: No module named pygame
- Assistant
The ImportError: No module named pygame indicates that the library is not installed in your Python environment. This is a common issue when running Pygame-based code for the first time. Let’s fix this by installing . Steps to Resolve Install Pygame: Use pip, the Python package manager, to install Pygame. Open your terminal or command prompt and run one of the following commands: For Python 3 (most common): text Collapse Wrap Copy pip3 install pygame For Python 2 or if pip3 doesn’t work: text Collapse Wrap Copy pip install pygame If neither works, try specifying the Python version explicitly: text Collapse Wrap Copy python3 -m pip install pygame Verify Installation: After installation, check that Pygame is installed by running this in a Python interpreter: python Collapse Wrap Copy import pygame print(pygame.) If it outputs a version number (e.g., 2.5.2), Pygame is installed correctly. Check Your Python Environment: If you have multiple Python installations, ensure you’re installing Pygame in the same environment where you’re running the script. For example: Run python3 tetris.py if you installed with pip3. Use which python3 (Linux/Mac) or where python (Windows) to confirm the Python path. If using an IDE (e.g., PyCharm, VSCode), ensure it’s configured to use the correct Python interpreter where is installed. Run the Code Again: Once Pygame is installed, save the previous code into tetris.py and run it: text Collapse Wrap Copy python3 tetris.py or text Collapse Wrap Copy python tetris.py depending on your setup. If You Still Get Errors Permission Denied: On Linux/Mac, you might need sudo pip3 install pygame, but it’s better to use a virtual environment: text Collapse Wrap Copy python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install pygame Wheel Errors: If you see errors about "no matching distribution," ensure your pip is up-to-date: text Collapse Wrap Copy pip install --upgrade pip Specific OS Issues: Let me know your operating system (Windows, Mac, ) and Python version if the above doesn’t work, and I’ll tailor the solution. After installing Pygame, the code should run without the ImportError. Let me know how it goes or if you hit another snag!