L.Shaelyn
L.Shaelyn

Reputation: 1

Python 'group object' has no attribute 'update_good' - chapter 14 sprite moving

I am having troubles with an assignment for school and my teacher doesn't know enough about coding to help me.

The link to the assignment is here: http://programarcadegames.com/index.php?lang=en&chapter=lab_sprite_moving

I got to step 6 on the assignment where it wants me to make the green blocks move but I cant seem to figure out how to make the update method work (I called it update_good in my code). When I try the run the code it comea up with the error AttributeError: 'Group' object has no attribute 'update_good'

Here is my code

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()

Below is the program I import that contains the good_block class and the update_good function. I think the problem might be in here

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

Below is the library has the class that A_good_block is inherited from

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)

Please help

Upvotes: 0

Views: 1189

Answers (1)

Micheal O'Dwyer
Micheal O'Dwyer

Reputation: 1261

The problem is that you don't fully understand what you're code is doing.

The reason why the code all_sprites_list.update() works is because that method is actually defined for that object. You have a pygame Group object which has it's own methods that you can read about here: https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group.

What you'll find is that the update() method is there but there isn't any update_good() method. The docs say that the update() method calls the update() method off every Sprite contained in it.

So it seems simple, just change update_good() to update() and then call the update() method of the Group and it will work.

Upvotes: 1

Related Questions