用户希望将多个形状和颜色变量随机混合,并在另一个函数中组合,以便在屏幕上打印图像。
- 用户目前遵循的教程使用不同的代码,但认为使用原始代码更容易理解。原始代码使用 7 种颜色和 5 种形状,并使用文本字符串代表这些形状和颜色。
- 问题在于运行游戏时没有任何反应,窗口没有响应。
- 解决方案:
- 问题在于用户没有正确地加载图像。用户使用 blit 来显示图像,但可能应该发送一个使用 pygame 打开的图像。改正方法如下:
ekraan.blit(pygame.image.load(color + NURK + ".png").convert_alpha())
- 另外,用户没有更新屏幕。在绘制完屏幕上的内容后,需要调用 pygame.display.update() 方法来更新屏幕。
- 代码例子:
- 以下代码提供了完整的解决方案,包括加载图像和更新屏幕:
import pygame
import random
# 设置游戏参数
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
REVEALSPEED = 8
COLS = 10
ROWS = 6
BOXSIZE = 40
GAPSIZE = 10
# 设置颜色
DARKGRAY = (60, 60, 60)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 128, 0)
PURPLE = (255, 0, 255)
CYAN = (0, 255, 255)
# 设置背景色和盒子颜色
BGCOLOR = DARKGRAY
BOXCOLOR = WHITE
# 设置形状
DONUT = 1
SQUARE = 2
DIAMOND = 3
LINES = 4
OVAL = 5
# 主函数
def main():
global MAINCLOCK, MAINSURF
pygame.init()
MAINCLOCK = pygame.time.Clock()
MAINSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
# 主游戏循环
while True:
# 检测事件
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
# 更新游戏状态
# 绘制游戏界面
MAINSURF.fill(BGCOLOR)
# ...
# 更新屏幕
pygame.display.update()
MAINCLOCK.tick(FPS)
# 获取随机化的游戏棋盘
def getRandomizedBoard():
# 获取所有可能形状和颜色的列表
icons = []
for c in (RED, GREEN, BLUE, YELLOW, ORANGE, PURPLE, CYAN):
for s in (DONUT, SQUARE, DIAMOND, LINES, OVAL):
icons.append((s, c))
# 随机打乱列表并截取一半的图标
random.shuffle(icons)
numIconsUsed = int(COLS * ROWS / 2)
icons = icons[:numIconsUsed] * 2 # 需要成对的图标
# 创建游戏棋盘数据结构
board = []
for x in range(COLS):
columns = []
for y in range(ROWS):
randomIndex = random.randint(0, len(icons) - 1)
columns.append(icons[randomIndex])
del icons[randomIndex]
board.append(columns)
return board
# 绘制形状
def drawShape(shape, color, boxx, boxy):
left, top = leftTopOfBox(boxx, boxy)
if shape == DONUT:
ekraan.blit(pygame.image.load(color + NURK + ".png").convert_alpha())
elif shape == SQUARE:
ekraan.blit(pygame.image.load(color + SQUARE + ".png").convert_alpha())
elif shape == DIAMOND:
ekraan.blit(pygame.image.load(color + DIAMOND + ".png").convert_alpha())
elif shape == LINES:
ekraan.blit(pygame.image.load(color + LINES + ".png").convert_alpha())
elif shape == OVAL:
ekraan.blit(pygame.image.load(color + OVAL + ".png").convert_alpha())
# 计算框的左上角坐标
def leftTopOfBox(boxx, boxy):
# 计算边距
xmargin = int((WINDOWWIDTH - (COLS * (BOXSIZE + GAPSIZE))) / 2)
ymargin = int((WINDOWHEIGHT - (ROWS * (BOXSIZE + GAPSIZE))) / 2)
left = boxx * (BOXSIZE + GAPSIZE) + xmargin
top = boxy * (BOXSIZE + GAPSIZE) + ymargin
return (left, top)
# 绘制游戏棋盘
def drawBoard(board, revealed):
for boxx in range(COLS):
for boxy in range(ROWS):
left, top = leftTopOfBox(boxx, boxy)
if not revealed[boxx][boxy]:
# 绘制覆盖的盒子
pygame.draw.rect(MAINSURF, BOXCOLOR, (left, top, BOXSIZE, BOXSIZE))
else:
# 绘制图标
shape, color = getShapeAndColor(board, boxx, boxy)
drawShape(shape, color, boxx, boxy)
# 获取形状和颜色
def getShapeAndColor(board, boxx, boxy):
return board[boxx][boxy][0], board[boxx][boxy][1]
# 检查鼠标是否悬停在某个框上
def isOverBox(x, y):
for boxx in range(COLS):
for boxy in range(ROWS):
left, top = leftTopOfBox(boxx, boxy)
boxRect = pygame.Rect(left, top, BOXSIZE, BOXSIZE)
if boxRect.collidepoint(x, y):
return (boxx, boxy)
return (None, None)
# 主函数入口
if __name__ == '__main__':
main()
以上代码提供了完整的解决方案,包括加载图像、更新屏幕和游戏的主逻辑。