也想试试
[Asm] 纯文本查看 复制代码```python
import pygame
import random
pygame.init()
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
# Screen dimensions
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 700
# Block dimensions
BLOCK_SIZE = 20
# Define shapes
S_SHAPE_TEMPLATE = [['.....',
'.....',
'..oo.',
'.oo..',
'.....'],
['.....',
'..o..',
'..oo.',
'...o.',
'.....']]
Z_SHAPE_TEMPLATE = [['.....',
'.....',
'.oo..',
'..oo.',
'.....'],
['.....',
'..o..',
'.oo..',
'.o...',
'.....']]
I_SHAPE_TEMPLATE = [['..o..',
'..o..',
'..o..',
'..o..',
'.....'],
['.....',
'.....',
'oooo.',
'.....',
'.....']]
O_SHAPE_TEMPLATE = [['.....',
'.....',
'.oo..',
'.oo..',
'.....']]
J_SHAPE_TEMPLATE = [['.....',
'.o...',
'.ooo.',
'.....',
'.....'],
['.....',
'..oo.',
'..o..',
'..o..',
'.....'],
['.....',
'.....',
'.ooo.',
'...o.',
'.....'],
['.....',
'..o..',
'..o..',
'.oo..',
'.....']]
L_SHAPE_TEMPLATE = [['.....',
'...o.',
'.ooo.',
'.....',
'.....'],
['.....',
'..o..',
'..o..',
'..oo.',
'.....'],
['.....',
'.....',
'.ooo.',
'.o...',
'.....'],
['.....',
'.oo..',
'..o..',
'..o..',
'.....']]
T_SHAPE_TEMPLATE = [['.....',
'..o..',
'.ooo.',
'.....',
'.....'],
['.....',
'..o..',
'..oo.',
'..o..',
'.....'],
['.....',
'.....',
'.ooo.',
'..o..',
'.....'],
['.....',
'..o..',
'.oo..',
'..o..',
'.....']]
SHAPES = {'S': S_SHAPE_TEMPLATE,
'Z': Z_SHAPE_TEMPLATE,
'J': J_SHAPE_TEMPLATE,
'L': L_SHAPE_TEMPLATE,
'I': I_SHAPE_TEMPLATE,
'O': O_SHAPE_TEMPLATE,
'T': T_SHAPE_TEMPLATE}
# Define block colors
SHAPE_COLORS = { 'S': GREEN,
'Z': RED,
'J': BLUE,
'L': ORANGE,
'I': CYAN,
'O': YELLOW,
'T': PURPLE }
# Define game variables
board = []
for i in range(10):
board.append([ '.' for j in range(20) ])
fps_clock = pygame.time.Clock()
fps = 25
display_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Tetris')
background_music = pygame.mixer.music.load('music/microchip.ogg')
pygame.mixer.music.play(-1, 0.0)
# Draws a single block at the specified coordinate with the specified color
def draw_block(x, y, color, surface):
pygame.draw.rect(surface, color, (x, y, BLOCK_SIZE, BLOCK_SIZE))
# Draws the gridlines of the playfield
def draw_grid(surface):
for x in range(0, SCREEN_WIDTH, BLOCK_SIZE):
pygame.draw.line(surface, GRAY, (x, 0), (x, SCREEN_HEIGHT))
for y in range(0, SCREEN_HEIGHT, BLOCK_SIZE):
pygame.draw.line(surface, GRAY, (0, y), (SCREEN_WIDTH, y))
# Draws the current piece being controlled by the player
def draw_piece(piece, surface):
shape_blocks = SHAPES[piece.shape][piece.rotation]
for x in range(5):
for y in range(5):
if shape_blocks[y][