pygame中无法移动对象

时间:2019-03-20 17:14:20

标签: python pygame

我正在为我的计算机科学课做一个项目,我们在这里使用pygame在屏幕上移动对象。我不是在使用pygame精灵,而是将图像导入pygame并将其用作自定义精灵。当我尝试在屏幕上移动“兔子”或“玩家”对象时,它似乎确实在更新位置。这是我要更改位置的Player对象:

class Player(object):
    def __init__(self, x, y, filename):
        self.x = x
        self.y = y
        self.image = pygame.transform.scale(pygame.image.load(filename), (150, 200))
        self.moving_left = False
        self.moving_right = False
        self.moving_up = False
        self.moving_down = False

    def moveUpdate(self):
        if self.moving_left:
            self.x -= 25
            pygame.display.update()
        if self.moving_right:
            self.y += 25
            pygame.display.update()
        if self.moving_up:
            self.y -= 25
            pygame.display.update()
        if self.moving_down:
            self.y += 25
            pygame.display.update()

    def moveEvent(self, event):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    self.moving_left = True
                if event.key == pygame.K_RIGHT:
                    self.moving_right = True
                if event.key == pygame.K_UP:
                    self.moving_up = True
                if event.key == pygame.K_DOWN:
                    self.moving_down = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    self.moving_left = False
                if event.key == pygame.K_RIGHT:
                    self.moving_right = False
                if event.key == pygame.K_UP:
                    self.moving_up = False
                if event.key == pygame.K_DOWN:
                    self.moving_down = False

    def draw(self, screen):
        screen.blit(self.image, (self.x, self.y))

这是我用来运行游戏本身的类:

class Game(object):
    def __init__(self):
        self.screensize = [1000, 1000]
        self.white = [255, 255, 255]
        self.black = [0, 0, 0]
        self.screen = pygame.display.set_mode(self.screensize)
        #self.bunny = pygame.transform.scale(pygame.image.load('bunny.png'), (150, 200))
        self.clock = pygame.time.Clock()
        self.player = Player(500, 500, 'bunny.png')

    def Run(self):
        run = True
        while run:
            self.clock.tick(60)
            self.screen.fill(self.white)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                    pygame.quit()
                    exit()
                elif event.type == pygame.K_ESCAPE:
                    run = False
                    pygame.quit()
                    exit()

                # - Objects Event Handle - 
                self.player.moveEvent(event)
            # - Updates - 
            #self.player.moveUpdate()

            # - Draws - 
            self.player.draw(self.screen)
            pygame.display.flip()

game = Game()
game.Run()

1 个答案:

答案 0 :(得分:0)

Player.moveEvent删除事件循环:

class Player(object):

    # [...]

    def moveEvent(self, event):

        # for event in pygame.event.get(): <----- to be removed

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                self.moving_left = True
            if event.key == pygame.K_RIGHT:
                self.moving_right = True
            if event.key == pygame.K_UP:
                self.moving_up = True
            if event.key == pygame.K_DOWN:
                self.moving_down = True
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                self.moving_left = False
            if event.key == pygame.K_RIGHT:
                self.moving_right = False
            if event.key == pygame.K_UP:
                self.moving_up = False
            if event.key == pygame.K_DOWN:
                self.moving_down = False

请注意,该方法在事件循环中被调用,并将事件传递给该方法。该方法必须处理输入参数事件:

while run:
    self.clock.tick(60)
    self.screen.fill(self.white)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            exit()
        elif event.type == pygame.K_ESCAPE:
            run = False
            pygame.quit()
            exit()
        # - Objects Event Handle - 
        self.player.moveEvent(event)

    # - Updates - 
    self.player.moveUpdate()