gpt4 book ai didi

python - 相同的代码不同的结果

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:37 25 4
gpt4 key购买 nike

在我的游戏中,我有两个玩家控制的坦克。两辆坦克具有相同的代码,但其中一辆改变了变量名称。两个水箱控制代码如下。运动控制工作正常,但电源不起作用。不管怎样,无论哪组坦克控制装置在先,它都是工作的。用户应该使用“o”,“p”和“e”,“r”来改变功率,但是使用第二组控制代码,即使使用keyup线,功率也将始终在变化而不是保持不变用户松开 key 。

我看过一些堆栈溢出和 youtube 问题,但它太高级了,我无法理解。我也请 friend 和老师看过。

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_wpygame.K_spygame.K_epygame .K_r 不在第一个事件循环中处理。第二个事件循环根本不会获取任何事件(大部分)。
在 1 个游戏循环中实现 1 个事件循环!

pygame.key.get_pressed() 返回的状态被评估,当 pygame.eventspygame.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)

关于python - 相同的代码不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56546811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com