Difference between revisions of "CSC111 Homework 11 solution program"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- The program and all the gif files associated with it can be found in this zip file. <source lang="python"> # hw11a.py # 111c - …')
 
 
(5 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==
 +
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]].
+
 
 +
==Source==
  
 
<source lang="python">
 
<source lang="python">
Line 8: Line 12:
 
# 111c - aq
 
# 111c - aq
 
# Kristina Fedorenko
 
# Kristina Fedorenko
 +
# (Edited by D. Thiebaut)
 
#
 
#
 
# This program is a simulation of swimming fish. Each fish is an object  
 
# This program is a simulation of swimming fish. Each fish is an object  
Line 24: 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 = []
  
  
 
 
n    = 7  # number of fish:
 
fRad = 45  # half width of the fish:
 
  
  
 
# ------------------------------------------------------------------------
 
# ------------------------------------------------------------------------
#                               CLASSES                                       
+
#                                     CLASSES                                       
 
# ------------------------------------------------------------------------
 
# ------------------------------------------------------------------------
#                             --BUBBLE--
+
#                                   --BUBBLE--
 
class bubble:
 
class bubble:
 
     """A class defining a bubble"""
 
     """A class defining a bubble"""
Line 122: 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3]
+
         ddy = [-3, -2, -1, 1, 2, 3] + 15*[0]
 
         index = randrange(len(ddy))
 
         index = randrange(len(ddy))
 
         if self.d:
 
         if self.d:
Line 130: 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 145: 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 153: 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 161: 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 220: Line 232:
  
 
</source>
 
</source>
 
+
</onlydft>
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 08:42, 7 September 2011

--D. Thiebaut 12:22, 28 April 2010 (UTC)



...