按键时Pygame图像不移动

时间:2016-02-23 17:27:41

标签: python pygame

运行程序时,Player类的更新部分不起作用。它会显示图片,但按下按键时我无法向右或向左移动。它一动不动。我错过了什么?

import pygame
import random
import os

pygame.init()
pygame.mixer.init()

display_width = 460
display_height = 600
FPS = 30

#colors
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Test")
clock = pygame.time.Clock()

img_dir = os.path.join(os.path.dirname(__file__), 'img')

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(player_img, (40,60))
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()
        self.rect.centerx = display_width/2
        self.rect.bottom = display_height - 10
        self.speedx = 0

    def update(self):
        self.speedx = 0
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    self.speedx = -7
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    self.speedx = 7
                    self.rect.x += self.speedx



#image loading
player_img = pygame.image.load(os.path.join(img_dir, "malirozi.png")).convert()

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

#game loop
running = True
while running:
    #Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    #Logic
    all_sprites.update()

    #Draw / render
    game_display.fill(black)
    all_sprites.draw(game_display)
    #after drawing everything, flip the display
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()
quit()

2 个答案:

答案 0 :(得分:1)

pygame.event.get的调用会消耗队列中的所有事件。 这意味着它应该在一个地方完成 - 当你这样做时 for event in pygame.event.get(): 在主要的博伊德,只是吼着while循环,你吞下所有 可能会在keydown方法中读取的update个事件。

对此的补救措施是确保只在代码中的一个点上拨打pygame.event.get。例如,您可以将未处理的事件附加到列表中,并将该列表作为参数传递给更新方法 - 您可以检查这些事件,而不是再次调用pygame.event.get

另请注意,行self.rect.x += self.speedx应位于if语句之外。

...
class Player(pygame.sprite.Sprite):
    def __init__(self):
       ...
       self.events = []
    def update(self):
        self.speedx = 0
        for event in self.events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    self.speedx = -7
                if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    self.speedx = 7
        self.rect.x += self.speedx
...
while running:
    #Events
    events = []
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        else:
            events.append(event)
    player.events = events
    #Logic
    all_sprites.update()
...
pygame.quit()
quit()

答案 1 :(得分:0)

你的问题是你的事件处理在两个不同的地方,而pygame一次只能运行一个事件处理程序。我假设从你的代码中你只希望你的家伙每次按下时移动7个像素。如果你想让他继续前进,你需要添加更多

import pygame
import random
import os

pygame.init()
pygame.mixer.init()

display_width = 460
display_height = 600
FPS = 30

#colors
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Test")
clock = pygame.time.Clock()

img_dir = os.path.join(os.path.dirname(__file__), 'img')

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.transform.scale(player_img, (40,60))
        self.image.set_colorkey(black)
        self.rect = self.image.get_rect()
        self.rect.centerx = display_width/2
        self.rect.bottom = display_height - 10
        self.speedx = 0


#image loading
player_img = pygame.image.load("guy.png").convert()

all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

#game loop
running = True
while running:
    #Events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                player.speedx = -7
            if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                player.speedx = 7
        player.rect.x += player.speedx
    #Logic
    all_sprites.update()
    player.rect.centerx += player.speedx
    print(player.speedx)

    #Draw / render
    game_display.fill(black)
    all_sprites.draw(game_display)
    #after drawing everything, flip the display
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()
quit()