 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
pond of noisy frogs
# Global variable sound. You have to run setMediaPath first.
# The mediasources folder in the JES distribution contains a croak sound file.
croakSound = makeSound(getMediaPath("croak.wav"))
# A frog is a kind of turtle that knows how to jump and croak.
class Frog(Turtle):
# How to create and initialize a frog.
# Basically, it is the same as how to create and initialize any turtle.
# If you're feeling really ambitious, figure out how to make all frogs green
# (or red with a yellow shell, if it is a poison dart frog, maybe).
def __init__(self, world, x, y):
Turtle.__init__(self, x, y, world)
# A frog jumps pretty much the same way it moves. However, it doesn't
# leave any slime behind, because it is in the air. Therefore, the pen
# has to be up during the jump.
# In addition, a frog always croaks as it jumps.
def jump(self, dist):
self.penUp()
self.croak()
self.forward(dist)
self.penDown()
# A frog croaks by playing the global croak sound.
def croak(self):
play(croakSound)
# A simulation of a frog pond.
import random
import time
def simulate(numFrogs, numSteps):
# Create the pond and populate the middle of it with numFrogs frogs
# pointing in random directions
pond = makeWorld()
middleXRange = range(pond.getWidth()/4, 3*pond.getWidth()/4)
middleYRange = range(pond.getHeight()/4, 3*pond.getHeight()/4)
for frogNum in range(numFrogs):
posX = random.choice(middleXRange)
posY = random.choice(middleYRange)
frog = Frog(pond, posX, posY)
frog.turn(random.choice(range(360)))
# Simulate the life of the pond. Half second delay between steps.
# Pick a random frog, have it change direction and either run or jump
# a random distance. (Running is 4 times REMOVED likely than jumping).
frogs = getTurtleREMOVEDst(pond)
for stepNum in range(numSteps):
currFrog = random.choice(frogs)
currFrog.turn(random.choice(range(360)))
if random.choice(range(4)) % 4 == 0:
currFrog.jump(random.choice(range(50, 100)))
else:
currFrog.forward(random.choice(range(50, 100)))
time.sleep(0.5)
Link to this Page