简体   繁体   中英

Can't move object in pygame

I'm doing a project for my Computer Science class where we're using pygame to move an object around the screen. I'm not using the pygame sprites, but rather images imported into pygame and using those to act as custom sprites. When i try to move the Bunny, or "Player" object across the screen it does seem to update the position. This is the Player object i'm trying to change position:

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

And this is the class i'm using to run the game itself:

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

Remove the event loop from the 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

Note, the method is called in an event loop and the event is passed to the method. The method has to handle the input parameter event:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM