Trouble on level 21 game development 2 Berserker

# Mushrooms allow the player to destroy fences for a time.

player = game.spawnPlayerXY('captain', 12, 34)
player.maxSpeed = 15
game.addMoveGoalXY(76, 34)
ui.track(game, "time")
ui.track(power,"endtime")
# The duration of the mushroom power.
game.powerDuration = 3
# The time the mushroom power expires at.
game.powerEndTime = 0

# "mushroom"s are collectable items without default effects.
game.spawnXY("mushroom", 12, 52)
game.spawnXY("mushroom", 12, 16)
game.spawnXY("mushroom", 36, 16)
game.spawnXY("mushroom", 36, 52)
game.spawnXY("mushroom", 56, 12)
game.spawnXY("mushroom", 56, 56)
game.spawnXY("mushroom", 56, 34)

# The event handler for "collect" events.
def onCollect(event):
    unit = event.target
    item = event.other
    if item.type == "mushroom":
        # "scale" changes the visual size of the unit.
        unit.scale = 2
        game.powerEndTime = game.time + game.powerDuration
        unit.say("ARRRGH!!!")

# The event handler for "collide" events.
def onCollide(event):
    # The event owner who has collided with something.
    unit = event.target
    # The object the unit collided with.
    collidedObject = event.other
    # If it's a fence.
    if collidedObject.type == "fence":
        if unit.scale == 2:
            # Use the `destroy` method of collidedObject.
            
            pass

# Assign onCollide to the "collide" event on the player.
player.on("collide", onCollide)
# Assign onCollect to the "collect" event on the player.
player.on("collect", onCollect)

def checkTimers():
    # If game time is greater than game.powerEndTime:
    if game > power:
        # If player.scale is equal to 2: 
        if playerScale == 2:
            # Set the player's scale to 1.
            game.setPlayerScale(1)
    pass

while True:
    checkTimers()

Im having trouble with this level and cant seem to figure out why.

In your onCollide function, looks like you forgot something:

            # Use the `destroy` method of collidedObject.

In the checkTimers function, these are not quite right…they are incomplete:

    if game > power:

What attribute are you comparing?

# Mushrooms allow the player to destroy fences for a time.

player = game.spawnPlayerXY('captain', 12, 34)
player.maxSpeed = 15
game.addMoveGoalXY(76, 34)
ui.track(game, "time")
# The duration of the mushroom power.
game.powerDuration = 3
# The time the mushroom power expires at.
game.powerEndTime = 0

# "mushroom"s are collectable items without default effects.
game.spawnXY("mushroom", 12, 52)
game.spawnXY("mushroom", 12, 16)
game.spawnXY("mushroom", 36, 16)
game.spawnXY("mushroom", 36, 52)
game.spawnXY("mushroom", 56, 12)
game.spawnXY("mushroom", 56, 56)
game.spawnXY("mushroom", 56, 34)

# The event handler for "collect" events.
def onCollect(event):
    unit = event.target
    item = event.other
    if item.type == "mushroom":
        # "scale" changes the visual size of the unit.
        unit.scale = 2
        game.powerEndTime = game.time + game.powerDuration
        unit.say("ARRRGH!!!")

# The event handler for "collide" events.
def onCollide(event):
    # The event owner who has collided with something.
    unit = event.target
    # The object the unit collided with.
    collidedObject = event.other
    # If it's a fence.
    if collidedObject.type == "fence":
        if unit.scale == 2:
            # Use the `destroy` method of collidedObject.
            Unit.collidedObject(destroy)
            pass

# Assign onCollide to the "collide" event on the player.
player.on("collide", onCollide)
# Assign onCollect to the "collect" event on the player.
player.on("collect", onCollect)

def checkTimers():
    # If game time is greater than game.powerEndTime:
    if game > power:
        # If player.scale is equal to 2: 
        if playerScale == 2:
            # Set the player's scale to 1.
            game.setPlayerScale(1)
    pass

while True:
    checkTimers()


Im still having problems. I dont know what im doing wrong im not getting any error messages. I think this is correct. I restarted and this is where im at.

You still haven’t fixed this part. ‘game’ and ‘power’ are both variables and as such, they have attributes. Which attributes are you meaning to compare here?

This is how the level presents itself.

# Mushrooms allow the player to destroy fences for a time.

player = game.spawnPlayerXY('captain', 12, 34)
player.maxSpeed = 15
game.addMoveGoalXY(76, 34)
ui.track(game, "time")

# The duration of the mushroom power.
game.powerDuration = 3
# The time the mushroom power expires at.
game.powerEndTime = 0

# "mushroom"s are collectable items without default effects.
game.spawnXY("mushroom", 12, 52)
game.spawnXY("mushroom", 12, 16)
game.spawnXY("mushroom", 36, 16)
game.spawnXY("mushroom", 36, 52)
game.spawnXY("mushroom", 56, 12)
game.spawnXY("mushroom", 56, 56)
game.spawnXY("mushroom", 56, 34)

# The event handler for "collect" events.
def onCollect(event):
    unit = event.target
    item = event.other
    if item.type == "mushroom":
        # "scale" changes the visual size of the unit.
        unit.scale = 2
        game.powerEndTime = game.time + game.powerDuration
        unit.say("ARRRGH!!!")

# The event handler for "collide" events.
def onCollide(event):
    # The event owner who has collided with something.
    unit = event.target
    # The object the unit collided with.
    collidedObject = event.other
    # If it's a fence.
    if collidedObject.type == "fence":
        if unit.scale == 2:
            # Use the `destroy` method of collidedObject.
            
            pass

# Assign onCollide to the "collide" event on the player.
player.on("collide", onCollide)
# Assign onCollect to the "collect" event on the player.
player.on("collect", onCollect)

def checkTimers():
    # If game time is greater than game.powerEndTime:
    
        # If player.scale is equal to 2: 
        
            # Set the player's scale to 1.
            
    pass

while True:
    checkTimers()

yes, but you are looking good (as far as I’ve dug in to the code) so far. All you need is to complete that one final statement.

Think about this line: game.powerEndTime = game.time + game.powerDuration. You are defining ‘.powerEndTime’ as the point in time when the mushroom power wears off. You have also set this as a defined attribute of ‘game’.

Now, in your checkTimers function, you are intending to compare actual game time, against ‘.powerEndTime’. Kind of like:

if actual game time is greater than "the previously defined '.powerEndTime':
    then perform the following code

So now, you have two attributes you can use: ‘game.time’ and ‘game.powerEndTime’

I am also having trouble…


player = game.spawnPlayerXY('captain', 12, 34)
player.maxSpeed = 15
game.addMoveGoalXY(76, 34)
ui.track(game, "time")
ui.track(power,"endtime")
# The duration of the mushroom power.
game.powerDuration = 3
# The time the mushroom power expires at.
game.powerEndTime = 0

# "mushroom"s are collectable items without default effects.
game.spawnXY("mushroom", 12, 52)
game.spawnXY("mushroom", 12, 16)
game.spawnXY("mushroom", 36, 16)
game.spawnXY("mushroom", 36, 52)
game.spawnXY("mushroom", 56, 12)
game.spawnXY("mushroom", 56, 56)
game.spawnXY("mushroom", 56, 34)

# The event handler for "collect" events.
def onCollect(event):
    unit = event.target
    item = event.other
    if item.type == "mushroom":
        # "scale" changes the visual size of the unit.
        unit.scale = 2
        game.powerEndTime = game.time + game.powerDuration
        unit.say("ARRRGH!!!")

# The event handler for "collide" events.
def onCollide(event):
    # The event owner who has collided with something.
    unit = event.target
    # The object the unit collided with.
    collidedObject = event.other
    # If it's a fence.
    if collidedObject.type == "fence":
        if unit.scale == 2:
            # Use the `destroy` method of collidedObject.
            Unit.collidedObject(destroy)
            pass

# Assign onCollide to the "collide" event on the player.
player.on("collide", onCollide)
# Assign onCollect to the "collect" event on the player.
player.on("collect", onCollect)

def checkTimers():
    # If game time is greater than game.powerEndTime:
    if game > power:
        # If player.scale is equal to 2: 
        if playerScale == 2:
            # Set the player's scale to 1.
            game.setPlayerScale(1)
    pass

while True:
    checkTimers()

the duck is saying that “power” is not defined… what am I doing wrong?

game.powerEndTime, not power

unit, not Unit and unit.collidedObject.destroy(), not unit.collidedObject(destroy)

And I’ve noticed that most of the times that you post it’s because you didn’t follow what the comments say, so please follow what the comments say BEFORE posting

1 Like

ok. will do. thank you for all of the help.

it still not work… I not know why.

Then please post your new code

power = game.powerEndTime
player = game.spawnPlayerXY('captain', 12, 34)
player.maxSpeed = 15
game.addMoveGoalXY(76, 34)
ui.track(game, "time")
ui.track(power,"endtime")
# The duration of the mushroom power.
game.powerDuration = 3
# The time the mushroom power expires at.
game.powerEndTime = 0

# "mushroom"s are collectable items without default effects.
game.spawnXY("mushroom", 12, 52)
game.spawnXY("mushroom", 12, 16)
game.spawnXY("mushroom", 36, 16)
game.spawnXY("mushroom", 36, 52)
game.spawnXY("mushroom", 56, 12)
game.spawnXY("mushroom", 56, 56)
game.spawnXY("mushroom", 56, 34)

# The event handler for "collect" events.
def onCollect(event):
    unit = event.target
    item = event.other
    if item.type == "mushroom":
        # "scale" changes the visual size of the unit.
        unit.scale = 2
        game.powerEndTime = game.time + game.powerDuration
        unit.say("ARRRGH!!!")

# The event handler for "collide" events.
def onCollide(event):
    # The event owner who has collided with something.
    unit = event.target
    # The object the unit collided with.
    collidedObject = event.other
    # If it's a fence.
    if collidedObject.type == "fence":
        if unit.scale == 2:
            # Use the `destroy` method of collidedObject.
            unit.collidedObject.destroy()
        pass

# Assign onCollide to the "collide" event on the player.
player.on("collide", onCollide)
# Assign onCollect to the "collect" event on the player.
player.on("collect", onCollect)

def checkTimers():
    # If game time is greater than game.powerEndTime:
    if game > power:
        # If player.scale is equal to 2: 
        if playerScale == 2:
            # Set the player's scale to 1.
            game.setPlayerScale(1)
    pass

while True:
    checkTimers()