Difference between revisions of "CSC111 Lab 11 2015"

From dftwiki3
Jump to: navigation, search
Line 221: Line 221:
 
* Make your car move some deltax, deltay in a loop, the same way you moved the MyRect and MyCirc objects in the previous problem.  Verify that the car moves correctly, including its label.
 
* Make your car move some deltax, deltay in a loop, the same way you moved the MyRect and MyCirc objects in the previous problem.  Verify that the car moves correctly, including its label.
 
<br />
 
<br />
=Animal Class=
+
=Aquarium=
 +
<br />
 +
* Point your browser to [[Fish_for_an_Aquarium|this page]] and drag the tank and a couple fish to your desktop.
 +
 
 +
* Write a new program called '''lab11_5.py''' and copy/paste the following code in it:
 +
<br />
 +
::<source lang="python">
 +
# lab11_5.py
 +
# Displays fish in an aquarium
 +
 
 +
from graphics import *
 +
import random
 +
 
 +
WIDTH  = 700  # geometry of the tank2.gif file
 +
HEIGHT = 517
 +
 
 +
       
 +
def main():
 +
    # open the window
 +
    win = GraphWin( "CSC Aquarium", WIDTH, HEIGHT )
 +
 
 +
    # display background image
 +
    background = Image( Point( WIDTH//2, HEIGHT//2 ), "tank2.gif" )
 +
    background.draw( win )
 +
 
 +
    # display the image of a fish at a random location
 +
    fish = Image( Point( random.randrange( WIDTH),
 +
                        random.randrange( HEIGHT) ),
 +
                  "fish0.gif" )
 +
    fish.draw( win )
 +
 
 +
    # close window when user clicks it
 +
    win.getMouse()
 +
    win.close()
 +
   
 +
main()
 +
 
 +
 
 +
 
 +
</source>
 +
 
 +
* Create a new class called '''Fish'''
 +
* Include a '''constructor''' that received the name of the gif file for the fish.
 +
* Include a '''member variable''' in the class to hold the image (Image class in graphics.py) of the fish.
 +
* Add a '''draw()''' method to the class, that will allow the main program to have the fish draw itself on the graphic window (win).
 +
* Add a '''moveRandom()''' method to the class, that will move the image of the fish some random deltaX and deltaY on the graphic window.  Reminder: user <tt>random.randrange( 10 )</tt> to generate a random number between 0 and 10, and use <tt>random.randrange( -10, 0 )</tt> to generate a random number between -10 and 0 (not included).
 +
* Modify the main program so that it simply creates and displays a fish object, as shown below:
 +
<br />
 +
::<source lang="python">
 +
def main():
 +
    # open the window
 +
    win = GraphWin( "CSC Aquarium", WIDTH, HEIGHT )
 +
 
 +
    # display background
 +
    background = Image( Point( WIDTH//2, HEIGHT//2 ), "tank2.gif" )
 +
    background.draw( win )
 +
 
 +
    # display a fish
 +
    fish = Fish( "fish0.gif" )
 +
    fish.draw( win )
 +
 
 +
    win.getMouse()
 +
    win.close()
 +
 
 +
</source>
 +
<br />
 +
* When your code works, and only then, add a loop to make the fish move:
 +
<br />
 +
::<source lang="python">
 +
def main():
 +
    # open the window
 +
    win = GraphWin( "CSC Aquarium", WIDTH, HEIGHT )
 +
 
 +
    # display background
 +
    background = Image( Point( WIDTH//2, HEIGHT//2 ), "tank2.gif" )
 +
    background.draw( win )
 +
 +
    # display a fish
 +
    fish = Fish( "fish0.gif" )
 +
    fish.draw( win )
 +
 
 +
    # animation loop
 +
    while win.checkMouse() == None:
 +
        fish.moveRandom()
 +
 
 +
    win.getMouse()
 +
    win.close()
 +
 
 +
</source>
 +
 
 +
<br />
 +
* Modify the moveRandom() method so that the fish disappears from the graphics window, it is moved back on the other side of the aquarium.
 +
* Create a list of 10 fish with the "fish0.gif" image, and 10 fish with the "fish20.gif" image (or some fish that is headed in the opposite direction).
 +
* Modify the Fish class so that you can define the direction a fish goes into.
 +
<br />
 
<br />
 
<br />
  

Revision as of 12:31, 12 April 2015

--D. Thiebaut (talk) 19:36, 11 April 2015 (EDT)



...