python 如何知道子弹从枪击中目标在Pygame雪碧

b1zrtrql  于 2022-10-30  发布在  Python
关注(0)|答案(1)|浏览(171)

我正在制作一个游戏与pygame和sprite,游戏有鸟,枪和子弹类。
当枪射出一颗子弹,子弹碰到鸟时,鸟和子弹应该立即消失。
我现在的挑战是知道子弹什么时候碰到鸟,然后让子弹和鸟一起消失。
我意识到的一件事是,当我朝鸟的方向开枪时,我的鸟不知何故消失了,但我发现,在子弹击中鸟之前,鸟就消失了。
有人能看看我的代码,告诉我哪里做错了吗?

import pygame
import os
import random

BACKGROUND_IMAGE = pygame.image.load(
os.path.join("Assets", "2dforest.jpeg"))

BACKGROUND_WIDTH, BACKGROUND_HEIGHT = 900, 500
pygame.display.set_caption("Hunting Birds")

WIN = pygame.display.set_mode((BACKGROUND_WIDTH, BACKGROUND_HEIGHT))
COLOR = (255, 200, 98)

class Birds(pygame.sprite.Sprite):

   def __init__(self, image, width, height, pos_x, pos_y):
      super().__init__()
      self.width = width
      self.height = height
      self.pos_x = pos_x
      self.pos_y = pos_y
      self.image = pygame.image.load(
        os.path.join("Assets", image)).convert_alpha()
      self.image = pygame.transform.scale(self.image, (175, 175))
      self.rect = self.image.get_rect()
      self.rect.center = (self.pos_x, self.pos_y)

class HunterGun(pygame.sprite.Sprite):

  def __init__(self, image, width, height, pos_x, pos_y):
    super().__init__()
    self.width = width
    self.height = height
    self.pos_x = pos_x
    self.pos_y = pos_y
    self.image = pygame.image.load(
        os.path.join("Assets", image)).convert_alpha()
    self.image = pygame.transform.scale(self.image, (175, 175))
    self.rect = self.image.get_rect()
    self.rect.center = (self.pos_x, self.pos_y)

 def move_and_turn(self, mover):
    keys_pressed = pygame.key.get_pressed()
    if (keys_pressed[pygame.K_LEFT]):
        pos_x, pos_y = self.rect.center
        pos_x -= mover
        self.rect.center = (pos_x, pos_y)
    elif (keys_pressed[pygame.K_RIGHT]):
        pos_x, pos_y = self.rect.center
        pos_x += mover
        self.rect.center = (pos_x, pos_y)
    elif (keys_pressed[pygame.K_UP]):
        pos_x, pos_y = self.rect.center
        pos_y -= mover
        self.rect.center = (pos_x, pos_y)
    elif (keys_pressed[pygame.K_DOWN]):
        pos_x, pos_y = self.rect.center
        pos_y += mover
        self.rect.center = (pos_x, pos_y)
        print(self.rect, "Down")

def rotate(self, angle):
    self.angle = angle
    self.image = pygame.transform.rotate(
        self.image, self.angle)
    self.pos_y += 2
    self.pos_x -= 18
    print(self.pos_x)
    self.rect.center = (self.pos_x, self.pos_y)
    self.rect = self.image.get_rect(center=(self.rect.center))

def create_bullect(self):
    pos_x, pos_y = self.rect.center

    return Bullet(pos_x + 95, pos_y - 67)

class Bullet(pygame.sprite.Sprite):

  def __init__(self, pos_x, pos_y):
    super().__init__()
    self.pos_x = pos_x
    self.pos_y = pos_y
    self.image = pygame.image.load(
        os.path.join("Assets", "bullet.png")).convert_alpha()
    self.image = pygame.transform.scale(self.image, (50, 80))
    self.rect = self.image.get_rect(center=(pos_x, pos_y))

  def update(self):
    self.rect.x += 13
    self.pos_x
    if(self.rect.x >= BACKGROUND_WIDTH + 200):
        print(self.rect)
        self.kill()
    elif(self.rect.y >= BACKGROUND_HEIGHT + 200):
        self.kill()

birds_sprite = pygame.sprite.Group()
for target in range(10):
new_target = Birds("birds.png", 100, 100,
                   random.randrange(0, BACKGROUND_WIDTH),               random.randrange(0, BACKGROUND_HEIGHT))
birds_sprite.add(new_target)

gunhunter = HunterGun("gunner.png", 100, 100,
                  BACKGROUND_WIDTH // 2, BACKGROUND_HEIGHT//2)
gun_sprite = pygame.sprite.Group()
gun_sprite.add(gunhunter)

bullet_group = pygame.sprite.Group()

pygame.init()
clock = pygame.time.Clock()
run = True

while run:
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         run = False
      keys_pressed = pygame.key.get_pressed()
      if (keys_pressed[pygame.K_k]):
         bullet_group.add(gunhunter.create_bullect())
         pygame.sprite.spritecollideany(
            birds_sprite, bullet_group, True, True)

      if(keys_pressed[pygame.K_r]):
          gunhunter.rotate(3)

    picture = pygame.transform.scale(BACKGROUND_IMAGE, (1000, 540))
    pygame.display.flip()

    WIN.blit(picture, (0, 0))

    birds_sprite.draw(WIN)
    bullet_group.draw(WIN)
    gun_sprite.draw(WIN)

    gunhunter.move_and_turn(4)
    birds_sprite.update()
    gun_sprite.update()
    bullet_group.update()
    clock.tick(60)

pygame.quit()
3pvhb19x

3pvhb19x1#

pygame.key.get_pressed()不是一个事件。pygame.key.get_pressed()返回一个序列,其中包含每个键的状态。如果某个键被按住,则该键的状态为1,否则为0。因此,您可以使用keys[pygame.K_d] - keys[pygame.K_a]keys[pygame.K_s] - keys[pygame.K_w]计算按下2个键时的移动结果。使用pygame.key.get_pressed()评估按钮的当前状态并获得连续移动。
键盘事件(请参阅pygame.event模块)只会在按钮状态变更时发生一次。KEYDOWN事件会在每次按下按钮时发生一次。KEYUP会在每次放开按钮时发生一次。请使用键盘事件来执行单一动作。
另请参阅How can i shoot a bullet with space bar?How do I stop more than 1 bullet firing at once?
冲突检测必须在应用程序循环中完成。另请参见How do I detect collision in pygame?
例如:

while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
           if event.key == pygame.K_k:
              bullet_group.add(gunhunter.create_bullect())

    if(keys_pressed[pygame.K_r]):
          gunhunter.rotate(3)

    pygame.sprite.spritecollideany(birds_sprite, bullet_group, True, True)

相关问题