【问题标题】:only last iteration of list in for loop takes effect when in while loop [Python, Pygame] for pacman game, how do i fix this?只有for循环中列表的最后一次迭代在用于pacman游戏的while循环[Python,Pygame]中生效,我该如何解决这个问题?
【发布时间】:2019-10-03 23:14:49
【问题描述】:

尝试在 pygame 中使导入的 csv 矩形在主游戏循环中的 for 循环中迭代元组列表,以添加与列表中的矩形的碰撞。只有列表中的最后一个矩形有碰撞,这意味着在游戏循环中只考虑最后一个元组的参数。

    while gameExit == False:
    for event in pygame.event.get():
        if event.type == QUIT:
            gameExit = True
        #Gets keypresses and moves the player accordingly
        if event.type == pygame.KEYDOWN:
            #print(str(event.key))
            if event.key == pygame.K_LEFT:
                xChange = -playersize/2
                yChange = 0
                #print(str(x)+", "+str(y))
            elif event.key == pygame.K_RIGHT:
                xChange = playersize/2
                yChange = 0
                #print(str(x)+", "+str(y))
            elif event.key == pygame.K_UP:
                yChange = -playersize/2
                xChange = 0
                #print(str(x)+", "+str(y))
            elif event.key == pygame.K_DOWN:
                yChange = playersize/2
                xChange = 0
        """
        #Make player stop moving after keyup
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or pygame.K_RIGHT:
                xChange = 0
            if event.key == pygame.K_UP or pygame.K_DOWN:
                yChange = 0
        """
    #if player goes out of bounds then move them to the other side of the screen
    if pacX > width or pacX < 0 or pacY > height or pacY < 0: 
        if pacX > width: pacX = 0
        if pacX < 0: pacX = width - playersize
        if pacY > height: pacY = 0
        if pacY < 0: pacY = height - playersize

    #is the movement selected going to intersect with a boundary?
    #THIS IS THE FOR LOOP REFERENCED ABOVE

        for coord in xy:
            x = float(coord[0])
            y = float(coord[1])
            rectwidth = float(coord[2])
            rectheight = float(coord[3])

            if pacX+xChange > round(x-playersize) and pacX+xChange < round(x+rectwidth) and pacY+yChange > round(y-playersize) and pacY+yChange < round(y+rectheight):
                touching = True
            else:
                touching = False


    #if not touching a boundary then allow move

    if not touching:
        pacX += xChange
        pacY += yChange
    elif touching == True:
        pass
        #print("Sorry, that's out of bounds...")



    #draw everything on the display
    DISPLAY.fill(grey)
    pygame.draw.rect(DISPLAY,yellow,[pacX,pacY,playersize,playersize])
    buildlevel(xy,DISPLAY,darkBlue)
    pygame.display.update()
    clock.tick(fps)

pygame.quit()
sys.exit()

CSV 文件: (每一个都是 pygame 中 pygame.draw.rect() 方法的坐标和尺寸参数。每一行在 xy 列表中存储为一个元组。)


100 100 100 100
200 200 100 100
300 300 100 100
300 300 100 100
400 400 100 100

【问题讨论】:

  • 我不确定,但我猜if not touching: 测试应该在for coord in xy: 循环内,不是吗?现在的方式,只是测试循环的最后一次交互,正如你所看到的那样。
  • 循环体的缩进方式,唯一的作用是设置touching变量,在下一次循环迭代时丢弃。

标签: python for-loop while-loop pygame iteration


【解决方案1】:

如果我们将您的示例简化为伪(python)代码,它看起来像这样:

for block in blocks:
    if collide(player, block):
        touching = True
    else:
        touching = False

如果您现在通过这个步骤,无论是在您的脑海中还是使用调试器,您都会意识到每次循环体执行时都会设置触摸,覆盖之前的值。要解决这个问题,只需在循环体中设置touching = True。

for block in blocks:
    if collide(player, block):
        touching = True

但是现在touching 可能还没有被初始化。这可以通过在循环前设置touching = False 来解决:

touching = False
for block in blocks:
    if collide(player, block):
        touching = True

【讨论】:

  • @Akliph 你是什么意思?什么清单?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-11
  • 1970-01-01
  • 2016-04-23
  • 2012-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多