产生多个敌人并在子弹击中时获取信息

时间:2019-12-24 21:42:08

标签: python object pygame

我正在尝试产生多个敌人,但我无法使其正常工作...请需要帮助,我正在使用分组,如您所见。 我也想获得射弹击中目标的信息,我用谷歌搜索并找到了一种使敌人和子弹消失的方法,但是我想让信息在发生时存储在变量中,但我不知道如何。 这是代码行:

enemy_hit = pygame.sprite.groupcollide(projectiles, enemies, True, pygame.sprite.collide_circle)

下面的代码是我使用的完整代码,正如我之前写的那样,我想产生多个敌人。

import pygame
pygame.init()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        self.images = self.one, self.two, self.three, = pygame.image.load("pictures\SpaceShip\one.png"), pygame.image.load("pictures\SpaceShip\\two.png"), pygame.image.load("pictures\SpaceShip\\three.png")
        self.image = self.one
        self.imageCount = 1
        self.rect = self.image.get_rect(center = (512, 630))
        self.speed = 4
        self.image = pygame.transform.scale(self.image,(81,80))

    def nextpicture(self):
        if self.imageCount == 1:
            self.image = self.one
            self.imageCount = 2
        elif self.imageCount == 2:
            self.image = self.two
            self.imageCount = 3
        elif self.imageCount == 3:
            self.image = self.three
            self.imageCount = 1
        self.image = pygame.transform.scale(self.image,(81,80))

class Projectile(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("pictures\shot.png")
        self.rect = self.image.get_rect(center = (player.rect.x+41, 610))
        self.speed = 4

    def move(self):
        self.rect.y -= self.speed
        if self.rect.y < 0:
            self.kill()

class Enemy(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("pictures\Enemy_Ufo.png")
        self.speed = 1
        self.y = 200
        self.x = 200
        self.rect = self.image.get_rect(center = (self.x,self.y))
        self.hit = False
    def __add__(self):
        self.x += 100
    def spawn(self):
        self.x = self.x + 100

    def death(self):
        pass

size = width, height = 1024, 768
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
background = 5, 7, 37
player = Player()
enemy = Enemy()
projectiles = pygame.sprite.Group()
enemies = pygame.sprite.Group()
icon = pygame.image.load('pictures\SpaceShip\SpaceShip_LowRez.png')
counter = 0
enemyCount = 0
for i in range(5):
    enemies.add(Enemy())
    print(Enemy().x)
    enemy.__add__()

run = True
while run:
    enemy_hit = pygame.sprite.groupcollide(projectiles, enemies, True, pygame.sprite.collide_circle)
    for hit in enemy_hit:
        enemies.add(Enemy())

    clock.tick(120)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_SPACE or event.key == pygame.K_w or event.key == pygame.K_UP) and len(projectiles) == 0:
              projectiles.add(Projectile())



    keys = pygame.key.get_pressed()
    if (keys[pygame.K_RIGHT] or keys[pygame.K_d]) == True and player.rect.x != width-80:
        player.rect.x += player.speed
    if (keys[pygame.K_LEFT] or keys[pygame.K_a]) == True and player.rect.x != 0:
        player.rect.x -= player.speed

    for projectile in projectiles:
        projectile.move()

    #Changes picture of the ship
    counter += 1
    if counter/4 == 1:
        player.nextpicture()
    if counter == 20:
        counter = 0

    enemy.death()
    pygame.display.set_icon(icon)
    pygame.display.set_caption("Space Invaders")
    screen.fill(background)
    projectiles.draw(screen)
    enemies.draw(screen)
    screen.blit(player.image, player.rect.topleft)
    pygame.display.flip()

1 个答案:

答案 0 :(得分:0)

将x坐标的参数添加到类Enemy的构造函数中:

class Enemy(pygame.sprite.Sprite):
    def __init__(self, x=200):
        super().__init__()
        self.image = pygame.image.load("pictures\Enemy_Ufo.png")
        self.speed = 1
        self.y = 200
        self.x = x
        self.rect = self.image.get_rect(center = (self.x,self.y))
        self.hit = False

使用random.randint()在任意位置创建敌人:

for i in range(5):
    enemies.add(Enemy(random.randint(0, width)))

当敌人被击中时增加score,并使用pygame.font(甚至是pygame.freetype模块)显示当前分数:

font = pygame.font.SysFont(None, 50)
score = 0

run = True
while run:

    # [...]

    enemy_hit = pygame.sprite.groupcollide(projectiles,enemies,True,pygame.sprite.collide_circle)
    for hit in enemy_hit:
        enemies.add(Enemy(random.randint(0, width)))
        score += 1

    # [...]

    screen.fill(background)

    text = font.render(str(score), False, (255, 255, 255))
    screen.blit(text, (20, 20))

    projectiles.draw(screen)
    enemies.draw(screen)
    screen.blit(player.image, player.rect.topleft)
    pygame.display.flip()