Difference between revisions of "CSC111 Homework 11 Solution Program 2014"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <source lang="python"> #---------------------------------------------------------------------- # hw11b.py # Julieanna Niu (bi) # 4/24/2014 # # Program containing s...")
 
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:56, 29 April 2014 (EDT)
 
--[[User:Thiebaut|D. Thiebaut]] ([[User talk:Thiebaut|talk]]) 12:56, 29 April 2014 (EDT)
 
----
 
----
 +
<onlydft>
 +
 +
<br />
 +
=Source=
 +
<br />
 +
 
<source lang="python">
 
<source lang="python">
 
#----------------------------------------------------------------------
 
#----------------------------------------------------------------------
Line 321: Line 327:
 
</source>
 
</source>
 
<br />
 
<br />
 +
==Output==
 
<br />
 
<br />
 +
<center>[[Image:CSC111UnhappyEaster.png|500px]]</center>
 
<br />
 
<br />
 
<br />
 
<br />
 +
=Program 2=
 
<br />
 
<br />
 +
This program uses inheritance to define the car classes.
 +
<br />
 +
<source lang="python">
 +
 +
# hw11b.py
 +
# Thuy Nguyen (ce)
 +
# This program prints out an example of a wrapping paper print
 +
# where the theme is cars, the background color is light pink,
 +
# and displays "Happy Easter" as the message. The uses two
 +
# different types of cars where the program, generates rows of cars
 +
# alternating between the two types of cars. The message is
 +
# displayed in the middle of the wrapping paper inside a light
 +
# blue rectangle.
 +
#
 +
# -------------------------------------------------------------------
 +
 +
from graphics import GraphicsWindow
 +
from random import seed
 +
from random import randrange
 +
 +
MAXWIDTH = 800
 +
MAXHEIGHT = 600
 +
 +
class Circle:
 +
    # a class containing 4 private member variables:
 +
    # - center x position of circle (integer)
 +
    # - center y position of circle (integer)
 +
    # - diameter of circle (integer)
 +
    # - color of circle (list of 3 integers between 0 and 255)
 +
    # methods:
 +
    # draw( canvas ): the circle draws itself
 +
 +
    # constructor: initializes a circle with all fields
 +
    def __init__( self, x, y, diameter, color ):
 +
        self._x      = x
 +
        self._y      = y
 +
        self._diameter= diameter
 +
        self._color  = color
 +
       
 +
    # draws a circle at defined (x,y) coordinates with specified color
 +
    def draw( self, canvas ):
 +
        canvas.setFill( self._color[0], self._color[1], self._color[2] )           
 +
        canvas.drawOval( self._x-self._diameter//2,
 +
                        self._y-self._diameter//2,
 +
                        self._diameter,
 +
                        self._diameter )
 +
 +
class Wheel:
 +
    # a class containing 3 private member variables:
 +
    # - center x position of wheel (integer)
 +
    # - center y position of wheel (integer)
 +
    # - diameter of wheel (integer)
 +
    # methods:
 +
    # draw( canvas ): the wheel draws itself
 +
 +
    # constructor: initializes two wheels using Circle class
 +
    def __init__( self, x, y, diameter ):
 +
        self._c1 = Circle( x, y, diameter, ( 0,0,0 ) )
 +
        self._c2 = Circle( x, y, diameter/2, ( 200, 200, 200 ) )
 +
 +
    # draws two wheels
 +
    def draw( self, canvas ):
 +
        self._c1.draw( canvas )
 +
        self._c2.draw( canvas )
 +
       
 +
