我正在为学校项目制作一个游戏,它是复制了任天堂的Punch-Out!!游戏(一个拳击游戏)。
我使用变量“left”和“right”来确定程序使用哪个图像,例如,“left = True”表示使用左侧闪避图像,“right = True”表示使用右侧闪避图像。当这两个变量都为False时,它使用站立或“空闲”图像。
到目前为止,我能够让角色向左或向右闪避,但我无法使他们回到中立位置并在按下按钮后将图像更改回站立位置。
我尝试在我想要延迟的点之间放置py.time.delay(),但到目前为止它还没有起作用。
以下是我的代码:
我使用变量“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