Pygame,产生第二枚导弹或外星人没有工作

时间:2018-10-06 20:58:03

标签: pygame spawn

我是一名大学生,我有一个游戏项目(我们可以选择我们想要的任何主题),该项目一开始很顺利,但后来遇到了一些意外问题。我选择创建一个简单版本的raiden 2(太空飞船射击游戏),但是当它降落产生导弹和外星人时,它并没有按预期工作。首先,当我按下太空按钮时,导弹将被创建并从底部移动到顶部,但是当我再次击中它时,它并不会产生第二个或可能会,但是我所能看到的只是导弹重新安置在顶部太空船的速度加上速度的增加(不是故意的),而我想在第一个移动的同时创建第二个。外星飞船也遇到了同样的问题,该实例创建并移动良好,但是当第二个生成时机到来时,第一个刚好在新坐标处重新定位并提高了速度。然后为外星人选择使用循环,然后移动它们,但速度又提高了,我只能看到一个。我还有4种需要产生的不同类别的外星人,但是如果我不能解决这些问题,我将无法继续进行。我还在这里检查了一些教程和其他问题,但是当我尝试解决方案不起作用时,我将继续搜索,但是如果有人发现错误并可以举一个例子,我将不胜感激。最后,只是告诉您我正在使用图片作为精灵等。这是代码:

外星人:

class aliens(pygame.sprite.Sprite):
def __init__(self, dw, dh, w, h):
    self.display_width = dw
    self.display_height = dh
    self.width = w
    self.height = h

    super().__init__()

第一外星人:

class alienPrivate(aliens):
def __init__(self, dw, dh, w, h):
    super().__init__(dw, dh, w, h)

    self.image = alienPrivateImg
    self.rect = privateRect
    self.rect.x = random.randrange(0, display_width - w)
    self.rect.y = random.randrange(2*(-h), -h)
    self.weapon = 1
    self.damage = 5
    self.health = 10
    self.speed = 4

# Private moves top -> bottom    
def update(self):
    self.rect.y += self.speed

导弹:

class Missiles(pygame.sprite.Sprite):
def __init__(self, dw, dh, wpn, pwlvl):
    super().__init__()

    self.weapon = wpn
    self.powerLevel = pwlvl
    self.setFire()

def setFire(self):
    if self.weapon == 1:
        if self.powerLevel == 1:
            self.image = bullets
            self.rect = bullet
    elif self.weapon == 2:
        if self.powerLevel == 1:
            self.image = frostLaserL1
            self.rect = frLaserL1
        elif self.powerLevel == 2:
            self.image = frostLaserL2
            self.rect = frLaserL2
        else:
            self.image = frostLaserL3
            self.rect = frLaserL3
    else:
        if self.powerLevel == 1:
            self.image = fireLaserL1
            self.rect = fLaserL1
        elif self.powerLevel == 2:
            self.image = fireLaserL2
            self.rect = fLaserL2
        else:
            self.image = fireLaserL3
            self.rect = fLaserL3

def update(self):
    self.rect.y -= 1

导弹生成:

missile = Missiles(display_width,display_height,weapon, powerLevel)
                missile.rect.x = hero.rect.x + 35
                missile.rect.y = hero.rect.y - 20
                all_sprites_list.add(missile)
                fire_list.add(missile)

外星人生成:

for spawn in range(5):
                private = alienPrivate(display_width, display_height, 50, 88)
                all_sprites_list.add(private)
                alien_list.add(private)

播放器类:

class player(pygame.sprite.Sprite):
def __init__(self, dw, dh, w, h):
    super().__init__()

    self.health = 100
    self.stamina = 100
    self.width = w
    self.height = h
    self.image = spaceshipImgBee
    self.rect = spaceshipRect
    self.rect.x = (dw / 2) - (self.width / 2)
    self.rect.y = dh - h
    self.weapon = 1
    self.powerLevel = 1


# Methods
def setPosx(self, x):
    self.rect.x = x

def setPosy(self, y):
    self.rect.y = y

