为什么蓝色方块在 Python pygame 库中无限移动

Why is blue square moving indefinitely in Python pygame library

我正在学习 python 并尝试使用 pygame 库制作贪吃蛇游戏。我制作了一个矩形,它使用按键移动,但不是移动一个块,而是移动到块的末尾。 每当我向任何方向按下时,无论是向上、向左、向右还是向下,而不是移动 1 个框大小,它都会移动到另一个方向的末端 谁能告诉我为什么会这样。

import pygame


box_size = 50
num_box = 15
color1 = (169, 215, 81)
color2 = (169,250,81)
x = 0
y = 0

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            break

    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT]:
        if x < width - box_size:
            x = x + box_size

    if keys[pygame.K_LEFT]:
        if x > 0:
            x = x - box_size

    if keys[pygame.K_UP]:
        if y > 0:
            y = y - box_size

    if keys[pygame.K_DOWN]:
        if y < height - box_size:
            y = y + box_size


    print(x,y)
    for i in range(0,num_box):
        if i %2 == 0:
            for j in range(0,num_box):
                if j%2 == 0:
                    pygame.draw.rect(win, color1, (box_size*j, box_size*i, box_size, box_size))
                else:
                    pygame.draw.rect(win, color2, (box_size * j, box_size * i, box_size, box_size))

        else:
            for k in range(0,num_box):
                if k%2 == 0:
                    pygame.draw.rect(win, color2, (box_size*k, box_size*i, box_size, box_size))
                else:
                    pygame.draw.rect(win, color1, (box_size * k, box_size * i, box_size, box_size))
    pygame.draw.rect(win, (0, 0, 250), (x, y, box_size, box_size))
   
    # # rect(surface, color, (left, top, width, height))


    pygame.display.update()
    pass

参见pygame.time.Clock.tick():

This method should be called once per frame.

使用pygame.time.Clock to control the frames per second and thus the game speed. The method tick() of a pygame.time.Clock对象,以这种方式延迟游戏,即循环的每次迭代都消耗相同的时间。例如:

clock = pygame.time.Clock()
while True:
    clock.tick(100)

    for event in pygame.event.get():
        # [...]

如果你想一步步移动对象你需要使用KEYDOWN事件而不是pygame.key.get_pressed():

run = True
while run:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        
    # INDENTATION
    #-->|    

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                if x < width - box_size:
                    x = x + box_size

            if event.key == pygame.K_LEFT:
                if x > 0:
                    x = x - box_size

            if event.key == pygame.K_UP:
                if y > 0:
                    y = y - box_size

            if event.key == pygame.K_DOWN:
                if y < height - box_size:
                    y = y + box_size

参见 How to get keyboard input in pygame? and How can I make a sprite move when key is held down

pygame.key.get_pressed() returns a sequence with the state of each key. If a key is held down, the state for the key is True, otherwise False. Use pygame.key.get_pressed() 评估按钮的当前状态并获得连续移动。
键盘事件(参见 pygame.event 模块)仅在键状态改变时发生一次。 KEYDOWN 事件在每次按下一个键时发生一次。 KEYUP 每松开一个键就会出现一次。将键盘事件用于单个动作,例如跳跃或产生子弹或 step-by-step 移动。