具有相同结果的相同代码

时间:2019-06-11 15:13:54

标签: python python-3.x pygame user-controls

在我的游戏中,我有两个由玩家控制的坦克。两个储罐具有相同的代码,但其中一个已更改了变量名称。以下是两个储罐控制代码。运动控件工作正常,但是力量却无法发挥作用。无论哪种方式,无论哪种油箱控制装置是第一个,它都是有效的。用户应使用“ o”,“ p”和“ e”,“ r”来更改功率,但是使用第二组控制代码(即使使用键控线),功率也将始终在变化而不是停滞不前。用户放开钥匙。

我看过一些堆栈溢出和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”来更改功率,放开键后,功率不会改变,而是保持在功率水平。

1 个答案:

答案 0 :(得分:0)

问题是原因,因为您有2个事件循环。所有事件都在第一个循环中处理,但是键pygame.K_wpygame.K_spygame.K_epygame.K_r在第一个事件循环中不处理。第2个事件循环根本不会获得任何事件(大部分)。
在1个游戏循环中实现1个事件循环!

pygame.key.get_pressed()pygame.events处理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)