[SOLVED] Help game dev 2 final project python

Hi !

I’d need help, I’m on the final project dev 2, using Python

here is my code:
game.spawnMaze(“forest”, 1)

Create your own game!

hero = game.spawnPlayerXY(“champion”, 38, 10)
hero.maxHealth = 450
hero.maxSpeed = 13
hero.attackDamage = 30

game.addSurviveGoal()
game.addCollectGoal()
game.addDefeatGoal(10)

Spawn objects into the game with spawnXY()

generator = game.spawnXY(“generator”, 60, 60)
generator.spawnType = “munchkin”
generator.spawnDelay = 4
game.spawnXY(“ogre”, 14, 16)
game.spawnXY(“ogre”, 27, 56)
game.spawnXY(“potion-medium”, 57, 44)
game.spawnXY(“potion-medium”, 37, 13)

def onSpawn(event):
unit = event.target
while true:
unit.findTheNearestEnemy()
if enemy:
unit.attack(enemy)

game.setActionFor(“munchkin”, “spawn”, onSpawn)
game.setActionFor(“ogre”, “spawn”, onSpawn)
bossGoal = game.addManualGoal(“Defeat the boss”)

munchkinsDefeated = 0
ogreDefeated = 0
ui.track(game, defeated)

def onDefeat(event):
unit = event.target
if unit.type == “munchkin”:
munchkinsDefeated = +1
if unit.type == “ogre”:
game.setGoalState(bossGoal, True)

game.setActionFor(“ogre”, “spawn”, onDefeat)
game.setActionFor(“munchkin”, “spawn”, ondefeat)

Except the maze and the hero, nothing appear on the game !
My code may be wrong, or is it a bug ?

Thanks!

Welcome to the forum @Lourimax ! :partying_face: This is a friendly place where you can ask help on levels, report bugs, or just chat with other coders! Don’t forget to read the guidelines if you haven’t yet. Have a great time!

Please for the next time, don’t forget to format your code using this button:

As for now, i have done it for you

game.spawnMaze(“forest”, 1)

Create your own game!
hero = game.spawnPlayerXY(“champion”, 38, 10)
hero.maxHealth = 450
hero.maxSpeed = 13
hero.attackDamage = 30

game.addSurviveGoal()
game.addCollectGoal()
game.addDefeatGoal(10)

Spawn objects into the game with spawnXY()
generator = game.spawnXY(“generator”, 60, 60)
generator.spawnType = “munchkin”
generator.spawnDelay = 4
game.spawnXY(“ogre”, 14, 16)
game.spawnXY(“ogre”, 27, 56)
game.spawnXY(“potion-medium”, 57, 44)
game.spawnXY(“potion-medium”, 37, 13)

def onSpawn(event):
     unit = event.target

while true:
     unit.findTheNearestEnemy()
     if enemy:
     unit.attack(enemy)

game.setActionFor(“munchkin”, “spawn”, onSpawn)
game.setActionFor(“ogre”, “spawn”, onSpawn)
bossGoal = game.addManualGoal(“Defeat the boss”)

munchkinsDefeated = 0
ogreDefeated = 0
ui.track(game, defeated)

def onDefeat(event):
     unit = event.target
     if unit.type == “munchkin”:
     munchkinsDefeated = +1
     if unit.type == “ogre”:
     game.setGoalState(bossGoal, True)

game.setActionFor(“ogre”, “spawn”, onDefeat)
game.setActionFor(“munchkin”, “spawn”, ondefeat)

Thanks a lot
Do you have any idea about why it doesn’t work ?

I’ve tried again, by writting everything, when I wrote, everything appear on the screen, and as soon as i start the game, all the elements that i’ve created simply deasappered…

Now it says : “unknown unary operator: UAdd”

Can you please post your new code?

Hi Aya,

thanks a lot, it was finally working with my new code !
But I have another pb with the level “accounts department”
CodeCombat - Coding games to learn Python and JavaScript?

here is my code:

# Comptez les valeurs des objets collectés et utilisez-les pour le score.

# Définition des personnages.
player = game.spawnPlayerXY("captain", 40, 34)
player.maxSpeed = 20

# Les coffres de gemmes sont les articles les plus précieux.
game.spawnXY("chest", 68, 56)
game.spawnXY("chest", 14, 14)

# Cette fonction engendre un objet aléatoire dans un endroit aléatoire.
def spawnRandomItem():
    itemNumber = game.randomInteger(1, 3)
    x = game.randomInteger(12, 68)
    y = game.randomInteger(12, 56)
    if itemNumber == 1:
        game.spawnXY("bronze-coin", x, y)
    elif itemNumber == 2:
        game.spawnXY("gold-coin", x, y)
    elif itemNumber == 3:
        game.spawnXY("gem", x, y)

itemInterval = 1
itemSpawnTime = 0

def checkSpawnTimer():
    if game.time >= itemSpawnTime:
        # Appelez la fonction spawnRandomItem
        spawnRandomItem()
        itemSpawnTime += itemInterval

game.score = 0

# L'événement "collect" est déclenché en collectant quelque chose.
def onCollect(event):
    # event.target contient le collecteur.
    collector = event.target
    # event.other contient l'élément collecté.
    item = event.other
    if item.value:
        # Ajouter de la puissance vitale pour des items de valeur.
        collector.health += item.value
        # Augmentez le score du jeu par la `value` (valeur) de l'objet:
        collector.score += item.value
        pass

# Attribuez le gestionnaire onCollect pour "player" sur l'événement "collect".
player.on("collect", onCollect)

# Le score dont le joueur a besoin pour gagner.
requiredScore = 220
goldGoal = game.addManualGoal('Collect at least 200 gold in 20 seconds')
ui.track(game, "score")

def checkGoals():
    if game.score >= requiredScore:
        game.setGoalState(goldGoal, True)

while True:
    checkSpawnTimer()
    checkGoals()

When I start playing and collect coin, it doesn’t report on the counting collect …

here is the problem, you have previously defined score, but as game.score, so it should be
game.score += item.value

1 Like

Thanks you SO MUCH Aya, fort helping me and for the quick answer !!

1 Like

You’re welcome, anytime :grin:

If you solved the problem, make sure to mark the post that helped you most as a solution. :slight_smile:

Done!
Thanks again !

1 Like

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