【问题标题】:Images not updating in pygamepygame中没有更新的图像
【发布时间】:2016-01-18 14:50:17
【问题描述】:

我正在为我的计算课程制作一个骰子游戏,我正在使用 python 来做这件事。游戏是 yahtzee,所以我加载了 6 张骰子图像来代表骰子的面。理论是生成一个 1 到 6 之间的随机数,然后根据该数字加载不同的骰子图像。我将骰子放在一个函数中,每当我按下空格键时都会调用它。尽管在外壳中生成并打印了一个随机数,但没有一个骰子图像正在更新。

            import pygame, random
            pygame.init()
            FPS = 30
            fpsClock = pygame.time.Clock()
            Display_Height = 500
            Display_Width = 750
            GameDisplay = pygame.display.set_mode((Display_Width, Display_Height))
            pygame.display.set_caption("Yahtzee")
            Background = pygame.image.load('Red Background.jpg')

            Dice   = pygame.image.load('Dice_1.jpg')
            Dice_1 = pygame.image.load('Dice_1.jpg')
            Dice_2 = pygame.image.load('Dice_2.jpg')
            Dice_3 = pygame.image.load('Dice_3.jpg')
            Dice_4 = pygame.image.load('Dice_4.jpg')
            Dice_5 = pygame.image.load('Dice_5.jpg')
            Dice_6 = pygame.image.load('Dice_6.jpg')
            #--------------------------------------------------------------------------
            myfont = pygame.font.SysFont("cambria", 17)
            GameDisplay.blit(Background,(0,0))
            RollsRem = 3
            count = 0
            dicexy = [[25,100], [175,100], [325,100], [475,100], [625,100]]
            #--------------------------------------------------------------------------
            randomnumber = 1
            def diceroll():
                randomnumber = random.randint(1,6)
                if randomnumber == 1:
                    Dice = Dice_1
                elif randomnumber == 2:
                    Dice = Dice_2
                elif randomnumber == 3:
                    Dice = Dice_3
                elif randomnumber == 4:
                    Dice = Dice_4
                elif randomnumber == 5:
                    Dice = Dice_5
                elif randomnumber == 6:
                    Dice = Dice_6
                print(randomnumber)

            GameQuit = False
            while not GameQuit:
                while count < 12:
                    GameDisplay.blit(Background,(0,0))
                    if RollsRem < 0:
                        count = count + 1
                        RollsRem = 3
                    for i in range(5):
                        GameDisplay.blit(Dice, dicexy[i])

                    Rolls_Remaining = myfont.render("Rolls Remaining: " + str(RollsRem), 1, (255,255,255))
                    GameDisplay.blit(Rolls_Remaining, (575, 20))
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            GameQuit = True     
                        if RollsRem >= 0:
                            if event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_SPACE:
                                    RollsRem = RollsRem -1

                                    pygame.display.update()
                                    for i in range(5):
                                        diceroll()
                                        pygame.display.update()
                                        GameDisplay.blit(Dice, dicexy[i])

                    fpsClock.tick(FPS)                
                    pygame.display.update()

            pygame.quit()
            quit()

【问题讨论】:

  • Dice in function diceroll 与您在 elswhere 使用的 Dice 无关。您必须 return 将该值从函数传递给调用者。顺便说一句,请阅读Style Guide for Python Code
  • btw:学习如何使用列表并将图像保留在列表中。你不需要这么多if/else - 你只需要一行Dice = dice_images[randomnumber]

标签: python function python-3.x pygame


【解决方案1】:

在函数diceroll() 中,变量Dice 被Python 解释器视为局部变量。从技术上讲,您可以通过在函数开头声明变量 global 来解决此问题,但这不是一种好的编码风格。

更好的选择是从该函数返回图像:

def diceroll():
    randomnumber = random.randint(1,6)
    if randomnumber == 1:
        dice = Dice_1
    elif randomnumber == 2:
        ...
    return dice

然后在主循环中使用结果:

for i in range(5):
    dice = diceroll()
    pygame.display.update()
    GameDisplay.blit(dice, dicexy[i])

另外,考虑使用数组来存储骰子图像。通过这种方式,您可以根据随机索引访问正确的图像,而无需繁琐的if ... elif ... else 块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多