Question
Re-write the solution to the simple game that was demonstrated below and extend it so that there's now a random number of food items scattered
Re-write the solution to the simple game that was demonstrated below and extend it so that there's now a random number of "food" items scattered on the "stage". The game finishes when the player/bug eats all of the food by going to each item. As before, the score is based on the number of steps taken to finish the food.
Instead of just placing the one piece of food, need to add a random amount of food, and the game ends when the bug has eaten all the pieces of food
import random
def bugFoodGame(): gridW=40 gridH=30 cellSize=10 canvasW=gridW*cellSize canvasH=gridH*cellSize stage=makeEmptyPicture(canvasW,canvasH) repaint (stage) antX=random.randint(0,gridW-1)*cellSize antY=random.randint(0,gridH-1)*cellSize addRectFilled (stage,antX,antY, cellSize, cellSize, black) repaint (stage) goalX=random.randint(0,gridW-1)*cellSize goalY=random.randint(0,gridH-1)*cellSize addRectFilled (stage,goalX,goalY, cellSize, cellSize, red) repaint (stage) numFood=1 stepsTaken=0 dir="" while (dir!="x" and numFood>0): dir=requestString("A: Left D: Right W: Up S: Down X: Exit") if (dir=="d"): addRectFilled (stage,antX,antY, cellSize, cellSize, white) antX=antX+cellSize stepsTaken=stepsTaken+1 if (dir=="a"): addRectFilled (stage,antX,antY, cellSize, cellSize, white) antX=antX-cellSize stepsTaken=stepsTaken+1 if (dir=="w"): addRectFilled (stage,antX,antY, cellSize, cellSize, white) antY=antY-cellSize stepsTaken=stepsTaken+1 if (dir=="s"): addRectFilled (stage,antX,antY, cellSize, cellSize, white) antY=antY+cellSize stepsTaken=stepsTaken+1 if (antX==goalX and antY==goalY): showInformation ("Yum!") numFood=numFood-1 addRectFilled (stage,antX,antY, cellSize, cellSize, black) repaint (stage) if numFood==0: showInformation ("You Won in " +str(stepsTaken)+ "Moves!") else: showInformation ("Quitter!")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started