【问题标题】:Pygame - Smoother MovementPygame - 更流畅的运动
【发布时间】:2016-08-29 07:55:10
【问题描述】:

我在main_screen 上添加了一个对象/图像,该对象称为cancer_cell。 我在这里想要做的是我希望对象顺利移动。我必须反复按箭头键才能保持移动。 如何让它移动while 箭头键被按下?

代码如下:

exitgame = False
cellpos_x = 0
cellpos_y = cancer_cell.get_rect().height*2
while not exitgame:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exitgame = True
            quitgame()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                cellpos_x -= 10
            if event.key == pygame.K_RIGHT:
                cellpos_x += 10


    gameplay_bg = pygame.image.load("/Users/wolf/Desktop/python/img/gameplay_bg.png").convert()
    main_screen.fill(white)
    main_screen.blit(gameplay_bg, [0,0])
    main_screen.blit(cancer_cell, [cellpos_x, cellpos_y])
    pygame.display.flip()
    clock.tick(20)

有人告诉我在How to use pygame.KEYDOWN 尝试解决方案: 但这也没有用。或者我做错了:

if event.type == pygame.KEYDOWN:
    key_pressed = pygame.key.get_pressed()
    if key_pressed[pygame.K_LEFT]:
        cellpos_x -= 10
    if key_pressed[pygame.K_RIGHT]:
        cellpos_x += 10

【问题讨论】:

  • 你可能想澄清你在问什么。
  • 所以我在main_screen上有这个对象/图像,称为cancer_cell,当我用箭头键移动时,我必须重复按,否则如果我按住键它不会移动下来。
  • 请编辑问题而不是评论。
  • 而且solution 并不难找到。
  • 我之前试过那个解决方案,不行。

标签: python pygame


【解决方案1】:

问题已解决

我已经通过从FOR 循环中取消缩进这部分来解决问题 虽然没有退出游戏:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exitgame = True
            quitgame()

    key_pressed = pygame.key.get_pressed()
    if key_pressed[pygame.K_LEFT]:
        cellpos_x -= 10
    if key_pressed[pygame.K_RIGHT]:
        cellpos_x += 10

【讨论】:

  • 基本上这里发生的事情是事件循环只应用一次事件效果。即if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: do stuff 表示 Pygame 将检测到您按下了左箭头键,执行您要求的任何操作一次,然后等待您释放该键并再次按下它,然后再次执行该效果。这就是为什么您必须一直按该键的原因。但是,使用 pygame.key.get_pressed() 意味着 pygame 将不断检查此值,而无需释放密钥。这是一个重要的区别。
  • 请记住这一点,例如,让精灵连续跟随鼠标位置(您可能希望pygame.mouse_getpos() 在事件循环之外),而如果您想实现一个按钮,您可能希望每次单击按钮时效果只发生一次(在这种情况下,最好使用事件循环中的条件)。
【解决方案2】:

我看到您已经解决了缩进问题,这是您示例的另一个版本:

import pygame

class Player(object):
    def __init__(self, img_path):
        self.image = pygame.image.load(img_path)
        self.x = 0
        self.y = self.image.get_rect().height*2

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_RIGHT]:
            self.x += dist
        elif key[pygame.K_LEFT]:
            self.x -= dist

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


pygame.init()
clock = pygame.time.Clock()
size = width, height = 1024, 768
speed = [2, 2]
white = 1, 1, 1
main_screen = pygame.display.set_mode(size)
gameplay_bg = pygame.image.load("background.jpg")
cancer_cell = Player("player.jpg")
running = False

while not running:
    event = pygame.event.poll()

    if event.type == pygame.QUIT:
        running = True

    main_screen.fill(white)
    main_screen.blit(gameplay_bg, [0, 0])

    cancer_cell.handle_keys()
    cancer_cell.draw(main_screen)

    pygame.display.flip()

    clock.tick(50)
    pygame.display.set_caption("fps: " + str(clock.get_fps()))

您需要调整图像的路径(“background.jpg”、“player.jpg”),在这个版本中,您不会在游戏事件循环中一遍又一遍地加载您的精灵,玩家的移动将是光滑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 2011-09-13
    • 1970-01-01
    相关资源
    最近更新 更多