Difference between revisions of "CSC111 Lab 11 2014"

From dftwiki3
Jump to: navigation, search
(Simple Test)
Line 33: Line 33:
 
</source>
 
</source>
 
<br />
 
<br />
 +
<br />
 +
<!-- ----------------------------------------------------------------------------------------------- -->
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
  
=Improve on it=
+
==Challenge 1:  Two rectangle, random colors==
* Try with different colors by picking a different combination of the three ints in '''setFill()'''
+
|}
* Try a random color.  Hints: to generate a random number between 0 and 255, you can do this:
+
[[Image:QuestionMark1.jpg|right|120px]]
:<source lang="python">
+
<br />
 +
* Change the color of the rectangle.
 +
* Add a second rectangle with a different color
 +
* Make the color of the two rectangles random. 
 +
<br />
 +
::'''Hints:''' to generate a random number between 0 and 255, you can do this:
 +
<br />
 +
:::<source lang="python">
 
# at the top of the program
 
# at the top of the program
 
from random import seed
 
from random import seed
 
from random import randrange
 
from random import randrange
  
def main():
+
# at the beginning of main()
    seed()
+
seed()
    ...
+
 
    x = randrange( 256 ):
+
# where you need a random number between 0 and 255 (included):
 +
x = randrange( 256 ):
 
</source>
 
</source>
 +
<br />
 +
<br />
 +
<!-- ----------------------------------------------------------------------------------------------- -->
 +
{| style="width:100%; background:silver"
 +
|-
 +
|
  
==Challenge==
+
==Challenge 2:  500 random rects==
Generate 500 random rects of random colors at random locations
+
|}
 +
[[Image:QuestionMark2.jpg|right|120px]]
 +
<br />
 +
* Generate 500 random rectangles of random colors at random locations, and with random width and height.
 +
<br />
 +
<br />
 +
<br />
 +
<center>[[Image:graphics2.png|400px]]</center>
 +
<br />
 +
<br />
  
 
=Draw circles=
 
=Draw circles=

Revision as of 16:51, 15 April 2014

--D. Thiebaut (talk) 17:42, 13 April 2014 (EDT)


Creating the Graphics Library


The graphics library written by Horstmann, the author of our textbook is available here. Download it and create a file called graphics.py in your current directory.

Simple Test

Graphics1.png


Verify that your library is correctly installed by running the python program below. Call it lab10.py. Make sure to save it in the same directory where you saved graphics.py.

from graphicsH import GraphicsWindow

MAXWIDTH = 800
MAXHEIGHT = 600


def main():
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()

    canvas.setFill( 255, 0, 0  )    # full red
    canvas.drawRect( 100, 100, 300, 200 )

    win.wait()
    win.close()

main()



Challenge 1: Two rectangle, random colors

QuestionMark1.jpg


  • Change the color of the rectangle.
  • Add a second rectangle with a different color
  • Make the color of the two rectangles random.


Hints: to generate a random number between 0 and 255, you can do this:


# at the top of the program
from random import seed
from random import randrange

# at the beginning of main()
seed()

# where you need a random number between 0 and 255 (included):
x = randrange( 256 ):



Challenge 2: 500 random rects

QuestionMark2.jpg


  • Generate 500 random rectangles of random colors at random locations, and with random width and height.




Graphics2.png



Draw circles

Create a Rectangle Class

from graphics import GraphicsWindow
from random   import randrange
from random   import seed
from time     import sleep

MAXWIDTH = 800
MAXHEIGHT= 600


class Rectangle:
    def __init__( self, x, y, width, height, color ):
        self._x      = x
        self._y      = y
        self._width  = width
        self._height = height
        self._color  = color   # list of 3 ints between 0 and 255

    def draw( self, canvas ):
        canvas.setFill( self._color[0], self._color[1], self._color[2] )            
        canvas.drawRect( self._x, self._y, self._width, self._height )

def twoRects( canvas ):
    rect1 = Rectangle( 100, 100, 500, 20, (255, 0, 0 ) )
    rect1.draw( canvas )
    
    rect2 = Rectangle( 200, 200, 50, 200, (165, 76, 89 ) )
    rect2.draw( canvas )

def fiveHundredRects( canvas ):
    for i in range( 500 ):
        x = randrange( MAXWIDTH*3//4 )
        y = randrange( MAXHEIGHT *3//4 )
        w = randrange( MAXWIDTH // 2 )
        h = randrange( MAXHEIGHT // 2 )
        red   = randrange( 256 )
        green = randrange( 256 )
        blue  = randrange( 256 )
        rect = Rectangle( x, y, w, h, (red, green, blue ) )
        rect.draw( canvas )
        
            
def main():
    seed()
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
    canvas = win.canvas()

    #twoRects( canvas )

    fiveHundredRects( canvas )

    win.wait()
    win.close()    
    
        
main()

Create a Circle Class

Use the example of Rectangle

Create a Circle Class with a Center

Modify the class so that the x and y passed are the x and y of the center of the circle.

Create a class Wheel that contains two concentric circles

The inside circle will be concentric with the outside one, and will always have a radius half the radius of the large one.

Generate 200 random wheels on the canvas

Create a new Class that is a car

  • one rectangle
  • two wheels
  • x, y of top left corner of rectangle
  • width and height of rectangle.
  • center of 1st wheel 1/4 width from left of car
  • center of 2nd wheel 1/4 width from right of car
  • radius of wheel = 1/8 of car width

Create a car with a top