我已经使用PyGame模块在Python中创建了Space Invaders克隆,但是遇到一些困难,导致它们在到达游戏屏幕边缘时无法一起向下移动。

当外星人到达游戏屏幕的边缘时,他们同时改变方向并降低水平时,我该怎么做?

import pygame
import random

class spaceInvader(pygame.sprite.Sprite):

    def __init__(self):
        self.image = pygame.image.load("spaceInvader.png")
        self.x = 200
        self.y = 390

        self.shots = []

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_RIGHT]:
            self.x+=dist
        elif key[pygame.K_LEFT]:
            self.x-=dist

    def draw(self, surface):
        surface.blit(self.image,(self.x,self.y))
        for s in self.shots:
            s.draw(screen)

class Alien(pygame.sprite.Sprite):

    def __init__(self,x,y,direction,alienType):
        pygame.sprite.Sprite.__init__(self)

        self.AlienType = alienType
        self.Direction = direction

        if alienType == 1:
            alienImage = pygame.image.load("alien1.png")
            self.Speed = 1
            self.Score = 5

        if alienType == 2:
            alienImage = pygame.image.load("alien2.png")
            self.Score = 15
            self.Speed = 1

        if alienType == 3:
            alienImage = pygame.image.load("alien3.png")
            self.Score = 10
            self.Speed = 1

        if alienType == 4:
            alienImage = pygame.image.load("alien4.png")
            self.Score = 20
            self.Speed = 1

        if alienType == 5:
            alienImage = pygame.image.load("alien5.png")
            self.Score = 25
            self.Speed = 1

        self.image = pygame.Surface([26, 50])
        self.image.set_colorkey(black)
        self.image.blit(alienImage,(0,0))
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def moveAliens(self):
        if self.Direction == "right":
            self.rect.x += self.Speed
        if self.Direction == "left":
            self.rect.x -= self.Speed

pygame.init()

pygame.mouse.set_visible(False)
screen = pygame.display.set_mode([400,400])

allAliens = pygame.sprite.Group()
spaceInvader = spaceInvader()

pygame.display.set_caption("Space Attack")
background_image = pygame.image.load("Galaxy.png").convert()
pygame.mouse.set_visible(True)
done = False
clock = pygame.time.Clock()
black =( 0, 0, 0)
white =( 255,255,255)
red = (255, 0, 0)
score = 0
enemies = []

#For X coords
spawnPositions = [50,100,150,200,250,300,350]

yCoord = 10

for n in range(5):
    for i in range(len(spawnPositions)):
        xCoord = spawnPositions[i]
        alienType = random.randint(1,5)
        alien = Alien(xCoord, yCoord,"right", alienType)
        allAliens.add(alien)
    yCoord = yCoord + 15

loop = 0
while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    for alien in (allAliens.sprites()):
        if alien.rect.x < 0:
            alien.rect.y = alien.rect.y + 15
            alien.Direction = "right"
        if alien.rect.x > 395:
            alien.rect.y = alien.rect.y + 15
            alien.Direction = "left"
        loop =+1

    for alien in (allAliens.sprites()):
        alien.moveAliens()

    spaceInvader.handle_keys()
    screen.blit(background_image,[0,0])
    spaceInvader.draw(screen)
    allAliens.draw(screen)
    pygame.display.flip()
    clock.tick(20)

pygame.quit()


谢谢。

最佳答案

您的问题出在这里:

for alien in (allAliens.sprites()):
    if alien.rect.x < 0:
        alien.rect.y = alien.rect.y + 15
        alien.Direction = "right"
    if alien.rect.x > 395:
        alien.rect.y = alien.rect.y + 15
        alien.Direction = "left"
    loop =+1


我认为外星人目前正在个别降落?

您需要更改此设置,以便当一个外星人触发这些if语句时,所有外星人的ydirection都已适当设置,而不仅仅是碰到那一个。

10-04 14:56