New here. I’m trying to play Usual Day and for some reason, towards the end of the level, a mushroom appears and an enemy appears right in line with the mushroom. The hero goes towards the enemy, getting the mushroom on the way and dies.
Defeat munchkins, collect coins. Everything as usual.
Use AND to check existence and type in one statement.
while True:
enemy = hero.findNearestEnemy()
# With AND, the type is only checked if enemy exists.
if enemy and enemy.type == “munchkin”:
hero.attack(enemy);
# Find the nearest item.
item = hero.findNearestItem()
# Collect item if it exists and its type is “coin”.
if item and item.type == “coin”:
hero.moveXY (item.pos.x, item.pos.y);
while True:
enemy = hero.findNearestEnemy()
# With AND, the type is only checked if enemy exists.
if enemy and enemy.type == "munchkin":
hero.attack(enemy);
# Find the nearest item.
item = hero.findNearestItem()
# Collect item if it exists and its type is "coin".
if item and item.type == "coin":
hero.moveXY (item.pos.x, item.pos.y);
No that is only 7 lines, so you should be able to complete the level. Also, make sure you format the code by wrapping it with three ticks. The result is easier to read.
while True:
enemy = hero.findNearestEnemy()
# With AND, the type is only checked if enemy exists.
if enemy and enemy.type == "munchkin":
hero.attack(enemy);
# Find the nearest item.
item = hero.findNearestItem()
# Collect item if it exists and its type is "coin".
if item and item.type == "coin":
hero.moveXY (item.pos.x, item.pos.y);
That’s better. It wants you to do it with under eight lines of code and that’s the seven lines. I’ll try with a different hero and see what happens.
Sure, try it with another hero. I tried that exact same code and it worked for me. I counted, only 7 lines, so it should fall into the under 8 category.
Well. it works now. I logged off, logged back in and played it. I guess it’s just the way the objects were being spawned on that first round. It just so happened a poisonous mushroom was in the way of getting to an enemy. Thanks!
while True:
enemy = hero.findNearestEnemy()
pass
# With AND, the type is only checked if enemy exists.
if enemy and enemy.type == "munchkin":
hero.attack(enemy);
pass
# Find the nearest item.
item = hero.findNearestItem()
# Collect item if it exists and its type is "coin".
if item and item.type == "coin":
hero.moveXY (item.pos.x, item.pos.y);
pass
my code isnt working as i would like it is just finding the enemy and not attacking it, nor moving at all
Hello! I think your code isn’t working, because it isn’t in While True.
Everything (item, if’s moveXY) should be in the While True loop. Not only enemy.