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

3
我正在为学校项目制作一个游戏,它是复制了任天堂的Punch-Out!!游戏(一个拳击游戏)。
我使用变量“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()

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


尝试使用time.sleep(300),但不应直接更改x位置,而是使redrawgamewindow()根据附加变量绘制位置,或使用move_ip(x, y) - amodrono
1个回答

0

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

# 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


1
谢谢!这真的很有帮助。我已经成功地实现了+=和-=命令。现在我离完成游戏更近了一步。 - user11909869

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接