Difference between revisions of "CSC111 Homework 11 solution program"
(→Source) |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 12:22, 28 April 2010 (UTC) | --[[User:Thiebaut|D. Thiebaut]] 12:22, 28 April 2010 (UTC) | ||
---- | ---- | ||
− | + | <onlydft> | |
==Zip file== | ==Zip file== | ||
The program and all the gif files associated with it can be found in this [[Media:CSC111_hw11_solution.zip | zip file]]. | The program and all the gif files associated with it can be found in this [[Media:CSC111_hw11_solution.zip | zip file]]. | ||
Line 29: | Line 29: | ||
import time | import time | ||
+ | # ------------------------------------------------------------------------ | ||
+ | # CONSTANTS | ||
+ | # ------------------------------------------------------------------------ | ||
# size of the window: | # size of the window: | ||
H = 481 | H = 481 | ||
W = 605 | W = 605 | ||
+ | n = 7 # number of fish: | ||
+ | fRad = 45 # half width of the fish: | ||
+ | |||
+ | # ------------------------------------------------------------------------ | ||
+ | # GLOBAL VARIABLE | ||
+ | # ------------------------------------------------------------------------ | ||
bubbles = [] | bubbles = [] | ||
− | |||
− | |||
− | |||
− | |||
# ------------------------------------------------------------------------ | # ------------------------------------------------------------------------ | ||
− | # | + | # CLASSES |
# ------------------------------------------------------------------------ | # ------------------------------------------------------------------------ | ||
− | # | + | # --BUBBLE-- |
class bubble: | class bubble: | ||
"""A class defining a bubble""" | """A class defining a bubble""" | ||
Line 127: | Line 132: | ||
def swim(self): | def swim(self): | ||
"""moves the fish horizontally and randomly up and down""" | """moves the fish horizontally and randomly up and down""" | ||
− | ddy = [-3, -2, -1 | + | ddy = [-3, -2, -1, 1, 2, 3] + 15*[0] |
index = randrange(len(ddy)) | index = randrange(len(ddy)) | ||
if self.d: | if self.d: | ||
Line 135: | Line 140: | ||
− | # | + | # ------------------------------------------------------------------------ |
# FUNCTIONS | # FUNCTIONS | ||
− | # | + | # ------------------------------------------------------------------------ |
− | |||
− | |||
def waitForClick( win, message ): | def waitForClick( win, message ): | ||
""" waitForClick: stops the GUI and displays a message. | """ waitForClick: stops the GUI and displays a message. | ||
Line 150: | Line 153: | ||
startMsg.undraw() # erase | startMsg.undraw() # erase | ||
+ | # ------------------------------------------------------------------------ | ||
# randomly generates a bubble at a specified point and | # randomly generates a bubble at a specified point and | ||
# appends it to the list of bubbles | # appends it to the list of bubbles | ||
Line 158: | Line 162: | ||
b.draw(win) | b.draw(win) | ||
+ | # ------------------------------------------------------------------------ | ||
# checks if the object with passed coordinates and radius is inside | # checks if the object with passed coordinates and radius is inside | ||
# the window | # the window | ||
Line 166: | Line 171: | ||
return Outside | return Outside | ||
+ | # ------------------------------------------------------------------------ | ||
# --MAIN FUNCTION-- | # --MAIN FUNCTION-- | ||
# runs the simulation | # runs the simulation | ||
+ | # ------------------------------------------------------------------------ | ||
def main(): | def main(): | ||
global H, W | global H, W | ||
Line 225: | Line 232: | ||
</source> | </source> | ||
− | + | </onlydft> | |
<br /> | <br /> | ||
<br /> | <br /> |