Comp 150 – Computer Programming 1

April 3, 2008

 

 

1.         Lab12.b – A GUI for the Trajectory Example

 

#

#  Name:

#  File: Lab12a.py

#  Date: April 3, 2008

#

 

import math

from graphics import *

from button 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 main():

 

    #  set initial conditions

 

    angle = 45

    vel = 100

    h0 = 0

    time = 0.1

 

    #  set up graphics window

   

    screenWidth = 640

    screenHeight = 480

    win = GraphWin("Lab 12a.py - Projectile Trajectory", \

                    screenWidth, screenHeight)

    win.setBackground("white")

 

    #  define world coordinate system

   

    win.setCoords(-20, -20, 1260, 940);

 

    #  draw axes

   

    Line(Point(0, 0), Point(1240,0)).draw(win)

    Line(Point(0, 0), Point(0, 920)).draw(win)

 

    #  display buttons

 

    goButton = button(win, Point(100, 900), 160, 40, "Go")

    anglePlusButton = button(win, Point(100, 850), 160, 40, "Angle+5")

    angleMinusButton = button(win, Point(100, 800), 160, 40, "Angle-5")

    velPlusButton = button(win, Point(100, 750), 160, 40, "Vel +5")

    velMinusButton = button(win, Point(100, 700), 160, 40, "Vel -5")

    quitButton = button(win, Point(100,650), 160, 40, "Quit")

 

    #  activate buttons

   

    goButton.activate()

    anglePlusButton.activate()

    angleMinusButton.activate()

    velPlusButton.activate()

    velMinusButton.activate()

    quitButton.activate()

 

    #  event handler loop

 

    while (True):

        p1 = win.getMouse()

       

        if goButton.clicked(p1):

 

            #  create Projectile object    

 

            cball = Projectile(angle, vel, h0);

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

            dot.setFill("yellow")

            dot.draw(win)

 

            #  plot trajectory

       

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

                x = cball.getX()

                y = cball.getY()

                Point(x,y).draw(win)

                cball.update(time)

 

            #  move cannon ball to final position

   

            dot.move(x, y)

            dot.setFill("red")

 

        #  check for changes in angle or initial velocity

           

        elif anglePlusButton.clicked(p1):

            angle += 5

        elif angleMinusButton.clicked(p1):

            angle -= 5

        elif velPlusButton.clicked(p1):

            vel += 5

        elif velMinusButton.clicked(p1):

            vel -= 5

           

        #  check for quit

 

        elif quitButton.clicked(p1):

            break;

           

    #  close graphics window when done

 

    win.close()

   

main()

 

2.         Lab12b.py

 

#

#  Name:

#  File: Lab12b.py

#  Date: April 3 2008

#

#  Desc: Almost Yahtzee!

#

 

from graphics import *

from random import randrange

from button import *

from dieview import *

 

class CheckBox:

 

    def __init__(self, win, center, size):

        x, y = center.getX(), center.getY()

        hsize = size/2.0

        self.win = win

        self.xmax , self.xmin = x+hsize, x-hsize

        self.ymax , self.ymin = y+hsize, y-hsize

 

        #  generate check box

       

        p1 = Point(self.xmin, self.ymin)

        p2 = Point(self.xmax, self.ymax)

        self.rect = Rectangle(p1, p2)

        self.rect.setFill('white')

        self.rect.draw(self.win)

 

        #  generate crossed lines

       

        self.l1 = Line(p1, p2)

        self.l1.setWidth(2)

        self.l2 = Line(Point(self.xmin, self.ymax), \

                       Point(self.xmax, self.ymin))

        self.l2.setWidth(2)

        self.checked = False;   # True if checked

 

    def getStatus(self):

        "Returns status of checkbox - either True or False"

        return self.checked

 

    def clicked(self, p):

        "Toggles state and appearance of of checkbox if clicked"

        if (self.xmin <= p.getX() <= self.xmax and \

            self.ymin <= p.getY() <= self.ymax):

            if self.checked:

                self.checked = False

                self.l1.undraw()

                self.l2.undraw()

            else:

                self.checked = True

                self.l1.draw(self.win)

                self.l2.draw(self.win)               

 

def main():

  

    #  Open a 401 by 401 graphics window object

 

    windowTitle  = "Yahtzee Dice Demo - April 3, 2008"

    screenWidth  = 401

    screenHeight = 401

   

    win = GraphWin(windowTitle, screenWidth, screenHeight)

    win.setCoords(0, 0, screenWidth, screenHeight)

    win.setBackground("darkgreen")

 

    #  display title to screen

 

    title = Text(Point(200, 350), windowTitle)

    title.setSize(14)

    title.setTextColor("white")

    title.draw(win)

 

    #  create and activate two buttons

 

    rollButton = button(win, Point(175, 17), 40, 20, "Roll")

    quitButton = button(win, Point(225, 17), 40, 20, "Quit")

    rollButton.activate();

    quitButton.activate()

 

    #  create 5 dice

 

    die1 = DieView(win, Point(75, 200), 50)

    die2 = DieView(win, Point(135, 200), 50)

    die3 = DieView(win, Point(195, 200), 50)

    die4 = DieView(win, Point(255, 200), 50)

    die5 = DieView(win, Point(315, 200), 50)

 

    #  create 5 corresponding check boxes

   

    check1 = CheckBox(win, Point(75, 160),20)

    check2 = CheckBox(win, Point(135, 160),20)

    check3 = CheckBox(win, Point(195, 160),20)

    check4 = CheckBox(win, Point(255, 160),20)

    check5 = CheckBox(win, Point(315, 160),20)

 

    #  event handler loop

   

    while (True):

        p1 = win.getMouse()

 

        #  roll dice event

       

        if rollButton.clicked(p1):

            if not check1.getStatus():

                die1.setValue(randrange(1,7))

            if not check2.getStatus():  

                die2.setValue(randrange(1,7))

            if not check3.getStatus():

                die3.setValue(randrange(1,7))

            if not check4.getStatus():

                die4.setValue(randrange(1,7))

            if not check5.getStatus():   

                die5.setValue(randrange(1,7))

 

        #   quit event

       

        elif quitButton.clicked(p1):

            break

 

        #   check if one or more checkboxes are clicked

       

        else:

            check1.clicked(p1)

            check2.clicked(p1)

            check3.clicked(p1)

            check4.clicked(p1)

            check5.clicked(p1)

 

    #  close graphics window when done.

 

    win.close()

 

main()