我正在尝试使用精灵制作基本的射击游戏。尝试运行时,我的for循环中的一行Alien.rect.x出现错误。它说我的对象Alien没有rect属性。我以为Alien类中的self.rect = self.image.get_rect()可以解决这个问题?我究竟做错了什么?我需要精灵列表吗?

#Created by Mark Schaeffler 4/17/18
import pygame
import random
import sys

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BACKGROUNDCOLOR = (0, 0, 0)
FPS = 40
PLAYERMOVERATE = 5

# set up pygame, the window, and the mouse cursor
pygame.init()
clock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Alien Invasion')
pygame.mouse.set_visible(False)

# set up fonts
font = pygame.font.Font(None, 48)

# set up sounds
game_over_sound = pygame.mixer.Sound("gameover.wav")
background_music = pygame.mixer.Sound("Background_Music.ogg")
click_sound = pygame.mixer.Sound("laser5.ogg")

#Terminate
def terminate():
    pygame.quit()
    sys.exit()

#Class for player
class Player(pygame.sprite.Sprite):
    def __init__(self):

        # Call the parent class (Sprite) constructor
        super().__init__()

        #Load player image
        self.image = pygame.image.load("Player1.png").convert()
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()

    def update(self):
        # Update the player's position
        # Get the current mouse position.
        pos = pygame.mouse.get_pos()

        # Set the player x, y position
        self.rect.x = pos[0]
        self.rect.y = pos[0]

#Class for bullets
class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        # Call the parent class (Sprite) constructor
        super().__init__()

        self.image = pygame.Surface([4, 10])
        self.image.fill(WHITE)

        self.rect = self.image.get_rect()

    def update(self):
        # Move the bullet
        self.rect.y -= 3

#Class for aliens
class Aliens(pygame.sprite.Sprite):

    def __init__(self):
        super().__init__()

        self.image = pygame.image.load("Aliensprite1")
        self.rect = self.image.get_rect()


    def reset_pos(self):
        self.rect.y = random.randrange(-300, -20)
        self.rect.x = random.randrange(0, WINDOWWIDTH)

    def update(self):
    # Move alien down one pixel
        self.rect.y += 1
        # If alien is too far down, reset to top of screen.
        if self.rect.y > 610:
            self.reset_pos()


def waitForPlayerToPressKey():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                terminate()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE: # pressing escape
                quits
                    terminate()
                return

# List of every sprite. All aliens, bullets, player
all_sprites_list = pygame.sprite.Group()

# List of each alien in the game
alien_list = pygame.sprite.Group()

# List of each bullet
bullet_list = pygame.sprite.Group()

for i in range(50):
    # This represents an alien
    alien = Aliens

    # Set random location for the aliens
    alien.rect.x = random.randrange(WINDOWWIDTH)

    # Add aliens to the list of objects
    alien_list.add(alien)
    all_sprites_list.add(alien)

def drawText(text, font, surface, x, y):
    textobj = font.render(text, 1, TEXTCOLOR)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

# "Start" screen
drawText('Alien Invasion', font, windowSurface, (WINDOWWIDTH / 3),
(WINDOWHEIGHT / 3))
drawText('Press any key to start.', font, windowSurface, (WINDOWWIDTH
/ 3) - 30, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()


# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

score = 0
topScore = 0
player.rect.y = 370
background_music.play()

# -------- Main Program Loop -----------

最佳答案

alien = Aliens替换为alien = Aliens() :)

关于python - 外星人类别没有rect属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50006537/

10-09 20:01