俄罗斯方块代码
这是俄罗斯方块的基本代码:
import pygame
import random
pygame.init()
# 设置游戏窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
# 设置游戏区域大小
PLAY_WIDTH = 300
PLAY_HEIGHT = 600
# 设置方块大小
BLOCK_SIZE = 30
# 设置方块移动速度
FPS = 60
# 设置颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GRAY = (128, 128, 128)
# 创建游戏窗口
WIN = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('俄罗斯方块')
# 加载字体
FONT = pygame.font.SysFont('Arial', 30)
# 定义方块形状
S = [['.....',
'.....',
'..00.',
'.00..',
'.....'],
['.....',
'..0..',
'..00.',
'...0.',
'.....']]
Z = [['.....',
'.....',
'.00..',
'..00.',
'.....'],
['.....',
'..0..',
'.00..',
'.0...',
'.....']]
I = [['..0..',
'..0..',
'..0..',
'..0..',
'.....'],
['.....',
'.....',
'0000.',
'.....',
'.....']]
O = [['.....',
'.....',
'.00..',
'.00..',
'.....']]
J = [['.....',
'.0...',
'.000.',
'.....',
'.....'],
['.....',
'..00.',
'..0..',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'...0.',
'.....'],
['.....',
'..0..',
'..0..',
'.00..',
'.....']]
L = [['.....',
'...0.',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..0..',
'..00.',
'.....'],
['.....',
'.....',
'.000.',
'.0...',
'.....'],
['.....',
'.00..',
'..0..',
'..0..',
'.....']]
T = [['.....',
'..0..',
'.000.',
'.....',
'.....'],
['.....',
'..0..',
'..00.',
'..0..',
'.....'],
['.....',
'.....',
'.000.',
'..0..',
'.....'],
['.....',
'..0..',
'.00..',
'..0..',
'.....']]
# 定义方块颜色
SHAPE_COLORS = {
'S': GREEN,
'Z': RED,
'I': BLUE,
'O': YELLOW,
'J': GRAY,
'L': WHITE,
'T': BLUE
}
# 定义方块类
class Block:
def __init__(self, shape, x, y):
self.shape = shape
self.color = SHAPE_COLORS[shape]
self.rotation = 0
self.x = x
self.y = y
def rotate(self):
self.rotation = (self.rotation + 1) % len(SHAPE[self.shape])
def move_left(self):
self.x -= 1
def move_right(self):
self.x += 1
def move_down(self):
self.y += 1
def draw(self):
shape_to_draw = SHAPE[self.shape][self.rotation]
for i, row in enumerate(shape_to_draw):
for j, col in enumerate(row):
if col == '0':
pygame.draw.rect(WIN, self.color, (self.x + j * BLOCK_SIZE, self.y + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# 定义游戏区域类
class PlayArea:
def __init__(self):
self.grid = [[None for _ in range(PLAY_WIDTH // BLOCK_SIZE)] for _ in range(PLAY_HEIGHT // BLOCK_SIZE)]
def add_block(self, block):
shape_to_add = SHAPE[block.shape][block.rotation]
for i, row in enumerate(shape_to_add):
for j, col in enumerate(row):
if col == '0':
self.grid[(block.y // BLOCK_SIZE) + i][(block.x // BLOCK_SIZE) + j] = block.color
def check_lines(self):
lines_to_remove = []
for i, row in enumerate(self.grid):
if None not in row:
lines_to_remove.append(i)
for line in lines_to_remove:
self.grid.pop(line)
self.grid.insert(0, [None for _ in range(PLAY_WIDTH // BLOCK_SIZE)])
def draw(self):
for i, row in enumerate(self.grid):
for j, col in enumerate(row):
if col:
pygame.draw.rect(WIN, col, (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 0)
pygame.draw.rect(WIN, BLACK, (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE), 1)
# 创建游戏区域实例
PLAY_AREA = PlayArea()
# 定义游戏主循环
def main():
# 定义方块初始位置
block_x = PLAY_WIDTH // 2 - BLOCK_SIZE // 2
block_y = 0
# 定义方块实例
block = Block(random.choice(list(SHAPE_COLORS.keys())), block_x, block_y)
clock = pygame.time.Clock()
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
block.move_left()
elif event.key == pygame.K_RIGHT:
block.move_right()
elif event.key == pygame.K_DOWN:
block.move_down()
elif event.key == pygame.K_UP:
block.rotate()
# 检查方块是否到达底部
if block.y + BLOCK_SIZE * len(SHAPE[block.shape][block.rotation]) >= PLAY_HEIGHT:
PLAY_AREA.add_block(block)
PLAY_AREA.check_lines()
block_x = PLAY_WIDTH // 2 - BLOCK_SIZE // 2
block_y = 0
block = Block(random.choice(list(SHAPE_COLORS.keys())), block_x, block_y)
# 绘制游戏区域和方块
WIN.fill(BLACK)
PLAY_AREA.draw()
block.draw()
# 更新窗口
pygame.display.update()
# 启动游戏
if __name__ == '__main__':
main()
以上代码是一个简单的俄罗斯方块游戏的基本代码,你可以根据需要进行修改和优化
原文地址: https://www.cveoy.top/t/topic/eEKu 著作权归作者所有。请勿转载和采集!