首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame中循环不循环,'+=‘只执行一次

Pygame中循环不循环,'+=‘只执行一次
EN

Stack Overflow用户
提问于 2020-06-30 02:15:20
回答 3查看 40关注 0票数 0

我正试着用pygame做一个完美的克隆来练习。当我的角色移动时,我希望它经历一个行走周期。问题是,当角色移动时,它只在行走循环中显示图像2,而不是循环整个过程。

我创建了一个名为Player的类,以便可以加载任意数量的播放器,并在其中创建了两个单独的函数,用于向左移动和向右移动。下面是我的代码:

代码语言:javascript
复制
class PLAYER(pygame.sprite.Sprite):
     def __init__(self, fighter):
          super().__init__()
          width = 40
          height = 110
          self.image = pygame.Surface([width, height])
          self.rect = self.image.get_rect()
          self.change_x = 0
          self.change_y = 0
          self.fighter = fighter

          if self.fighter == "Frozone":
               self.FaceLeft = pygame.image.load("FrozoneFaceLeft.png")
               self.WalkLeft = pygame.image.load("FrozoneWalkLeft1.png")

          self.LWalkCycle = [self.FaceLeft, self.WalkLeft]

          self.index = 0
          self.image = self.LWalkCycle[self.index]

     def go_left(self, x):
          self.index += 1
          self.rect.x += x
          self.image = self.LWalkCycle[self.index]
          if self.index >= len(self.LWalkCycle):
               self.index = 0

“Face left”精灵也是步行循环的一部分。

下面是游戏的主要功能

代码语言:javascript
复制
def main():
     pygame.init()
     screen = pygame.display.set_mode([1400, 750])
     player1 = PLAYER("Frozone")
     activePlayers = pygame.sprite.Group()
     activePlayers.add(player1)
     clock = pygame.time.Clock()

     while True:
          for event in pygame.event.get():
               if event.type == pygame.QUIT:
                    pygame.quit()
               elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_a:
                         player1.go_left(-3)

          activePlayers.update()
          activePlayers.draw(screen)
          clock.tick(10)
          pygame.display.flip()

if __name__ == '__main__':
     main()

还有一个停止功能,在'a‘键上升后激活。它会将self.index重置为0,我确信它是有效的。

我发现并不是每次执行go_left()时self.index都会增加。我不知道为什么。

EN

回答 3

Stack Overflow用户

发布于 2020-06-30 02:23:06

在使用self.index时,它看起来永远不可能是0。

可以这样想:只要输入方法,就递增它,然后使用递增后的值。在溢出时将其重置为0,然后在进入方法后立即递增。

代码语言:javascript
复制
 # Index is 0 here
 self.index += 1  # Then incremented to 1

 self.image = self.LWalkCycle[self.index]  # Then the incremented value is used.

 if self.index >= len(self.LWalkCycle):
     self.index = 0

我看到了两种解决方案:

在使用self.index.之后,

  • 在-1处开始self.index,这样在递增时它就是0(丑陋,而不是sensical).
  • Increment
票数 0
EN

Stack Overflow用户

发布于 2020-06-30 03:34:21

这里有一个可能更干净的替代方案……您可以使用itertools中的cycle来管理循环。或者,您可以自己处理索引,并必须将其重置为-1,我同意这有点难看。

代码语言:javascript
复制
import pygame
from itertools import cycle

class PLAYER(pygame.sprite.Sprite):
     def __init__(self, fighter):
          super().__init__()
          width = 40
          height = 110
          self.image = pygame.Surface([width, height])
          self.rect = self.image.get_rect()
          self.change_x = 0
          self.change_y = 0
          self.fighter = fighter

          if self.fighter == "Frozone":
               self.FaceLeft = pygame.image.load("fig1.png")
               self.WalkLeft = pygame.image.load("fig2.png")

          self.LWalkCycle = [self.FaceLeft, self.WalkLeft]
          # make a cycle of the possible indices...  in this case, it is just [0, 1]
          self.l_walk_index = cycle(range(len(self.LWalkCycle)))

          self.index = 0
          self.image = self.LWalkCycle[self.index]

     def go_left(self, x):
          idx = next(self.l_walk_index)  # increment in the cycle
          self.image = self.LWalkCycle[idx]
          self.rect.x += x
票数 0
EN

Stack Overflow用户

发布于 2020-07-01 09:57:40

问题最终导致该函数仅在按键时执行,而不是每隔一帧检查按键是否被按下。

我替换了下面的代码:

代码语言:javascript
复制
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_a:
            player1.go_left(-3)

有了这个:

代码语言:javascript
复制
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
    player1.go_left(-3)

这样,它就会检查该键是否在此循环中按下,而不管它是否在上一次循环中按下

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62644312

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档