在我的游戏中,我有两个玩家控制的坦克。两辆坦克具有相同的代码,但其中一辆改变了变量名称。两个水箱控制代码如下。运动控制工作正常,但电源不起作用。不管怎样,无论哪组坦克控制装置在先,它都是工作的。用户应该使用“o”,“p”和“e”,“r”来改变功率,但是使用第二组控制代码,即使使用keyup线,功率也将始终在变化而不是保持不变用户松开钥匙。
我看过一些堆栈溢出和 youtube 问题,但它对我来说太高级了,无法理解。 我也请朋友和老师看过。
keys= pygame.key.get_pressed()
if keys[pygame.K_j]:
tank1x -= tankmove
elif keys[pygame.K_l]:
tank1x += tankmove
elif keys[pygame.K_i]:
changeturret1 = 1
elif keys[pygame.K_k]:
changeturret1 = -1
elif keys[pygame.K_u]:
fire1(gun1,tank1x,tank1y,currentturretpos1,fire_power1)
elif keys[pygame.K_o]:
power_change1 = -1
elif keys[pygame.K_p]:
power_change1 = 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_i or event.key == pygame.K_k:
changeturret1 = 0
if event.key == pygame.K_o or event.key == pygame.K_p:
power_change1 = 0
currentturretpos1 += changeturret1
if currentturretpos1 > 9:
currentturretpos1 = 9
elif currentturretpos1 < 0:
currentturretpos1 = 0
fire_power1 += power_change1
if fire_power1 > 100:
fire_power1 = 100
elif fire_power1 < 1:
fire_power1 = 1
message("Power: "+str(fire_power1)+"%",black,850,100)
if keys[pygame.K_a]:
tankx -= tankmove
elif keys[pygame.K_d]:
tankx += tankmove
elif keys[pygame.K_w]:
changeturret = 1
elif keys[pygame.K_s]:
changeturret = -1
elif keys[pygame.K_q]:
fire(gun,tankx,tanky,currentturretpos,fire_power)
elif keys[pygame.K_e]:
power_change = -1
elif keys[pygame.K_r]:
power_change = 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_w or event.key == pygame.K_s:
changeturret = 0
if event.key == pygame.K_e or event.key == pygame.K_r:
power_change = 0
currentturretpos += changeturret
if currentturretpos > 9:
currentturretpos = 9
elif currentturretpos < 0:
currentturretpos = 0
fire_power += power_change
if fire_power > 100:
fire_power = 100
elif fire_power < 1:
fire_power = 1
message("Power: "+str(fire_power)+"%",black,0,100)
用户应使用“e”、“r”和“o”、“p”来改变功率,当松开按键时,功率不会改变,而是保持在功率水平。
问题是原因,因为你有 2 个事件循环。所有事件都在第一个循环中处理,但键
pygame.K_w
、pygame.K_s
、pygame.K_e
、pygame.K_r
在第一个事件循环中不处理。第二个事件循环根本不会收到任何事件(大部分)。pygame.key.get_pressed()
s 由 pygame.event
或 pygame.event.get()
处理时,将评估由 pygame.event.pump()
返回的状态。pygame.key.get_pressed()
:
# the one and only event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_i or event.key == pygame.K_k:
changeturret1 = 0
if event.key == pygame.K_o or event.key == pygame.K_p:
power_change1 = 0
if event.key == pygame.K_w or event.key == pygame.K_s:
changeturret = 0
if event.key == pygame.K_e or event.key == pygame.K_r:
power_change = 0
# get the key states after 'pygame.event.get()'
keys = pygame.key.get_pressed()
if keys[pygame.K_j]:
tank1x -= tankmove
elif keys[pygame.K_l]:
tank1x += tankmove
elif keys[pygame.K_i]:
changeturret1 = 1
elif keys[pygame.K_k]:
changeturret1 = -1
elif keys[pygame.K_u]:
fire1(gun1,tank1x,tank1y,currentturretpos1,fire_power1)
elif keys[pygame.K_o]:
power_change1 = -1
elif keys[pygame.K_p]:
power_change1 = 1
currentturretpos1 += changeturret1
if currentturretpos1 > 9:
currentturretpos1 = 9
elif currentturretpos1 < 0:
currentturretpos1 = 0
fire_power1 += power_change1
if fire_power1 > 100:
fire_power1 = 100
elif fire_power1 < 1:
fire_power1 = 1
message("Power: "+str(fire_power1)+"%",black,850,100)
if keys[pygame.K_a]:
tankx -= tankmove
elif keys[pygame.K_d]:
tankx += tankmove
elif keys[pygame.K_w]:
changeturret = 1
elif keys[pygame.K_s]:
changeturret = -1
elif keys[pygame.K_q]:
fire(gun,tankx,tanky,currentturretpos,fire_power)
elif keys[pygame.K_e]:
power_change = -1
elif keys[pygame.K_r]:
power_change = 1
currentturretpos += changeturret
if currentturretpos > 9:
currentturretpos = 9
elif currentturretpos < 0:
currentturretpos = 0
fire_power += power_change
if fire_power > 100:
fire_power = 100
elif fire_power < 1:
fire_power = 1
message("Power: "+str(fire_power)+"%",black,0,100)