Python使用运行总循环更改alpha

时间:2014-05-13 17:09:21

标签: python pygame

我正试图淡化htpCap1 blit。变量接受并显示我直接调用的.set_alpha()的任何值,但不会通过运行的总循环更改。任何帮助将不胜感激。

P.S。是的,我对此非常新,并意识到这可能是初学者的错误。因此,请提前感谢您的时间。

#================================= Setup =======================================
    #imports
    import pygame
    import sys
    import os
    import time
    import pyganim


    #Init
    pygame.mixer.init()
    pygame.mixer.pre_init(44100, -16, 2, 2048)
    pygame.init()

    # Game Window Size
    WINDOWWIDTH = 1600
    WINDOWHEIGHT =  900
    windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)


    # Game Window Captions
    pygame.display.set_caption('Project Apprentice')


    #StaticIMGs
    htpCap1 = pygame.image.load('sprites/htpcapt1.gif').convert(24)
    alpha1 = 0
    htpCap1.set_alpha(alpha1)
    htpLeft = pygame.image.load('sprites/htpleft.png')
    sprite = pygame.image.load('sprites/sprite_down.gif')
    wasd = pygame.image.load('sprites/htpctrlorig.gif')
    w = pygame.image.load('sprites/htpctrlW.gif')
    a = pygame.image.load('sprites/htpctrlA.gif')
    s = pygame.image.load('sprites/htpctrlS.gif')
    d = pygame.image.load('sprites/htpctrlD.gif')


    #Anim
    frameAnim = pyganim.PygAnimation([('sprites/htp1.gif', 0.07),
                                      ('sprites/htp2.gif', 0.07),
                                      ('sprites/htp3.gif', 0.07)])
    frameAnim.play()

    spriteUp = pyganim.PygAnimation([('sprites/sprite_up.gif', 0.1),
                                     ('sprites/sprite_move_up.000.gif', 0.1),
                                     ('sprites/sprite_move_up.001.gif', 0.1),
                                     ('sprites/sprite_move_up.002.gif', 0.1)])
    spriteLeft = pyganim.PygAnimation([('sprites/sprite_left.gif', 0.01),
                                     ('sprites/sprite_move_left.000.gif', 0.1),
                                     ('sprites/sprite_move_left.001.gif', 0.1),
                                     ('sprites/sprite_move_left.002.gif', 0.1)])
    spriteDown = pyganim.PygAnimation([('sprites/sprite_down.gif', 0.1),
                                     ('sprites/sprite_move_down.000.gif', 0.1),
                                     ('sprites/sprite_move_down.001.gif', 0.1),
                                     ('sprites/sprite_move_down.002.gif', 0.1)])
    spriteRight = pyganim.PygAnimation([('sprites/sprite_right.gif', 0.01),
                                     ('sprites/sprite_move_right.000.gif', 0.1),
                                     ('sprites/sprite_move_right.001.gif', 0.1),
                                     ('sprites/sprite_move_right.002.gif', 0.1)])

    BGCOLOR = (255, 255, 255)


    # Clock function
    mainClock = pygame.time.Clock()




    #===========================  Program Loop ===================================
    done = False
    while done == False:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT:
                done = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    import menuscreen
                    done = True

        windowSurface.fill((BGCOLOR))

        frameAnim.blit(windowSurface, (800,0))
        windowSurface.blit(htpLeft, (0,0))
        windowSurface.blit(htpCap1, (20,120))


        for i in range(255):
            alphaChange = 1
            alpha1 += alphaChange        

        ticks = pygame.time.get_ticks()
        if ticks < 5000:
            windowSurface.blit(sprite, (1180,310))
            windowSurface.blit(wasd, (1050,600))        
        if ticks > 5001:
            spriteRight.blit(windowSurface, (1180,310))
            spriteRight.play()
            windowSurface.blit(d, (1050,600))
        if ticks >= 9000:
            spriteRight.stop()
            spriteDown.blit(windowSurface, (1180,310))
            spriteDown.play()
            windowSurface.blit(s, (1050,600))
        if ticks >= 11000:
            spriteDown.stop()
            spriteLeft.blit(windowSurface, (1180,310))
            spriteLeft.play()
            windowSurface.blit(a, (1050,600))
        if ticks >= 13000:
            spriteLeft.stop()
            spriteUp.blit(windowSurface, (1180,310))
            spriteUp.play()
            windowSurface.blit(w, (1050,600))
        if ticks >= 16000:
            spriteUp.stop()
            windowSurface.blit(sprite, (1180,310))
            windowSurface.blit(wasd, (1050,600))


        pygame.display.update()
        mainClock.tick(60) # Feel free to experiment with any FPS setting.
    pygame.quit()

1 个答案:

答案 0 :(得分:0)

您在for循环中为alpha1分配了新值,但这并没有神奇地改变htpCap1的alpha值。您必须反复拨打htpCap1.set_alpha

此外,您在调用htpCap1后必须将htpCap1.set_alpha blit到屏幕上。否则,您显然不会看到不透明度发生任何变化。

所以,而不是

for i in range(255):
    alphaChange = 1
    alpha1 += alphaChange    

使用类似

的内容
if alpha1 < 255:
    alpha1 += 1
    htpCap1.set_alpha(alpha1)

并且可能调整&#34;时间&#34;使用比1

更小/更大的值
if alpha1 < 255:
    alpha1 += 0.3
    htpCap1.set_alpha(int(alpha1))