The problem is my hero moves to late after the water is thrown and the ogre gets it and kills me there are other times where poison is thrown and the ogre drinks it but then water is thrown and poison a few seconds later and my hero drinks the water with the poison and then dies
# Race munchkins to the water distilled by Omarn Brewstone!
# The continue statement is powerful for managing complicated logic.
# When the program uses the continue statement, the rest of the loop is skipped.
# However, unlike with "break", the loop repeats instead of stopping.
# Use "continue" to verify the conditions of the ambush.
myX = self.pos.x
myY = self.pos.y
# If there is no enemy, continue out of the loop.
loop:
enemy = self.findNearest(self.findEnemies())
item = self.findNearest(self.findItems())
if not enemy:
continue
self.say("I see an enemy!")
# If there is an enemy, but no item, ask for a potion and continue out of the loop.
if not item:
self.say("Give me a drink!")
continue
# Use an if-statement to check the item's type. If the type is "poison", continue out of the loop.
if item.type is "poison":
continue
# If it is not, the potion must be a bottle of water, so walk to it and return to the starting position!
self.moveXY(item.pos.x, item.pos.y)
self.moveXY(myX, myY)
In every loop iteration your hero is saying something when he sees an enemy. The say function will last around one second, time you can better spend by doing something else, like grabbing the healthy water.
Your hero must shut the [redacted, no swearing is allowed] up when he sees an enemy. Thats all.
You know tv series where the main villian is talking and talking while the good guys do important stuff, like finishing him off? Yeah thats your hero currently. While he says “I see an enemy!” the mentioned enemy will sneak past your guard and grab the water.
Just keep it simple:
Call for a drink if you must.
If there is a healthy drink which is no poison: Grab the drink and walk back to your start position. Dont be talky.
# Race munchkins to the water distilled by Omarn Brewstone!
# The continue statement is powerful for managing complicated logic.
# When the program uses the continue statement, the rest of the loop is skipped.
# However, unlike with "break", the loop repeats instead of stopping.
# Use "continue" to verify the conditions of the ambush.
myX = self.pos.x
myY = self.pos.y
# If there is no enemy, continue out of the loop.
loop:
enemy = self.findNearest(self.findEnemies())
item = self.findNearest(self.findItems())
if not enemy:
continue
#self.say("hey, there's an enemy!")
# If there is an enemy, but no item, ask for a potion and continue out of the loop.
self.say("Give me a potion!")
continue
if not item:
continue
# Use an if-statement to check the item's type. If the type is "poison", continue out of the loop.
if item.type is "water":
self.moveXY(item.pos.x, item.pos.y)
self.moveXY(myX, myY)
self.say("Ahhhhhhh, water")
# If it is not, the potion must be a bottle of water, so walk to it and return to the starting position!
else:
continue
Could you post the code correctly formatted? I am not a python coder but I believe the intendation is important and the currently posted code reveals problems with that.
Please remove the prompt to say something when seeing an enemy. It was very frustrating to my 10 year old novice programmer, and took me looking at it to realize the timing disaster. Simply commenting out his self.say(“Oooo, there’s one!”) completely fixed it.
Now, I have the harder job of explaining why commenting it out fixed the timing issues without losing him in the weeds.
I agree that the initial code should not contain a call to the say() method. The initial code is never supposed to lead to a non-obvious lose condition.