图片未在pygame

时间:2016-01-18 14:50:17

标签: python function python-3.x pygame

我正在为我的计算课程制作一个骰子游戏,我正在使用python来做到这一点。游戏是yahtzee,所以我加载了6个骰子图像来代表骰子的面孔。理论是生成1到6之间的随机数,然后根据数字是什么来加载不同的骰子图像。我将骰子卷放在一个函数中,每当我按空格键时它就被调用。虽然在shell中生成并打印了随机数,但没有任何骰子图像正在更新。

            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()

1 个答案:

答案 0 :(得分:2)

在函数diceroll()中,Python解释器将变量Dice视为局部变量。从技术上讲,您可以通过在函数开头声明变量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阻止。