Difference between revisions of "CSC111 Lab 11"

From dftwiki3
Jump to: navigation, search
(Fish (Reported from Lab 10))
Line 70: Line 70:
 
* Make the fish go up or down, slightly, randomly, as it moves forward...
 
* Make the fish go up or down, slightly, randomly, as it moves forward...
  
 +
==Aquarium==
 +
 +
<center>
 +
[[Image:tank2.gif]]
 +
</center>
 
<br />
 
<br />
 +
Get a copy of this image:
 +
 +
  getcopy tank2.gif
 +
 +
 
<br />
 
<br />
 
<br />
 
<br />

Revision as of 10:50, 14 April 2010


This lab deals with classes, objects, and graphics. It builds on the examples we saw in class on Monday and Wednesday.



Fileicon-pdf.png
You may find the following document describing the different graphic objects introduce in the Zelle's textbook useful.



Fish (Reported from Lab 10)

  • See all the fish? Make a note of one of the fish, and get a copy of its file as follows
  getcopy fishxx.gif            (replace xx by the actual number of the fish you want)
  • Write a new graphics program and copy/paste the following code in it:


.

from graphics import *
H = 400
W = 400

def waitForClick( win, message ):
    """ waitForClick: stops the GUI and displays a message.  
    Returns when the user clicks the window. The message is erased."""

    # wait for user to click mouse to start
    startMsg = Text( Point( win.getWidth()/2, win.getHeight()/2 ), message )
    startMsg.draw( win )    # display message
    win.getMouse()          # wait
    startMsg.undraw()       # erase


def main():
    global H, W
    win = GraphWin( "Fish Tank", W, H )
    waitForClick( win, "click to start" )

    fish = Image( Point( W/2, H/2 ), "fish15.gif" )  # replace 15 by the number of your fish
    fish.draw( win )

    waitForClick( win, "click to end" )
    win.close()

main()

 
.
  • Add a for loop and make the object fish move by dx, dy some fixed number of steps...
  • Create a an new class which is a fish, with its own file name, and its own dx and dy.
  • Create an object issued from the class. Verify that you can make the object move on the screen.
  • Create a school of several fish that move around the screen. The fish may move in opposite directions, but we'll assume that all fish move in the direction of where their head points to!!!
  • Make the fish go up or down, slightly, randomly, as it moves forward...

Aquarium

Tank2.gif


Get a copy of this image:

  getcopy tank2.gif