Python';组对象';没有属性';更新_good';-第十四章精灵移动
我在学校的作业上遇到了麻烦,我的老师对编码了解不够,无法帮助我 此处是作业的链接: 我进入了作业的第6步,它希望我移动绿色块,但我似乎不知道如何使更新方法工作(我在代码中称之为update_good)。 当我尝试运行代码时,它出现了一个错误AttributeError:“Group”对象没有属性“update\u good” 这是我的密码Python';组对象';没有属性';更新_good';-第十四章精灵移动,python,function,class,inheritance,pygame,Python,Function,Class,Inheritance,Pygame,我在学校的作业上遇到了麻烦,我的老师对编码了解不够,无法帮助我 此处是作业的链接: 我进入了作业的第6步,它希望我移动绿色块,但我似乎不知道如何使更新方法工作(我在代码中称之为update_good)。 当我尝试运行代码时,它出现了一个错误AttributeError:“Group”对象没有属性“update\u good” 这是我的密码 import pygame import random import block_library import good_block_library A_
import pygame
import random
import block_library
import good_block_library
A_Block = block_library.Block
A_Good_Block = good_block_library.good_block
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Player(pygame.sprite.Sprite):
""" The class is the player-controlled sprite. """
# -- Methods
def __init__(self, x, y):
"""Constructor function"""
# Call the parent's constructor
super().__init__()
# Set height, width
self.image = pygame.Surface([15, 15])
self.image.fill(BLUE)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# -- Attributes
# Set speed vector
self.change_x = 0
self.change_y = 0
def changespeed(self, x, y):
""" Change the speed of the player"""
self.change_x += x
self.change_y += y
def update(self):
""" Find a new position for the player"""
self.rect.x += self.change_x
self.rect.y += self.change_y
# Initialize Pygame
pygame.init()
# Set the height and width of the screen
screen_width = 700
screen_height = 400
screen = pygame.display.set_mode([screen_width, screen_height])
bump_sound = pygame.mixer.Sound("bump.wav")
good_sound = pygame.mixer.Sound("good_block.wav")
bad_sound = pygame.mixer.Sound("bad_block.wav")
# This is a list of 'sprites.' Each block in the program is
# added to this list. The list is managed by a class called 'Group.'
good_block_list = pygame.sprite.Group()
bad_block_list = pygame.sprite.Group()
# This is a list of every sprite.
# All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
for i in range(50):
# This represents a block
A_Good_Block = A_Block(GREEN, 20, 15)
# Set a random location for the block
A_Good_Block.rect.x = random.randrange(screen_width)
A_Good_Block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
good_block_list.add(A_Good_Block)
all_sprites_list.add(A_Good_Block)
for i in range(50):
# This represents a block
bad_block = A_Block(RED, 20, 15)
# Set a random location for the block
bad_block.rect.x = random.randrange(screen_width)
bad_block.rect.y = random.randrange(screen_height)
# Add the block to the list of objects
bad_block_list.add(bad_block)
all_sprites_list.add(bad_block)
# Create a player block
player = Player(10, 15)
all_sprites_list.add(player)
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
score = 0
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 3)
# Reset speed when key goes up
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-3, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 3)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -3)
# Clear the screen
screen.fill(WHITE)
#!!!! this is where the problem occurs v
good_block_list.update_good()
all_sprites_list.update()
# See if the player block has collided with anything.
blocks_hit_list = pygame.sprite.spritecollide(player, good_block_list, True)
blocks_hit_list2 = pygame.sprite.spritecollide(player, bad_block_list, True)
# Check the list of collisions.
for block in blocks_hit_list:
score += 1
good_sound.play()
for block in blocks_hit_list2:
score-= 1
bad_sound.play()
font = pygame.font.SysFont('Calibri', 25, True, False)
text = font.render("Score: " + str(score), True, BLACK)
screen.blit(text, [30, 350])
# Draw all the spites
all_sprites_list.draw(screen)
if player.rect.y == 0 or player.rect.y == 399:
bump_sound.play()
if player.rect.x == 1 or player.rect.x == 700:
bump_sound.play()
# update the screen
pygame.display.flip()
# Limit to 60 frames per second
clock.tick(60)
pygame.quit()
下面是我导入的程序,它包含good_block类和update_good函数。我想问题可能在这里
def main():
pass
if __name__ == '__main__':
main()
import pygame
import block_library
import random
GREEN = (0, 255, 0)
screen_width = 700
screen_height = 400
A_Block = block_library.Block
change_x = random.randint(-3,3)
change_y = random.randint(-3,3)
good_block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()
class good_block(A_Block):
def changespeed(self, x, y):
""" Change the speed of the player"""
self.change_x += x
self.change_y += y
def update_good(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
下面是库中继承了一个好块的类
import pygame
import random
class Block(pygame.sprite.Sprite):
"""
This class represents the ball.
It derives from the "Sprite" class in Pygame.
"""
def __init__(self, color, width, height):
""" Constructor. Pass in the color of the block,
and its x and y position. """
# Call the parent class (Sprite) constructor
super().__init__()
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
self.change_x = random.randint(-3,3)
self.change_y = random.randint(-3,3)
请帮助问题在于您没有完全理解您的代码在做什么 代码
all\u sprites\u list.update()
之所以有效,是因为该方法实际上是为该对象定义的。您有一个pygame组
对象,它有自己的方法,您可以在此处阅读:
你会发现有update()
方法,但没有任何update\u good()
方法。文档说update()
方法调用update()
方法关闭其中包含的每个精灵
所以看起来很简单,只要将update\u good()
更改为update()
,然后调用组的update()
方法,它就会工作