【问题标题】:how to shoot bullets in pygame? [duplicate]如何在pygame中射击子弹? [复制]
【发布时间】:2020-11-16 18:18:10
【问题描述】:

我正在制作一个太空入侵者类型的游戏,我想问一下我如何才能做到这一点,以便以下代码中的子弹一旦发射就不会跟随宇宙飞船。

import pygame, random, math, sys

pygame.init()
window = pygame.display.set_mode((400, 700))
background = pygame.image.load('sky(background).png')

character = pygame.image.load('start-up.png')
characterX = 165
characterY = 600
x_speed = 0

def character_blit(x, y):
    window.blit(character, (x, y))

bullet = pygame.image.load('torpedo (1).png')
bulletY = 600
y_speed_bullet = 0

def bullet_blit(x, y):
    window.blit(bullet, (x, y))

comets = pygame.image.load('asteroid.png')
comet_x = random.randint(0, 336)
comet_y = 0
y_speed = 0.3

def comet_blit(x, y):
    window.blit(comets, (x, y))

def iscollision(x1, y1, x2, y2):
    distance = math.sqrt((math.pow(x1 - x2, 2))
                         + (math.pow(y1 - y2, 2)))
    if distance < 47:
        return True
    else:
        return False


transparent = (0, 0, 0, 0)
score = 0
comets_that_hit_earth = 0
run = True
while run:
    window.fill((0, 0, 0))
    window.blit(background, (0, 0))
    bulletX = characterX + 20
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_speed = -0.3
            if event.key == pygame.K_RIGHT:
                x_speed = 0.3
            if event.key == pygame.K_SPACE:
                y_speed_bullet = y_speed * -1 * 2.3
                bulletX = 
    collision = iscollision(bulletX, bulletY, comet_x, comet_y)

    if collision:
        score += 1
        comet_y = -64
        comet_x = random.randint(0, 336)
        bulletY = 600
    elif bulletY <= -24:
        bulletY = 600

    collision_player_and_comet = iscollision(characterX, characterY, comet_x, comet_y)
    if collision_player_and_comet:
        sys.exit()
    characterX += x_speed
    comet_y += y_speed
    bulletY += y_speed_bullet
    if characterX <= -64:
        characterX = 400
    elif characterX >= 464:
        characterX = 0
    if comet_y >= 764:
        comet_y = 0
        y_speed += 0.005
        comets_that_hit_earth += 1
        comet_x = random.randint(0, 336)
    bullet_blit(bulletX, bulletY)
    character_blit(characterX, characterY)
    comet_blit(comet_x, comet_y)
    pygame.display.update()

我尝试将 bulletX 更改为被击中时的 bulletX,但无法管理。我需要为子弹使用完全不同的系统还是可以用我的系统来做。

【问题讨论】:

    标签: python pygame bullet


    【解决方案1】:

    您只需要设置一次bulletX = characterX + 20 - 当子弹发射时。目前,您在每次 while run 循环迭代时将其设置为该值,这就是它的位置不断更新的原因。因此,如果您删除了它,并且仅在需要时更新 X 位置,那么您的代码部分可能会如下所示:

    if event.key == pygame.K_SPACE:
        y_speed_bullet = y_speed * -1 * 2.3
        bulletX = characterX + 20
    

    虽然你会想要一些东西来跟踪子弹当前是否被发射,这样按住或反复按空格不会重置子弹的位置。

    【讨论】:

      【解决方案2】:

      不要在应用循环中连续设置x坐标。

      设置y_speed_bullet == 0时的x坐标。这导致子弹被放在飞船旁边,但在发射时沿着一条直线:

      if y_speed_bullet == 0:
          bulletX = characterX + 20
      

      设置按下SPACE时子弹的起始位置:

      if event.key == pygame.K_SPACE:
          y_speed_bullet = y_speed * -1 * 2.3
          bulletX = characterX + 20
      

      当子弹射出窗外或撞击彗星时设置y_speed_bullet = 0


      run = True
      while run:
          window.fill((0, 0, 0))
          window.blit(background, (0, 0))
      
          if y_speed_bullet == 0:
              bulletX = characterX + 20
      
          for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  run = False
              if event.type == pygame.KEYDOWN:
                  if event.key == pygame.K_LEFT:
                      x_speed = -0.3
                  if event.key == pygame.K_RIGHT:
                      x_speed = 0.3
                  if event.key == pygame.K_SPACE:
                      y_speed_bullet = y_speed * -1 * 2.3
                      bulletX = characterX + 20
      
          collision = iscollision(bulletX, bulletY, comet_x, comet_y)
          if collision:
              score += 1
              comet_y = -64
              comet_x = random.randint(0, 336)
          if collision or bulletY <= -24:
              bulletY = 600
              y_speed_bullet = 0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 2014-02-21
        • 2019-06-02
        相关资源
        最近更新 更多