Python 如何在Pygame中设置延迟/冷却?

Python 如何在Pygame中设置延迟/冷却?,python,pygame,timedelay,Python,Pygame,Timedelay,我正在为一个学校项目制作一个游戏,它正在复制任天堂的打孔!!比赛(拳击比赛) 我使用变量left和right来确定程序使用的图像,例如left=True表示我使用左减淡图像,right=True表示我使用右减淡图像。当两个变量都为False时,它将使用静止或“空闲”图像 到目前为止,我能够使角色向左或向右闪避,但我无法让他们回到中立位置,并在按下按钮后将图像更改回站立位置 我已经尝试在我想要延迟的点之间放置py.time.delay(),但到目前为止还没有成功 这是我的代码: import py

我正在为一个学校项目制作一个游戏,它正在复制任天堂的打孔!!比赛(拳击比赛)

我使用变量
left
right
来确定程序使用的图像,例如
left=True
表示我使用左减淡图像,
right=True
表示我使用右减淡图像。当两个变量都为False时,它将使用静止或“空闲”图像

到目前为止,我能够使角色向左或向右闪避,但我无法让他们回到中立位置,并在按下按钮后将图像更改回站立位置

我已经尝试在我想要延迟的点之间放置py.time.delay(),但到目前为止还没有成功

这是我的代码:

import pygame as py

py.init()
window = py.display.set_mode((1000,600))

#Variables
x = 350
y = 250
height = 20
width =5
left = False
right = False

normIdle = py.image.load('p1idle.png')

dodgeLeft = py.image.load('p1DodgeLeft.png')

dodgeRight = py.image.load('p1DodgeRight.png')

#Dodging/ Image changing
def redrawgamewindow():
    if left:
        window.blit(dodgeLeft, (x,y))

    elif right:
        window.blit(dodgeRight, (x,y))
    else:
        window.blit(normIdle, (x,y))

    py.display.update()

#Main Loop
run = True
while run:
    #py.time.delay(300)

    for event in py.event.get():
        if event.type == py.QUIT:
            run = False

    keys = py.key.get_pressed()
    if keys[py.K_LEFT]:
        left = True
        right = False
        x -= 20
        py.time.delay(300)
        left = False
        right = False
        x += 20

    if keys[py.K_RIGHT]:
        left = False
        right = True
        x += 20
        py.time.delay(300)
        left = False
        right = False
        x -= 20

    redrawgamewindow()

    window.fill((0,0,0))
py.quit()

我希望让角色向左闪避并停留几毫秒,然后自动返回到站立图像中的原始位置。

我能够通过在主循环之前开始的一些小修改使代码正常工作:

# first, before the main loop draw the game window with Idle. This didn't show up before because you were going into the loop and redrawing idle. But now that we check to see if you are redrawing idle, it is probably a good idea to initialize things with redrawgamewindow().
redrawgamewindow()

#Main Loop
run = True
while run:
    #py.time.delay(300)

    for event in py.event.get():
        if event.type == py.QUIT:
            run = False

    # this is a variable to see if the user did something that would change the graphics
    redraw_my_char = False

    keys = py.key.get_pressed()
    if keys[py.K_LEFT]:
        left = True
        right = False
        redraw_my_char = True

    #note that this does not prevent button mashing e.g. right and left at once. You 
    if keys[py.K_RIGHT]:
        left = False
        right = True
        redraw_my_char = True

    if redraw_my_char: # you could say if did Left or Right, but I assume you'll be implementing punching, so it would probably be easier just to track  one variable, redraw_my_char
        # draw left or right
        redrawgamewindow()
        py.time.delay(300)
        left = False
        right = False
        # with left or right reset, draw again
        redrawgamewindow()

    window.fill((0,0,0))
py.quit()
我删除了x+=/x-=命令,因为它们只在发送延迟之前和之后更改。我可以看到当你的游戏变得更复杂时,它们将被使用,但是对于这个问题,它们还没有用处


另外,作为一种次要的样式,为了去掉一个变量,您还可以使用
if event.type==py.QUIT:break

尝试使用
time.sleep(300)
,但您不应该直接更改
x
位置,而是让
重画gamewindow()
基于其他变量绘制位置,或者使用
move\u ip(x,y)
。谢谢!这真的很有帮助。我还能够在中实现+=和-=命令。我现在离比赛结束已经很近了。