class Car:
 +
    # a class containing 5 private member variables:
 +
    # - x position of car (integer)
 +
    # - y position of car (integer)
 +
    # - width of car (integer)
 +
    # - height of car (integer)
 +
    # - color of car (list of 3 integers between 0 and 255)
 +
    # methods:
 +
    # draw( canvas ): the car draws itself
 +
 +
    # constructor: initializes a car with all fields
 +
    def __init__( self, x, y, width, height, color ):
 +
        self._x      = x
 +
        self._y      = y
 +
        self._width  = width
 +
        self._height  = height
 +
        self._color  = color 
 +
 +
        # build the body
 +
        self._body = Rectangle( x, y, width, height, color )
 +
       
 +
        # build the wheels
 +
        self._w1 = Wheel( x + width//4, y + height, width//5 )
 +
        self._w2 = Wheel( x + 3*width//4, y + height, width//5 )
 +
 +
    # draws a car with its body and wheels
 +
    def draw( self, canvas ):
 +
        self._body.draw( canvas )
 +
        self._w1.draw( canvas )
 +
        self._w2.draw( canvas )
 +
 +
 +
class CarTop( Car ):
 +
    # a class inherited from Car class with one additional private member variable
 +
    # - the top hood of a car
 +
    # methods:
 +
    # draw( canvas ): the car with top draws itself
 +
   
 +
    def __init__( self, x, y, width, height, color, topColor ):
 +
        super().__init__( x, y, width, height, color )
 +
        self._top = Rectangle( x+width//4, y-height//2, width//2, height//2, topColor )
 +
 +
    # draws a car with a top hood
 +
    def draw( self, canvas ):
 +
        super().draw( canvas )
 +
        self._top.draw( canvas )
 +
 +
class CarTop2( Car ):
 +
    # a class inherited from Car class with two additional private member variables
 +
    # - the top hood of a car
 +
    # - the window of a car
 +
    # methods:
 +
    # draw( canvas ): the car with top and window draws itself
 +
   
 +
    def __init__( self, x, y, width, height, color, topColor ):
 +
        super().__init__( x, y, width, height, color )
 +
        self._top = Rectangle( x+width//2, y-height//2, width//2, height//2, topColor )
 +
        self._window = Rectangle( x+width//3, y+height//4, width//3, height//3, ( 0,0,0 ) )
 +
 +
    # draws a car with a top hood and window
 +
    def draw( self, canvas ):
 +
        super().draw( canvas )
 +
        self._top.draw( canvas )
 +
        self._window.draw( canvas )
 +
 +
class Rectangle:
 +
    # a class containing 5 private member variables
 +
    # - x position of rectangle (integer)
 +
    # - y position of rectanle (integer)
 +
    # - width of rectangle (integer)
 +
    # - height of rectangle (integer)
 +
    # - color of rectangle (list of 3 integers between 0 and 255)
 +
    # methods:
 +
    # draw( canvas ): the rectangle draws itself
 +
 +
    # constructor: initializes rectangle with all fields
 +
    def __init__( self, x, y, width, height, color ):
 +
        self._x      = x
 +
        self._y      = y
 +
        self._width  = width
 +
        self._height = height
 +
        self._color  = color 
 +
 +
    # draws a rectangle
 +
    def draw( self, canvas ):
 +
        canvas.setFill( self._color[0], self._color[1], self._color[2] )           
 +
        canvas.drawRectangle( self._x, self._y, self._width, self._height )
 +
 +
# --------------------------------------------------------------------------------
 +
# setRandomColor(): sets a random color to a car's body and hood
 +
# --------------------------------------------------------------------------------
 +
def setRandomColor():
 +
    r = randrange( 255 )
 +
    g = randrange( 255 )
 +
    b = randrange( 255 )
 +
    return r, g, b
 +
 +
# --------------------------------------------------------------------------------
 +
# multipleCars( canvas ): displays the cars such that they are organized into
 +
# rows, one above the other, and alternating each type of car in separate rows
 +
# --------------------------------------------------------------------------------
 +
def multipleCars( canvas ):
 +
    width = 100
 +
    height = 25
 +
    for x in range( 0, MAXWIDTH, 11*width//10 ):
 +
        for y in range( 0, MAXHEIGHT, 4*height ):
 +
            color = setRandomColor()
 +
            topColor = setRandomColor()
 +
            c = CarTop( x, y, width, height, color, topColor )
 +
            c.draw( canvas )
 +
        for y in range( 2*height, MAXHEIGHT, 4*height ):
 +
            color = setRandomColor()
 +
            topColor = setRandomColor()
 +
            c2 = CarTop2( x, y, width, height, color, topColor )
 +
            c2.draw( canvas )
 +
           
 +
# ================================================
 +
#                  Main Program
 +
# ================================================           
 +
def main():
 +
    seed()
 +
    win = GraphicsWindow(MAXWIDTH, MAXHEIGHT)
 +
    canvas = win.canvas()
 +
   
 +
    multipleCars( canvas )
 +
    canvas.setBackground( 'thistle1' )
 +
    canvas.setFill( 'azure' )
 +
    canvas.drawRectangle( MAXWIDTH // 3, 2*MAXHEIGHT // 5, 325, 60 )
 +
    canvas.setTextFont( "times", 40, "bold" )
 +
    canvas.drawText( MAXWIDTH // 3, 2*MAXHEIGHT // 5, "Happy Easter" )
 +
   
 +
    win.wait()
 +
    win.close()
 +
   
 +
main()
 +
 +
 +
 +
</source>
 
<br />
 
<br />
 
<br />
 
<br />
 +
</onlydft>
 +
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 21:45, 9 January 2015

--D. Thiebaut (talk) 12:56, 29 April 2014 (EDT)



...