def moveRigth(self, x):
    self.rect.x += x

def moveLeft(self, y):
    self.rect.y += y

def setWeapon(self, x):
    if self.weapon == x:
        if self.weapon == 1 and self.powerLevel == 1:
            self.damage += 10
            self.powerLevel += 1
        elif self.weapon == 1 and (self.powerLevel > 1 and self.powerLevel < 3):
            self.powerLevel += 1
        elif self.weapon == 2 and self.powerlevel < 3:
            self.damage += 15
            self.powerLevel += 1
        elif self.weapon == 3 and self.powerLevel < 3:
            self.damage += 25
            self.powerLevel += 1
    else:
        if x == 1:
            self.weapon = x
            self.damage = 10
            self.powerLevel = 1
        elif x == 2:
            self.weapon = x
            self.damage = 20
            self.powerLevel = 1
        elif x == 3:
            self.weapon = x
            self.damage = 50
            self.powerLevel +=1

游戏的其​​余部分(其中一部分)包括while循环:

import pygame
import random
import time

# Colors
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)

# Start Pygame
pygame.init()

# Window Size and Name
display_width = 700
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Star Protector")

# Window refresh rate
fps = 60
clock = pygame.time.Clock()

#Images
spaceshipImgBee = pygame.image.load('Hero_Spaceship_Bee_80x79.png')
spaceshipRect = spaceshipImgBee.get_rect()

alienPrivateImg = pygame.image.load('Alien_Private_50x88.png')
privateRect = alienPrivateImg.get_rect()

bullets = pygame.image.load('Bullets_10x20.png')
bullet = bullets.get_rect()

all_sprites_list = pygame.sprite.Group()
alien_list = pygame.sprite.Group()
fire_list = pygame.sprite.Group()

hero = player(display_width,display_height, 80, 79)
all_sprites_list.add(hero)
killed = False

x = 0
y = 0
i = 0
l = 0
score = 0
while not killed:
weapon = hero.weapon
powerLevel = hero.powerLevel
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        killed = True

    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            x = -10
            i = 1
        elif event.key == pygame.K_RIGHT:
            x = 10
            i = 1
        elif event.key == pygame.K_UP:
            y = -10
            i = 2
        elif event.key == pygame.K_DOWN:
            y = 10
            i = 2
        elif event.key == pygame.K_SPACE:
            if weapon == 1:
                missile = Missiles(display_width,display_height,weapon, powerLevel)
                missile.rect.x = hero.rect.x + 35
                missile.rect.y = hero.rect.y - 20
                all_sprites_list.add(missile)
                fire_list.add(missile)
            elif weapon == 2:
                if powerLevel == 1:
                    frlaser = Missiles(display_width, display_height, weapon, powerLevel)
                    frlaser.rect.x = (hero.rect.x / 2) - 7
                    all_sprites_list.add(frlaser)
                    fire_list.add(frlaser)

                elif powerLevel == 2:
                    frlaser = Missiles(display_width, display_height, weapon, powerLevel)
                    frlaser.rect.x = (hero.rect.x / 2) - 20
                    all_sprites_list.add(frlaser)
                    fire_list.add(frlaser)

                else: 
                    frlaser = Missiles(display_width, display_height, weapon, powerLevel)
                    frlaser.rect.x = (hero.rect.x / 2) - 40
                    all_sprites_list.add(frlaser)
                    fire_list.add(frlaser)
            else:
                if powerLevel == 1:
                    flaser = Missiles(display_width, display_height, weapon, powerLevel)
                    flaser.rect.x = (hero.rect.x / 2) -7
                    all_sprites_list.add(flaser)
                    fire_list.add(flaser)
                elif powerLevel == 2:
                    flaser = Missiles(display_width, display_height, weapon, powerLevel)
                    flaser.rect.x = (hero.rect.x / 2) -20
                    all_sprites_list.add(flaser)
                    fire_list.add(flaser)
                else:
                    flaser = Missiles(display_width, display_height, weapon, powerLevel)
                    flaser.rect.x = (hero.rect.x / 2) - 40
                    all_sprites_list.add(flaser)
                    fire_list.add(flaser)

    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            x = 0
            i = 1
        elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            y = 0
            i = 2

