gpt4 book ai didi

python - Alien 类没有 rect 属性

转载 作者:行者123 更新时间:2023-11-30 22:16:15 26 4
gpt4 key购买 nike

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

#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 -----------

最佳答案

您需要创建 Aliens 类的实例,您缺少括号:

alien = Aliens()

这会调用类构造函数并创建 Aliens 的新对象。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com