Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Re-write the solution to the simple game that was demonstrated below and extend

ID: 3875588 • Letter: R

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 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!")

Explanation / Answer

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)


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: # when numFood became zero then program will exit

exit()