俄罗斯方块是一款经典的游戏,其核心玩法是控制下落的方块,使其填满一行或多行并消除得分。
以下是一个简单的 Python 实现俄罗斯方块的示例代码:
```python
import pygame
import random
定义常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BLOCK_SIZE = 30
BOARD_WIDTH = 10
BOARD_HEIGHT = 20
SPEED = 500
初始化 Pygame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("俄罗斯方块")
定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
定义方块类
class Block:
def init(self, x, y):
self.x = x
self.y = y
self.color = random.choice([RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA])
self.shape = [random.choice([BLOCK_SIZE, BLOCK_SIZE // 2, BLOCK_SIZE // 3])]
def move(self, dx, dy):
self.x += dx
self.y += dy
def rotate(self):
self.shape = [(self.shape[j][i] * -1) for i in range(len(self.shape)) for j in range(len(self.shape[0]))]
定义游戏类
class Game:
def init(self):
self.board = [[None for j in range(BOARD_WIDTH)] for i in range(BOARD_HEIGHT)]
self.score = 0
self.block = None
self.next_block = Block(SCREEN_WIDTH // 2 - BLOCK_SIZE // 2, 0)
self.is_moving = False
self.is_rotating = False
self.is_dropping = False
self.is_cleared = False
def new_block(self):
self.block = Block(self.next_block.x, self.next_block.y)
self.block.shape = [random.choice([BLOCK_SIZE, BLOCK_SIZE // 2, BLOCK_SIZE // 3])]
self.block.color = random.choice([RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA])
def move_block(self, dx, dy):
if not self.is_moving:
self.is_moving = True
self.block.move(dx, dy)
if self.is_dropping:
self.new_block()
self.is_dropping = False
if self.is_cleared:
self.score += len(self.board[0])
self.new_block()
self.is_cleared = False
elif self.is_rotating:
self.block.rotate()
self.is_rotating = False
elif self.block.y < 0 or self.block.y >= BOARD_HEIGHT:
self.is_moving = False
self.is_dropping = True
self.block.move(0, SPEED)
self.block.y = BOARD_HEIGHT - 1
if self.is_cleared:
self.score += len(self.board[0])
self.new_block()
self.is_cleared = False
elif self.block.x < 0 or self.block.x >= BOARD_WIDTH:
self.is_moving = False
self.is_dropping = True
self.block.move(-SPEED, 0)
self.block.x = BOARD_WIDTH - 1
if self.is_cleared:
self.score += len(self.board[0])
self.new_block()
self.is_cleared = False
elif self.block.colliderect(pygame.Rect(BOARD_WIDTH // 2 - BLOCK_SIZE // 2, 0, BLOCK_SIZE, BLOCK_SIZE)):
self.is_moving = False
self.is_dropping = True
self.block.move(0, SPEED)
self.block.y = BOARD_HEIGHT - 1
if self.is_cleared:
self.score += len(self.board[0])
self.new_block()
self.is_cleared = False
elif self.block.colliderect(pygame.Rect(BOARD_WIDTH // 2 - BLOCK_SIZE // 2, BOARD_HEIGHT - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)):
self.is_moving = False
self.is_dropping = True
self.block.move(-SPEED, 0)
self.block.x = BOARD_WIDTH - 1
if self.is_cleared:
self.score += len(self.board[0])
self.new_block()
self.is_cleared = False
elif self.block.colliderect(pygame.Rect(0, BOARD_HEIGHT - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)):
self.is_moving = False
self.is_dropping = True
self.block.move(SPEED, 0)
self.block.y = BOARD_HEIGHT - 1
if self.is_cleared:
self.score += len(self.board[0])
self.new_block()
self.is_cleared = False
elif self.block.colliderect(pygame.Rect(BOARD_WIDTH // 2 - BLOCK_SIZE // 2, 0, BLOCK_SIZE, BLOCK_SIZE)):
self.is_rotating = True
self.block.rotate()
self.is_rotating = False
elif self.block.colliderect(pygame.Rect(BOARD_WIDTH // 2 - BLOCK_SIZE // 2, BOARD_HEIGHT - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)):
self.is_rotating = True
self.block.rotate()
self.is_rotating = False
elif self.block.colliderect(pygame.Rect(0, BOARD_HEIGHT - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)):
self.is_rotating = True
self.block.rotate()
self.is_rotating = False
elif self.block.colliderect(pygame.Rect(BOARD_WIDTH // 2 - BLOCK_SIZE // 2, 0, BLOCK_SIZE, BLOCK_SIZE)):
self.is_moving = True
self.block.move(0, SPEED)
self.is_moving = False
elif self.block.colliderect(pygame.Rect(BOARD_WIDTH // 2 - BLOCK_SIZE // 2, BOARD_HEIGHT - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)):
self.is_moving = True
self.block.move(-SPEED, 0)
self.is_moving = False
elif self.block.colliderect(pygame.Rect(0, BOARD_HEIGHT - BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)):
self.is_moving = True
self.block.move(SPEED, 0)
self.is_moving = False