import pygame
import random
# 游戏窗口大小
WIDTH, HEIGHT = 300, 600
# 每个方块的宽度和高度
BLOCK_SIZE = 30
# 方块移动的速度
SPEED = 3
class Block:
"""方块类"""
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
self.shape = shape
self.rotation = 0
def draw(self, surface):
"""绘制方块"""
for row in range(len(self.shape)):
for col in range(len(self.shape[0])):
if self.shape[row][col] == 1:
pygame.draw.rect(surface, self.color, (self.x + col * BLOCK_SIZE, self.y + row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
def move(self, dx, dy):
"""方块移动"""
self.x += dx
self.y += dy
def rotate(self):
"""方块旋转"""
self.rotation = (self.rotation + 1) % 4
self.shape = [[self.shape[y][x] for y in range(len(self.shape))] for x in range(len(self.shape[0])-1, -1, -1)]
def check_collision(self, grid):
"""检查方块与网格的碰撞"""
for row in range(len(self.shape)):
for col in range(len(self.shape[0])):
if self.shape[row][col] == 1:
if self.x + col * BLOCK_SIZE < 0 or self.x + col * BLOCK_SIZE &
用python做俄罗斯方块游戏
于 2023-05-24 12:30:07 首次发布