只有for循环中list的最后一次迭代在while循环[Python,Pygame]中生效,对于pacman游戏,如何解决这个问题?

2024-04-29 13:04:42 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试使pygame中rects的导入csv遍历主游戏循环中for循环中的元组列表,以便添加与列表中rects的冲突。列表中只有最后一个rect有冲突意味着在游戏循环中只考虑最后一个元组的参数

    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

Tags: orthekeyeventifpygameprintstr
1条回答
网友
1楼 · 发布于 2024-04-29 13:04:42

如果我们将您的示例简化为伪(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

相关问题 更多 >