私は学校の課題に悩まされています。Python 'group object'に属性 'update_good'がありません - 第14章スプライトが動く
割り当てへのリンクはこちらです: http://programarcadegames.com/index.php?lang=en&chapter=lab_sprite_moving
私はそれは私が緑のブロックを移動したいと考え割り当てにステップ6になったが、私はupdateメソッドを動作させる方法を見つけ出すように見えるカント(私は自分のコードでupdate_goodと呼んだ)。 私はエラーはAttributeErrorと、それは角膜アップコードを実行しようとすると:「グループ」オブジェクトはここで何の属性「update_good」
を持っていない私のコードの下に
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
あるライブラリはA_good_blockが