Difference between revisions of "CSC111 Lab 10 2010"

From dftwiki3
Jump to: navigation, search
(Random cars going in random directions)
(Fish)
Line 162: Line 162:
 
   tar -xzvf allfish.tgz
 
   tar -xzvf allfish.tgz
  
* Copy all the fish to your public_html directory (which is your directory for Web pages):
+
* Take a look at what is in your directory:
  
   cp fish*.tgz public_html
+
   ls
  
* Make all the fish readable by the Web server:
+
:See all the fish there? 
  
 +
* You are going to create a Web page with a picture of all of them, just so that you can see what they look like.
  
  chmod a+r public_html/fish*
+
* Copy all the fish files to your public_html directory. This is the directory where you can store Web pages that will be visible on the Web.
  
 +
  cp fish*.gif public_html
  
* Point your favorite browser to your 111c URL:  http://beowulf.csc.smith.edu/~111c-xx/  (''replace xx by your two-letter Id'')
+
* go to your public_html directory
  
* Click on any of the fish to see what they look like.
+
  cd public_html
 +
 
 +
* list all the fish files:
 +
 
 +
  ls -1                      (''that's ell ess space minus one'')
 +
 
 +
* save the list in a file, which will become a python program
 +
 
 +
  ls -1  >!  index.html
 +
 
 +
* emacs the file '''index.html''' and add the following code around your list of fish files:

Revision as of 21:40, 6 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.


A class for a wheel

Below is the code we wrote in class yesterday:

      # lab10_1
      # A program incorporating a class for a wheel
      # which can move freely on the screen

      from graphics import *
      WinXBR = 700    # window geometry
      WinYBR = 500


      #----------------------------------------------------------------
      # Wheel class
      #----------------------------------------------------------------
      class Wheel:
          """A wheel is two concentric circles that move as one unit"""

          def __init__( self, c, rs, rb ):
              """constructor.  Gets the center, the small radius and the large radius"""
              self.bigCircle   = Circle( c, rb )
              self.smallCircle = Circle( c, rs )

          def move( self, dx, dy ):
              """moves the wheel by dx horizontally and dy vertically"""
              self.bigCircle.move( dx, dy )
              self.smallCircle.move( dx, dy )

          def draw( self, win ):
              """draws the circles on the window"""
              self.bigCircle.draw( win )
              self.smallCircle.draw( win )

      #----------------------------------------------------------------
      # waitForClick: stops the GUI and displays a message.  
      #----------------------------------------------------------------
      def waitForClick( win, message ):
          # wait for user to click mouse to start
          startMsg = Text( Point( WinXBR/2, WinYBR/2 ), message )
          startMsg.draw( win )    # display message
          win.getMouse()          # wait
          startMsg.undraw()       # erase


      #----------------------------------------------------------------
      #----------------------------------------------------------------
      def main():
          win = GraphWin( "Demo: Wheeling Around", WinXBR, WinYBR  )
          waitForClick( win, "Click mouse to start" )
          
          #--- create a wheel ---
          w = Wheel( Point( 30, 50 ), 25, 30 )
          w.draw( win )
              
          waitForClick( win, "Click mouse to stop" )
          win.close()

      main()


  • Run the program.
  • Add a loop that will make the wheel move by 400 steps, horizontally.
  • Add a method to set the color of the wheel. This method will receive two colors, and set the color of the inside circle with the first color passed, and the color of the outside circle with the second color passed. Just look at how the methods of the class Wheel work to figure out how to write the setFill() method. It has to be logical. This is new syntax for us, so don't hesitate to ask for help if you aren't sure...
  • Test the setFill() method.
  • How does your program work if the user specifies the larger radius first in the constructor? For example, replace the creation of the w object with this statement:
      w = Wheel( Point( 30, 50 ), 30, 25 )
What happens with the colors?
  • Modify the class so that it swaps the radii around if the user does not specify them in the correct order.
  • The Circle, Rectangle, Line and other objects supported by the graphics library all have an undraw() method, that does take no parameters. Add an undraw() method to your Wheel class.
  • The wheel is very close to a circle. In fact it's a circle with a smaller circle inside, so some of the methods that apply only to circles should probably be added our wheel class.
    • getCenter()
    • getRadius()
These are different from the others: they return values. Start with getRadius() first. Make it return the radius of the larger circle.


The getCenter() method of the Circle returns a point. Your method should return a point too. In fact, it is a simple method: it will return the point returned by the getCenter() method of any of the two circles... Think about it...


  • Test your method with the python statements below... They will energize all your methods:
         w = Wheel( Point( 30,30 ), 20, 25 )
         w.draw( win )
         waitForClick( win, "click to color" )    
         w.setFill( "orange", "black" )
         waitForClick( win, "click to hide" )
         w.undraw()
         waitForClick( win, "click to bring back" )
         w.draw( win )
         waitForClick( win, "click to get coordinates of wheel" )
         p = w.getCenter()
         x = p.getX()
         y = p.getY()
         waitForClick( win, "center of wheel is at (%d, %d)" % ( x, y ) )
         r = w.getRadius()
         waitForClick( win, "radius of the wheel is %d" % ( r ) )

A class for a car

Rather than telling you how to create the class for a car, try to figure it out by seeing how car objects are used in the simple example below. All you need to know is that a car contains 3 objects: two wheels, and a rectangle, which is horizontal, and whose two bottom corners correspond to the centers of the wheels. Only the outside radius of the wheel is specified. The inside radius is taken to be 60% the size of the outside radius.

Car is the class, and car1 the object.

          # create a car, first wheel centered at 30,30, 2nd wheel
          # at 90,30, both wheels with a radius of 20, and the 
          # rectangle with a height of 40.
          car1 = Car( Point( 30, 30 ), 20, Point( 90,30 ), 20, 40 )
          car1.draw( win )

          # color the wheels grey with black tires, and the rectangle yellow
          car1.setFill( "black", "grey", "yellow" )

          # make the car move on the screen    
          for i in range( 400 ):
              car1.move( 1, 0 )

Random cars going in random directions

  • Make your program create a list of 5 cars.
  • Each car has a random horizontal velocity, making it go left, or right.
  • Make your program move all 5 cars at the same time, until all the cars have exited the window. As soon as the cars all disappear your program will stop.

Fish

  • get a copy of the file allfish.tgz (available here as well):
 getcopy allfish.tgz
  • then unpack it
 tar -xzvf allfish.tgz
  • Take a look at what is in your directory:
 ls
See all the fish there?
  • You are going to create a Web page with a picture of all of them, just so that you can see what they look like.
  • Copy all the fish files to your public_html directory. This is the directory where you can store Web pages that will be visible on the Web.
  cp fish*.gif public_html
  • go to your public_html directory
 cd public_html
  • list all the fish files:
 ls -1                      (that's ell ess space minus one)
  • save the list in a file, which will become a python program
 ls -1  >!   index.html
  • emacs the file index.html and add the following code around your list of fish files: