[SOLVED] Game of Coins Step 4: Power-Ups Level Help

Hello guys, long time no see. Anyway, I need some help on this level. For some reason the hero does not kill the enemies when he gets the power-up. I think all my code is correct but I am also not sure. Here is my code:

# Step 4 of creating a Pac-Man style arcade game.

# The game layout and items. Scroll down.
game.spawnXY("forest", 16, 16)
game.spawnXY("forest", 32, 16)
game.spawnXY("forest", 48, 16)
game.spawnXY("forest", 64, 16)
game.spawnXY("forest", 16, 32)
game.spawnXY("forest", 32, 32)
game.spawnXY("forest", 48, 32)
game.spawnXY("forest", 64, 32)
game.spawnXY("forest", 16, 48)
game.spawnXY("forest", 32, 48)
game.spawnXY("forest", 48, 48)
game.spawnXY("forest", 64, 48)

game.spawnXY("bronze-coin", 16, 8)
game.spawnXY("bronze-coin", 24, 8)
game.spawnXY("bronze-coin", 32, 8)
game.spawnXY("bronze-coin", 48, 8)
game.spawnXY("bronze-coin", 56, 8)
game.spawnXY("bronze-coin", 64, 8)
game.spawnXY("bronze-coin", 72, 8)

game.spawnXY("bronze-coin", 8, 16)
game.spawnXY("bronze-coin", 24, 16)
game.spawnXY("bronze-coin", 40, 16)
game.spawnXY("bronze-coin", 56, 16)
game.spawnXY("bronze-coin", 72, 16)

game.spawnXY("bronze-coin", 8, 24)
game.spawnXY("bronze-coin", 16, 24)
game.spawnXY("bronze-coin", 24, 24)
game.spawnXY("bronze-coin", 32, 24)
game.spawnXY("bronze-coin", 40, 24)
game.spawnXY("bronze-coin", 48, 24)
game.spawnXY("bronze-coin", 56, 24)
game.spawnXY("bronze-coin", 64, 24)
game.spawnXY("bronze-coin", 72, 24)

game.spawnXY("bronze-coin", 24, 32)
game.spawnXY("bronze-coin", 56, 32)

game.spawnXY("bronze-coin", 8, 40)
game.spawnXY("bronze-coin", 16, 40)
game.spawnXY("bronze-coin", 24, 40)
game.spawnXY("bronze-coin", 32, 40)
game.spawnXY("bronze-coin", 40, 40)
game.spawnXY("bronze-coin", 48, 40)
game.spawnXY("bronze-coin", 56, 40)
game.spawnXY("bronze-coin", 64, 40)
game.spawnXY("bronze-coin", 72, 40)

game.spawnXY("bronze-coin", 8, 48)
game.spawnXY("bronze-coin", 24, 48)
game.spawnXY("bronze-coin", 40, 48)
game.spawnXY("bronze-coin", 56, 48)
game.spawnXY("bronze-coin", 72, 48)

game.spawnXY("bronze-coin", 8, 56)
game.spawnXY("bronze-coin", 16, 56)
game.spawnXY("bronze-coin", 24, 56)
game.spawnXY("bronze-coin", 32, 56)
game.spawnXY("bronze-coin", 48, 56)
game.spawnXY("bronze-coin", 56, 56)
game.spawnXY("bronze-coin", 64, 56)
game.spawnXY("bronze-coin", 72, 56)

game.spawnXY("mushroom", 40, 8)
game.spawnXY("mushroom", 8, 32)
game.spawnXY("mushroom", 72, 32)
game.spawnXY("mushroom", 40, 56)

game.score = 1000
# The duration of power-ups.
game.powerDuration = 6
# The time until a power-up runs out. Used for UI.
game.powerTime = 0
# The time a power-up expires. Used internally.
game.powerEndTime = 0
ui.track(game, "time")
ui.track(game, "score")
# Adding ui for game.powerTime:
ui.track(game, "powerTime")

game.addCollectGoal()
game.addSurviveGoal();

player = game.spawnPlayerXY("knight", 8, 8)
player.maxSpeed = 30

# The function make the player big and strong.
def powerPlayerUp():
    player.scale = 2
    player.attackDamage = 100
    game.powerEndTime = game.time + game.powerDuration

# The function return the player in the normal state.
def powerPlayerDown():
    player.scale = 1
    player.attackDamage = 1

def onCollect(event):
    player = event.target
    item = event.other
    if item.type == "bronze-coin":
        game.score += 1
    if item.type == "mushroom":
        game.score += 5
        # Use powerPlayerUp to strengthen the player:
        powerPlayerUp()

def onCollide(event):
    player = event.target
    other = event.other
    # If other is a "scout" and the player's scale is 2:
    if other == "scout" and player.scale == 2:
        # Defeat the other with the defeat method:
        other.die()

player.on("collect", onCollect)
# Assign the event handler for the player's "collide" event:
player.on("collide", onCollide)

generator = game.spawnXY("generator", 41, 31)
generator.spawnType = "scout"
generator.spawnDelay = 6;

def onSpawn(event):
    unit = event.target
    unit.maxSpeed = 8
    unit.attackDamage = player.maxHealth
    while True:
        # Enemies run away from the big player.
        if player.scale == 2:
            unit.behavior = "RunsAway"
        else:
            unit.behavior = "AttacksNearest"

def onDefeat(event):
    # Increase game.score for defeated enemies:
    game.score += 10

game.setActionFor("scout", "spawn", onSpawn)
game.setActionFor("scout", "defeat", onDefeat)


def checkTimeScore():
    game.score -= 0.5
    if game.score < 0:
        game.score = 0

def checkPowerTimer():
    # Remaining power time.
    game.powerTime = game.powerEndTime - game.time
    if game.powerTime <= 0:
        game.powerTime = 0
        if player.scale == 2:
            powerPlayerDown()

# Lets combine all the time based functions.
def checkTimers():
    checkTimeScore()
    checkPowerTimer()

while True:
    checkTimers()

# Win the game.

Can you send me the link to this level?
Lydia

https://codecombat.com/play/level/game-of-coins-step-4-power-ups?

I need to go now. See you tomorrow.

1 Like

This is supposed to be

other.defeat()

Lydia

1 Like

It does not seem to hurt the scouts.

Also, the cleave only works once.

Your code looks fine, but you have to change what I said in the previous post. And then you have to play the level.
Lydia

I did, but the cleave only works once and you have to wait for it to happen again. I wonder if the cleave works the same way with cooldown as in normal levels.

Can you post your code again, and if possible, a short video on the issue?
Lydia

I have this app: image
Which allows me to send short GIFs. This is the website
Lydia

I completed the level just fine.
First, I moved to the nearest mushroom.
Second, I destroyed all the ogres spawned so far, then destroyed the generator.
Finally, I can have time to collect all of the items, without the fear of getting beaten by ogres.
Lydia

When you attack the scouts, do they die on the first hit?

If you have the power-up, yes. I just recorded a GIF and it is saving. So I will upload it as soon as it saves.
Lydia

GIF:
Codecombat
This is how I completed it and it worked.
Edit: Also, can you also remove the working code so others don’t try and cheat?
Lydia

2 Likes

AAAAAARGGGHHHHHH!! I did not know that you can kill things by clicking on them! Oh thankyou @Lydia_Song. This has helped a lot.

2 Likes

You’re welcome! Can you remove the working code?
Lydia

Removed. This case is officially closed.

Thanks!
Lydia
20 chars

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.