Comp 150 – Computer Programming 1

March 26, 2008

 

Lab11a.py – Playing Craps!

 

#

#  Name:

#  File: Lab11a.py

#  Date: March 26, 2008

#

 

from random import randrange

 

class MSDie:

#

#   A Multi-Sided Die Class

#

    def __init__(self, sides):  # constructor

        self.sides = sides

        self.value = randrange(1, sides+1)

 

    def roll(self):

        self.value = randrange(1, self.sides+1)

 

    def getValue(self):

        return self.value

 

    def setValue(self, value):

        self.value = value

     

def main():

 

    #display title

 

    print "\nCraps\n"

 

    #  create two die objects

 

    die1 = MSDie(6)

    die2 = MSDie(6)

 

    # ask-before0-iterating loop

 

    again = "y"

    while (again == "y"):

 

        #  roll dice

 

        die1.roll()

        die2.roll()

 

        # get & display sum of pips

       

        total = die1.getValue() +  die2.getValue()

        print "Roll = %d %d" % (die1.getValue(), die2.getValue())

 

        # check possible won/lose outcomes

       

        if (total == 2) or (total == 3) or (total == 12):

            print "Total is %d - You Lose!" % total

        elif (total == 7) or (total == 11):

            print "Total is % d - You Win" % total

        else:

 

            #  establish the "point"

            

            point = total

            print "Your point is %d" % point

 

            #  continue rolling until point made or 7 comes up

           

            while (True):       # loop and a half

                die1.roll()

                die2.roll()

                total = die1.getValue() + die2.getValue()

                print "Roll = %d %d" % (die1.getValue(), die2.getValue())

                if (total == point):

                    print "Made your point! - You Win!"

                    break

                elif (total == 7):

                    print "Rolled a 7 - You Lose!"

                    break

 

        #  ask to go again

 

        again = raw_input("\nAgain? (y/n): ")

 

main()        

 

 

Lab11b.py – The trajectory of a cannon-ball

 

 

#

#  Name:

#  File: Lab11b.py

#  Date: March 26, 2008

#

 

# from math import pi, sin, cos

 

import math

from graphics import *

 

class Projectile:

#

#   Projectile class

#

    def __init__(self, angle, velocity, height):   # constructor

        self.xpos = 0.0                          

        self.ypos = height

        theta = angle* math.pi / 180.0

        self.xvel = velocity * math.cos(theta)

        self.yvel = velocity * math.sin(theta)

 

    def getX(self):

        return self.xpos

 

    def getY(self):

        return self.ypos

 

    def update(self, time):

        self.xpos = self.xpos + time * self.xvel

        yvel1 = self.yvel - 9.8 * time

        self.ypos = self.ypos + time * (self.yvel + yvel1)/2.0

        self.yvel = yvel1

 

def getInputs():

    #

    #  get values for initial degree of trajectory, initial velocity

    #  and initial height and time increment

    #

    angle = input("Enter the launch angle (in degrees): ")

    vel = input("Enter the initial velocity (in meters/sec): ")

    h0 = input("Enter the initial height (in meters): ")

    time = input("Enter the time interval between position calculations: ")

    return angle, vel, h0, time

 

def main():

 

    #  get initial conditions

   

    angle, vel, h0, time = getInputs()

 

    #  create cannon ball object

   

    cball = Projectile(angle, vel, h0)

 

    #  set up graphics window

   

    screenWidth = 640

    screenHeight = 480

    win = GraphWin("Projectile Trajectory", screenWidth, screenHeight)

    win.setBackground("white")

 

    #  define world coordinate system

   

    win.setCoords(-20, -20, 2*screenWidth-20 , 2*screenHeight-20);

 

    #  draw axes

   

    Line(Point(0, 0), Point(2*screenWidth-40,0)).draw(win)

    Line(Point(0, 0), Point(0, 2*screenHeight-40)).draw(win)

 

    #  display initial position of cannon ball

        

    dot = Circle(Point(0,h0), 10)

    dot.setFill("red")

    dot.draw(win)

 

    #  plot trajectory

 

    maxY = h0    

    while (cball.getY() >= 0):

        x = cball.getX()

        y = cball.getY()

        if (y > maxY):

            maxY = y

        Point(x,y).draw(win)

        cball.update(time)

 

    #  move cannon ball to final position

   

    dot.move(x, y)

 

    #  display max height and distance traveled

   

    print "\nDistance traveled: %0.1f meters " % (cball.getX())

    print "Maximum height: %0.1f meters " % (maxY)

 

    #  close graphics window when done

   

    raw_input("Hit (Enter) to Quit: ")

    win.close()

   

   

   

main()