如何把 delay/cooldown 放在 Pygame 中?
How to put delay/cooldown in Pygame?
我正在为一个学校项目制作游戏,它正在复制任天堂的 Punch-Out!!游戏(拳击游戏)。
我使用变量 left
和 right
来确定程序使用哪个图像,例如left = True
表示我使用左闪避图像,right = True
表示我使用右闪避图像。当两个变量都为 False 时,它使用站立或 'idle' 图像。
到目前为止我可以让角色向左或向右闪避,但我无法让他们回到中立位置并在按下按钮后将图像变回站立位置。
我试过在我想要延迟的点之间放置一个 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
.
我正在为一个学校项目制作游戏,它正在复制任天堂的 Punch-Out!!游戏(拳击游戏)。
我使用变量 left
和 right
来确定程序使用哪个图像,例如left = True
表示我使用左闪避图像,right = True
表示我使用右闪避图像。当两个变量都为 False 时,它使用站立或 'idle' 图像。
到目前为止我可以让角色向左或向右闪避,但我无法让他们回到中立位置并在按下按钮后将图像变回站立位置。
我试过在我想要延迟的点之间放置一个 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
.