相同的代码不同的结果

问题描述 投票:0回答:1

在我的游戏中,我有两个玩家控制的坦克。两辆坦克具有相同的代码,但其中一辆改变了变量名称。两个水箱控制代码如下。运动控制工作正常,但电源不起作用。不管怎样,无论哪组坦克控制装置在先,它都是工作的。用户应该使用“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”来改变功率,当松开按键时,功率不会改变,而是保持在功率水平。

python python-3.x pygame user-controls
1个回答
0
投票

问题是原因,因为你有 2 个事件循环。所有事件都在第一个循环中处理,但键

pygame.K_w
pygame.K_s
pygame.K_e
pygame.K_r
在第一个事件循环中不处理。第二个事件循环根本不会收到任何事件(大部分)。
在 1 个游戏循环中实现 1 个事件循环!

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)
© www.soinside.com 2019 - 2024. All rights reserved.