#Move the spaceship with boundaries
if i == 1:
    hero.moveRigth(x)
elif i == 2:
    hero.moveLeft(y)

if hero.rect.x > display_width - hero.width:
    hero.setPosx(display_width - hero.width)
elif hero.rect.x < 0:
    hero.setPosx(0)
elif hero.rect.y < 0:
    hero.setPosy(0)
elif hero.rect.y > display_height - hero.height:
    hero.setPosy(display_height - hero.height)


#Background Color - Image
gameDisplay.blit(backgCosmos, (0 ,l))
#gameDisplay.fill(WHITE)

if score == 100:
    for spawn in range(5):
        private = alienPrivate(display_width, display_height, 50, 88)
        all_sprites_list.add(private)
        alien_list.add(private)

if l + 600 > 600:
    gameDisplay.blit(backgRepublic, (0 ,l -600))

l += 1

all_sprites_list.update()

#Print player - start the battle
all_sprites_list.draw(gameDisplay)

score += 1

pygame.display.update()
clock.tick(fps)

pygame.quit()
quit()

对于图像,我无法上传(不知道为什么),但是您可以随机快速调整大小并进行测试。谢谢您的时间!

2 个答案:

答案 0 :(得分:0)

您已使用不同的实例正确生成了外星人专用物,但需要对导弹执行相同的操作。 for i in range(number)将给出单独的实例。否则,pygames sprite .add()将假定您引用的是该组中已经存在的实例。

请问您可以将游戏循环代码while True:放在这里吗?

在此期间,检查将.update()传递给各个子画面是否会提高速度。最后,如果导弹从显示屏上消失了,您只需将它移除()。

答案 1 :(得分:0)

此错误的修复很有趣,因为它看起来像您正确创建了missile并将其添加到组中一样(通过打印sprite和组可以看到它),但是问题是您为导弹分配了与bullet属性相同的全局变量self.rect,这意味着所有导弹都放置在同一位置。因此,当您更新区域的坐标(missile.rect.y = hero.rect.y - 20)时,会影响所有导弹精灵,因为它们共享相同的区域。

您应该删除bullet矩形,并仅在self.rect方法中创建__init__,以便每枚导弹都具有自己的矩形(图像仍可以共享)。

此外,最好在创建实例时将xy坐标直接传递给__init__方法:

missile = Missiles(
    hero.rect.x+35, hero.rect.y-20,
    display_width,display_height,weapon, powerLevel)

class Missiles(pygame.sprite.Sprite):
    def __init__(self, x, y, dw, dh, wpn, pwlvl):
        super().__init__()
        self.weapon = wpn
        self.powerLevel = pwlvl
        self.setFire()
        # You can pass the `x, y` coordinates as the `topleft`,
        # `center` or another argument.
        self.rect = self.image.get_rect(center=(x, y))
        # Or set the coordinates afterwards.
        # self.rect.center = (x, y)

    def setFire(self):
        if self.weapon == 1:
            if self.powerLevel == 1:
                self.image = bullets
        elif self.weapon == 2:
            if self.powerLevel == 1:
                self.image = frostLaserL1
            elif self.powerLevel == 2:
                self.image = frostLaserL2
            else:
                self.image = frostLaserL3
        else:
            if self.powerLevel == 1:
                self.image = fireLaserL1
            elif self.powerLevel == 2:
                self.image = fireLaserL2
            else:
                self.image = fireLaserL3

    def update(self):
        self.rect.y -= 1

您需要对外星飞船执行相同的操作:

class AlienPrivate(aliens):  # Use uppercase names for classes.
    def __init__(self, dw, dh, w, h):
        super().__init__(dw, dh, w, h)
        self.image = alienPrivateImg
        x = random.randrange(display_width-w)
        y = random.randrange(2*(-h), -h)
        self.rect = self.image.get_rect(center=(x, y))