【问题标题】:How do I make my rocket go back to its position when hitting the border?如何让我的火箭在击中边界时回到原来的位置?
【发布时间】:2021-11-14 09:10:24
【问题描述】:

我是编程新手,从 pygame 零开始。我正在制作一个小游戏,你向外星人发射火箭。但是我的火箭在发射时一直卡在边界上,我做了一个重新加载功能,但我希望它自动运行(当它击中边界或外星人回到正常位置时)。谁能帮我解决这个问题?

alien = Actor('alien', (100,100))
ship =Actor('ship', (500,400))
rocket_fire = Actor('rocket_fire', (500,400))


WIDTH = 1000
HEIGHT =500

def draw():
    screen.clear()
    screen.blit('space_back', (0,0))
    rocket_fire.draw()
    ship.draw()
    alien.draw()

def move_ship(ship):
    if keyboard.left:
        ship.x -= 3
        rocket_fire.x -= 3

    elif keyboard.right:
        ship.x += 3
        rocket_fire.x += 3

    elif keyboard.space:
        animate(rocket_fire, pos = (ship.x,0))

    elif keyboard.r:
        rocket_fire.pos = (ship.x,ship.y)

def move_alien(alien):
    alien.right +=2

    if alien.left > WIDTH:
        alien.right = 0
    collide = rocket_fire.colliderect(alien)

    if collide == 0:
        alien.image = 'alien'
    elif collide == 1:
        alien.image = 'nuclear_explosion'

def update():
    rocket_fire.draw()
    ship.draw()
    alien.draw()
    move_ship(ship)
    move_alien(alien)

【问题讨论】:

标签: python pgzero


【解决方案1】:

您可以尝试在“collide == 1”之后将初始值分配给“rocket_fire”。

elif collide == 1:
    alien.image = 'nuclear_explosion'
    // rocket_fire = (500,400) -> this is just a representation to assign position; not accurate as per the code

【讨论】:

  • 它起作用了,但它只有在与外星演员碰撞时才会返回。我可以这样做,但如果它碰到边界?
  • 是的,你可以尝试在火箭击中边界时分配位置值,但你需要编写一个检测边界的条件语句:如果边界,则分配位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多