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"))
#------------------------------------------------------------------------------
# Frog
#------------------------------------------------------------------------------
# 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)
#------------------------------------------------------------------------------
# NoisyFrog
#------------------------------------------------------------------------------
# A noisy Frog is a kind of frog.
class NoisyFrog(Frog):
# How to create and initialize a noisy frog.
# Basically, it is the same as how to create and initialize any turtle.
def __init__(self, world, x, y):
Turtle.__init__(self, x, y, world)
# A noisy 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.
# Unlike a frog, a noisy frog croaks twice when jumping
def jump(self, dist):
self.penUp()
self.croak()
self.forward(dist)
self.croak()
self.penDown()
# We don't need to specify code for NoisyFrog.croak(), because
# we have already written Frog.croak(), and NoisyFrogs *are* Frogs
#------------------------------------------------------------------------------
# The pond simulation
#------------------------------------------------------------------------------
# A simulation of a noisy frog pond.
import random
import time
def simulate(numFrogs, numSteps):
# Create the pond and populate the middle of it with numFrogs noisy 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 = NoisyFrog(pond, posX, posY)
frog.turn(random.choice(range(360)))
# Simulate the life of the pond. Half second delay between steps.
# Pick a random toad, have it change direction and either run or jump
# a random distance. (Running and jumping are equally likely).
frogs = getTurtleList(pond)
for stepNum in range(numSteps):
currFrog = random.choice(frogs)
currFrog.turn(random.choice(range(360)))
if random.choice(range(2)) % 4 == 0:
currFrog.jump(random.choice(range(50, 100)))
else:
currFrog.forward(random.choice(range(50, 100)))
time.sleep(0.25)
Links to this Page