Python俄罗斯方块游戏代码示例 - 基于Pygame库
以下是一个简单的俄罗斯方块游戏的Python代码,基于Pygame库:
import pygame
import random
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 初始化 Pygame
pygame.init()
# 设置屏幕尺寸
SCREEN_WIDTH = 320
SCREEN_HEIGHT = 480
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
# 设置字体
font = pygame.font.SysFont(None, 25)
# 定义方块大小和数量
BLOCK_SIZE = 20
BLOCK_NUM_X = SCREEN_WIDTH // BLOCK_SIZE
BLOCK_NUM_Y = SCREEN_HEIGHT // BLOCK_SIZE
# 定义方块形状
BLOCK_SHAPES = [
[(1, 1, 1, 1)],
[(1, 1, 1), (0, 1, 0)],
[(1, 1, 0), (0, 1, 1)],
[(0, 1, 1), (1, 1, 0)],
[(1, 0, 0), (1, 1, 1)],
[(0, 0, 1), (1, 1, 1)],
[(1, 0, 0), (1, 0, 0), (1, 1, 1)],
]
# 定义方块颜色
BLOCK_COLORS = [RED, GREEN, BLUE, WHITE]
# 初始化游戏区域
game_area = [[0] * BLOCK_NUM_X for i in range(BLOCK_NUM_Y)]
# 随机生成一个方块
current_block = random.choice(BLOCK_SHAPES)
current_block_x = BLOCK_NUM_X // 2 - len(current_block[0]) // 2
current_block_y = 0
current_block_color = random.choice(BLOCK_COLORS)
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# 检查是否需要移动方块
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and current_block_x > 0:
current_block_x -= 1
if keys[pygame.K_RIGHT] and current_block_x < BLOCK_NUM_X - len(current_block[0]):
current_block_x += 1
if keys[pygame.K_DOWN] and current_block_y < BLOCK_NUM_Y - len(current_block):
current_block_y += 1
# 检查是否需要旋转方块
if keys[pygame.K_UP]:
current_block = list(zip(*current_block[::-1]))
# 检查方块是否落地
landed = False
for y in range(len(current_block)):
for x in range(len(current_block[0])):
if current_block[y][x] == 1:
if game_area[current_block_y+y+1][current_block_x+x] != 0 or current_block_y+y+1 == BLOCK_NUM_Y:
landed = True
break
# 如果方块已落地,更新游戏区域并生成新方块
if landed:
for y in range(len(current_block)):
for x in range(len(current_block[0])):
if current_block[y][x] == 1:
game_area[current_block_y+y][current_block_x+x] = current_block_color
current_block = random.choice(BLOCK_SHAPES)
current_block_x = BLOCK_NUM_X // 2 - len(current_block[0]) // 2
current_block_y = 0
current_block_color = random.choice(BLOCK_COLORS)
# 绘制游戏区域和方块
screen.fill(BLACK)
for y in range(BLOCK_NUM_Y):
for x in range(BLOCK_NUM_X):
if game_area[y][x] != 0:
pygame.draw.rect(screen, game_area[y][x], [x*BLOCK_SIZE, y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE])
for y in range(len(current_block)):
for x in range(len(current_block[0])):
if current_block[y][x] == 1:
pygame.draw.rect(screen, current_block_color, [(current_block_x+x)*BLOCK_SIZE, (current_block_y+y)*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE])
# 显示得分
score = sum([1 for row in game_area if 0 not in row])
score_text = font.render('Score: ' + str(score), True, WHITE)
screen.blit(score_text, [10, 10])
# 更新屏幕
pygame.display.update()
# 设置游戏帧率
pygame.time.Clock().tick(10)
这个代码实现了简单的俄罗斯方块游戏逻辑,包括方块的移动、旋转、落地、得分统计等功能。但是这个代码只是基础版本,还有很多可以改进和增强的地方。
例如,可以添加以下功能:
- 完整的行消除功能
- 不同的游戏难度
- 音效和背景音乐
- 更精美的图形和界面
- 游戏结束判断和处理
- 玩家存档和排行榜
希望这个代码能够帮助您学习Python游戏开发,并激发您创造更有趣和更复杂的游戏的灵感。
原文地址: https://www.cveoy.top/t/topic/nMI5 著作权归作者所有。请勿转载和采集!