Pygame键盘动画无法正常工作

时间:2014-07-13 03:59:00

标签: python animation pygame

import pygame



class Sprite:

    def __init__(self, x, y, curren_time):

        self.rect = pygame.Rect(x, y, 100, 110)

        self.images = []

#Tells pygame the image list that you want to stitch together
        for x in range(10):
            img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG")
#Append the image to each other to create the animation effect
            self.images.append( img )

        self.current_image = 0

        self.time_num = 100
        self.time_target = curren_time + self.time_num

    def update(self, curren_time):

        if curren_time >= self.time_target:

            self.time_target = curren_time + self.time_num

            self.current_image += 1

            if self.current_image == len(self.images):
            self.current_image = 0

    def render(self, window):

        window.blit(self.images[self.current_image], self.rect)
#Colors
black = (0,0,0)
white = (255,255,255)




def main():

    pygame.init()

    window = pygame.display.set_mode((800,600))

    pygame.display.set_caption("Sprites")

    move_x, move_y = 0, 0

    clock = pygame.time.Clock()
    curren_time = pygame.time.get_ticks()

    player = Sprite(110,100,curren_time)

    font = pygame.font.SysFont(None, 150)
    pause_text = font.render("PAUSE",1,white)
    pause_rect = pause_text.get_rect( center = window.get_rect().center )

#Adding how many places the image moves when using the key function
    state_game = True
    state_pause = False

#So while True
    while state_game:

        curren_time = pygame.time.get_ticks()



        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                state_game = False

            elif event.type == pygame.KEYDOWN:

                if event.key ==pygame.K_ESCAPE:
                    state_game = False

                elif event.key == pygame.K_SPACE:
                    state_pause = not state_pause

                if event.key == pygame.K_LEFT:
                    move_x = -3

                elif event.key == pygame.K_RIGHT:
                    move_y = 3

                elif event.key == pygame.K_UP:
                    move_x = -3

                elif event.key == pygame.K_DOWN:
                    move_y = 3

            elif event.type == pygame.KEYUP:

                if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                    move_x = 0

                elif event.key in (pygame.K_UP, pygame.K_DOWN):
                    move_y = 0



        if not state_pause:
            player.rect.x += move_x
            player.rect.y += move_y
            player.update(curren_time)

#Fills the window with a color

        window.fill(black)

        player.render(window)

        if state_pause:
            window.blit(pause_text,pause_rect)

        pygame.display.flip()



        clock.tick(50)



    pygame.quit()



if __name__ == '__main__':
    main()
#Code constructed by furas

所以问题是每当我按下右键时,动画会从左侧的屏幕上滑落。当你的手指离开钥匙时,动画不会停止。我自己搜索过这个问题,但是找不到任何问题。如果你看到可能导致问题的事情,请告诉我。谢谢! (向上键=向下,向下键=向下,向右键=向左,向左键=向左)

1 个答案:

答案 0 :(得分:2)

您的问题是move_xmove_yK_RIGHT交换{/ 1}}。

K_UP

应该是:

            elif event.key == pygame.K_RIGHT:
                move_y = 3

            elif event.key == pygame.K_UP:
                move_x = -3
相关问题