Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Cannot finish "From dust to dust"

Hi there,

I am having problems finishing this level: “From dust to dust”.
My player reaches the red x but the game does not finish… Can someone give me a hint what I am doing wrong? I pasted below the code I am running.

# Block passages with forest tiles.
# Then destroy them when the player defeats some ogres.

# Set up the player.
player = game.spawnXY("duelist", 6, 34)
player.attackDamage = 35
player.maxHealth = 750
player.maxSpeed = 15
# The player should move through the forest to win.
game.addMoveGoalXY(76, 34)

def onSpawn(event):
    duelist = event.target
    while True:
        enemy = player.findNearestEnemy()
        if enemy:
            if enemy.type != "generator":
                duelist.attack(enemy)

# Set up enemies.
munchkinSpawner = game.spawnXY("generator", 16, 56)
munchkinSpawner.spawnType = "munchkin"
munchkinSpawner.spawnDelay = 3

scoutSpawner = game.spawnXY("generator", 40, 10)
scoutSpawner.spawnType = "scout"
scoutSpawner.spawnDelay = 5

# These forest tiles should block the passages.
passageForest1 = game.spawnXY("forest", 28, 34)
# Create the second forest to block the second passage {52, 34}:
passageForest2 = game.spawnXY("forest", 52, 34)

game.defeated = 0
ui.track(game, "defeated")

def onDefeat(event):
    defeated = event.target
    game.defeated += 1
    # If 3 ogres are defeated:
    if game.defeated == 3:
        # Defeat the munchkinSpawner.
        munchkinSpawner.defeat()
        # Destroy the first forest passage.
        passageForest1.destroy()
        player.moveXY(52, 34)
    # If 6 ogres are defeated:
    if game.defeated == 6:
        # Call the defeat method for the scoutSpawner:
        scoutSpawner.defeat()
        # Destroy the second forest passage.
        passageForest2.destroy()
        player.moveXY(76, 34)

# Set the "defeat" event handler for "munchkin"s and "scout"s.
game.setActionFor("munchkin", "defeat", onDefeat)
game.setActionFor("scout", "defeat", onDefeat)
game.setActionFor("duelist", "spawn", onSpawn)


# Beat the game!

I think it has to do with you switching the hero line to a regular spawn.

player = game.spawnXY("duelist", 6, 34)

The Startup code has it as a player spawn:

player = game.spawnPlayerXY("duelist", 6, 34)

This also allows you to remove the extra lines of code for the onSpawn function and the move methods.

Hmm, seems to work! Thank you very much. If I remove the moveXY functions, the player does not move anymore. And if the onSpawn function is removed, then I guess I need a while True loop outside any methods where the player will kill enemies all the time. Otherwise not sure how I can do it.

Where can I find more information on what the methods are doing? The documentation in the game is not very helpful…

In this section of code combat, the focus is on developing the functionality of a game. Once you click the run button, you actually play the game live using your mouse clicking to move and to attack. More like developing a regular game this isn’t using coding to play it.

I haven’t been able to find a good source for documentation on CodeCombat. Although, the forum can help with quite a few of the minor details to pass the levels. There are a few of us that have played most if not all of the levels available to subscribers and have learned the mechanics of CodeCombat coding reasonably well. The Game Dev sections are probably the least supported considering it is meant to be an introduction to the main concepts to develop a game.