web1.muirfield-h.schools.nsw.edu.auweb1.muirfield-h.schools.nsw.edu.au/technology/resources/sdd/Pyth… ·...
Transcript of web1.muirfield-h.schools.nsw.edu.auweb1.muirfield-h.schools.nsw.edu.au/technology/resources/sdd/Pyth… ·...
��������������� ���� �
��������������� ���� �
��������������������������������������������������
���������
��������������
��������������� ����
!"#����$��
� ��������������%�����������%��������������$�������������������������&������'
� ���"��"�������������%����������������������������'
� ���������������������������������������������%����������"��������"�����������������������"���������'
��������������� ���� (
!"#����$��
� ���"��"������%��������������$��$��������������������������'
� ���"��"������%�������������$������������������$��$�����$���)����������������*�%�����'
��������������� ���� +
,���-�.�$��%����!"#����
� �������������������������%�’$����$�������������/����������������������������������������������'
� 0�’�����%��-������-���������/������������������������� ������������������'
� ����� �������������$��������������"#��������������������1��������������������Circle'����������������%�2����������%����%����������%��������������������$����"#����'
��������������� ���� 3
,���-�.�$��%����!"#����
� ������������$�����"#��� %�����������������$��������������-��%����������������������'
� 4������������� ����"#����������������
�' ���������������������������������'
' ���������������������������������������������'
��������������� ���� 5
,���-�.�$��%����!"#����
� �������������������������������������"#���������������$��"���'
� ������������� �������������� ������������������“��$�” ������������"#���'
� ��������$��� �������������$��"��������������������������������"���� ������"#���'
��������������� ����
��������������� ���� 6
,���-�.�$��%����!"#����
� ��Circle �"#����%�����$����������$��"�����������center �%����������"���������������������������������� ����radius �%�����������������������������������2�������'
� ����draw ��������&����������center ���radius ����������%�����
��&��������%����%��������"���������'
��������������� ���� 7
,���-�.�$��%����!"#����
� ����move �������%�������������$�������center �����������������%�����������������������'
� �����"#���������������"����������� ������������'����������������"#���������������%���������"����������"#����%�����$�'
� ��������������������������%����������������%����-��%������'
��������������� ���� 8
,���-�.�$��%����!"#����
� 9�%��"#��������������������������"����$�-���������������'�:����������-�������������������������������������������������������%���������'
� ����������-������%���������"#����myCircle = Circle(Point(0,0),20)
� Circle �������������������� ��������������$�-�����������������'
��������������� ���� ��
,���-�.�$��%����!"#����
myCircle = Circle(Point(0,0), 20)
� ������������������������%�Circle������������������������������������������$��"���myCircle'
� ��������������������������������������������������;����������������������$��"����)center ���radius*��������myCircle'
��������������� ���� ��
,���-�.�$��%����!"#����
myCircle = Circle(Point(0,0), 20)
� !�������������������"���������� �������"������������"�����������������������myCircle.draw(win)myCircle.move(dx,dy)
��������������� ���� �
�����"���������������������
� <��’���������%��������������������������������������������"���������������#������'
� 0�’�������������������%�������������"���%������$���%������������$���������������������������$���������'
��������������� ���� (
��������������� ���� �(
�����"���������������������
� �����������������������%����"����������������)���������* ������������$��������)��������������������* ���������������������)���������*�������������"��'
� �����������%����"�������������������������#���������$����"����������-�������������)���������*'
��������������� ���� �+
�����"���������������������
� �������������������$����������������’�������������������8'7������'
� ������"#�����������%�������������� ����� ��������������������%����"����$�������%��������' ����'�������������������� �����������%����"��'+����'�����������������������"#����%���������������"�-���%���������'
��������������� ���� �3
�����"���������������������
� =����������� �%������������$���������������$����������������������������"���������������������������'
� >�%�$�� �%�’������$����������"����%������������� ���������������� �������������������������������"#������$���������������������������������/�������������������������������������)��?���*'
��������������� ���� �5
�����������������
� @�$�����������������������"��� ���’���"$�����%�����������������������������������������"�������%����������������’����������������������������$���'
� <��’������-�����������������������������"���������������)& ��*�%�����& ����������������������������������������� �������������"�$�����������'
��������������� ���� �6
�����������������
� ������������"��������������������)� �* ����%��%����������-���������������$��������������������'
� �����������������$�����%�����$����$������������������%���)������$���*��������������������%���)������$��&*'������&�����������%����"�������������"������$������������������������'
��������������� ���� �7
�����������������
� ������%������������%������������� �&%����������������������������������'
� >�%�$�� �� %����������$����������������$���'������ $��������%�������������������$����������"����������$��������"���������������'
��������������� ���� +
��������������� ���� �8
�����������������
� Input the simulation parameters: angle, velocity, height, interval.
� Calculate the initial position of the cannonball: xpos, ypos
� Calculate the initial velocities of the cannonball: xvel, yvel
� While the cannonball is still flying:� Update the values of xpos, ypos, and yvelfor interval seconds further into the flight
� Output the distance traveled as xpos
��������������� ���� �
�����������������
� =��������1%���������������def main():
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: ")
� ������������������������������������������"�������������'���2�����������������������h0A
xpos = 0ypos = h0
��������������� ���� �
�����������������
� ���%��-��%��������������������$����������������������� �%�������������yvel=velocity*sin(theta)���xvel=velocity*cos(theta)'
��������������� ����
�����������������
� !������������������������� ���������������������"�������������� ����theta = (π*angle)/180'
� theta = (angle * pi)/180.0xvel = vel * cos(theta)yvel = vel * sin(theta)
� ��������������� �%��%������-�������������������������������"�����������������������������while ypos >= 0.0:
� 0�������B?���������������%����������������"�������������������������'
��������������� ���� (
�����������������
� C������������������������%��%���������������������������������"��������$�����time ��������������'
� ������%��������������������%������������� �xvel ��������������'
� ����"��������$�������(������������3������������������������'������������������%����"��3��D�(���������%�'��������������������������'������������%����"��3��D�(�E'��?�3(�������'
� xpos = xpos + time * xvel��������������� ���� +
�����������������
� 0��-���%����yvel ����������������������������������$���������������1$�����������������$�������'
� C��������� �yvel �������������"��8'7���� ������������������������$���'
� ����'��������������$��������%����"���'�)8'7*�?�'87����'
� yvel1 = yvel - 9.8 * time
��������������� ���� 3
��������������� ���� 3
������������������
� �������������%�������������"�����$�����$������������$� �%����������������������$��� $�������$���������$������������$�'
� ����������$����������������$�������������� ������������������$���������������������������$��������������������������������������$��ypos = ypos + time * (yvel + yvel1)/2.0
��������������� ���� 5
��������������# cball1.py# Simulation of the flight of a cannon ball (or other projectile)# This version is not modularized.
from math import pi, sin, cos
def main():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: ")
radians = (angle * pi)/180.0xpos = 0ypos = h0xvel = vel * cos(radians)yvel = vel * sin(radians)while ypos >= 0:
xpos = xpos + time * xvelyvel1 = yvel - 9.8 * timeypos = ypos + time * (yvel + yvel1)/2.0yvel = yvel1
print "\nDistance traveled: %0.1f meters." % (xpos)
main()
��������������� ���� 6
4������;������������
� ��������������$�������� �%���������������1%���������������)������1��%�������* �"�������������$����������������������������'
� 0������������������������������ �������������&���������������"������$��"���'
��������������� ���� 7
4������;������������def main():
angle, vel, h0, time = getInputs()
xpos, ypos = 0, h0
xvel, yvel = getXYComponents(vel, angle)
while ypos >= 0:
xpos, ypos, yvel = updateCannonBall(time, xpos, ypos, xvel, yvel)
print "\nDistance traveled: %0.1f meters." % (xpos)
� ����������"���"$�����%��������������������������������������"�����������������������������������������'
��������������� ���� 8
4������;������������
� �����$�����������������������������������A
� �������"������$��"�������"�����������������������7 �������theta ���yvel1 �����������getXYComponents ���updateCannonBall ���������$���'
� ��������"��������� �"���-���������-�������������"�����������/������������������������������� ����������%�������������������������������A
��������������� ���� (�
4������;������������
� ���������$��"��� ������time ����������������������������%�$��������������������������'
� ������$����������������%������$���������������������������$����'
� :��-A������������"���"������%�A
��������������� ���� 5
��������������� ���� (�
4������;������������
� �������������������1%����������"����"#��� �"��������/���������������������������������xpos ypos xvel & ���yvel'
� ��������������%���Projectile ���������
F����������G ����������������"#�������-�������"���'�����������������������������%�����������������������"#���������������������$��"��'
��������������� ���� (
4������;������������
� =���������"#���1"����������def main():
angle, vel, h0, time = getInputs()cball = Projectile(angle, vel, h0)while cball.getY() >= 0:
cball.update(time) print "\nDistance traveled: %0.1f meters." % (cball.getX())
main()
� ����-�������%��-�%��������Projectile ��������������������������������update �getX ���getY'
��������������� ���� ((
C&������4����1����������
� ������������)��������������*�������"��%������&����� �����%��������"�����������������&'
� �������������������������%������������������"�����������'
� <��’����������������������MSDie ���
�����������1����������'
��������������� ���� (+
C&������4����1����������
� C���MSDie �"#����%����-��%��%��
������
� >�%����������������'
� ��’����������$���
� 0�������%�MSDie ��������� �%��
��������� ��������"���������������%�����$�'
��������������� ���� (3
C&������4����1����������
� 0���$��������������������%������������������������������
� roll H ����������������������$����
"��%���������� ��������$�'
� setValue H �������������������������$����
)�'�'�����*
� getValue H ����%���������������$������'
��������������� ���� (5
C&������4����1����������BBB������?�4����)5*BBB�����'��I���)*
�BBB�����'����)*
BBB�����'��I���)*3
BBB���� �?�4����)�(*BBB���� '��I���)*
�BBB���� '����)*
BBB���� '��I���)*8
BBB���� '���I���)7*BBB���� '��I���)*
7
��������������� ���� 6
��������������� ���� (6
C&������4����1����������
� =���������"#���1���������$��"���� �%�������������"����$�-�������MSDie ����������� ������$������������"����������������������'
� !��������"#�����%����-�������-������������"�������������������������$��"��'
� ����������������$��"�� �����������-����������������$��������������'
� 0�������������������$������������������"��1 "����������$�������$��������������'
� ����$�������"��������"������roll ���setRoll ������� �������������"������getValue������'
��������������� ���� (7
C&������4����1����������# msdie.py# Class definition for an n-sided die.
from random import randrange
class MSDie:
def __init__(self, sides):self.sides = sidesself.value = 1
def roll(self):self.value = randrange(1, self.sides+1)
def getValue(self):return self.value
def setValue(self, value):self.value = value
��������������� ���� (8
C&������4����1����������
� ������������������$����������class <class-name>:
<method-definitions>
� 4����������-��������-�����������A����������������������������������-������������������������� ����������������1�������������'
� ���������������������������������%�������self �%���������������������������"#�������%�����������������������'
��������������� ���� +�
C&������4����1����������
� ��������%���$���main ��������������&�������die1.setValue(8)'
� J���������������������� ���������&����������������%�������1�������/������� main ��������������������������������������������'�����������������������������������������������������������������������"#�������%�������������������"����������'�>��� ������������������������������setValue ��������������MSDie ���� �������die1 ����������������MSDie'
��������������� ���� +�
C&������4����1����������
� �����������������������������������������������$��������������"������������������������������'�������������������������� ����������������������������������������"#����self = die1value = 8
� ����"����������������������&������'
��������������� ���� +
C&������4����1����������
� �����������������������������#���������%����������������%�������'������������ �����������������������%���die1.setValue(8)'
� 4����������������%��������������� �"���������������������������������������������self ����������%����������
������������'
��������������� ���� 7
��������������� ���� +(
C&������4����1����������
� ����self ������������"��--������
�����'�0�����������������������������������������������������������������������������������������'��� �%�����������setValue ����������������������'
��������������� ���� ++
C&������4����1����������
��������������� ���� +3
C&������4����1����������
� !"#�������������������%����'���������$��"�������$��������������������������������"#���'
� ��������$��"���������������"����������������������������<object>.<instance-var>
� <��-�����setValue �%������self.value����������������������$��"���value ������������"#���'�C���MSDie �"#������������%��value'
��������������� ���� +5
C&������4����1����������
� ����������������$��������������'����������������$�����������������������%�����%��K’�'
� __init__ ��������"#��������������'�
����������������������������������;�����%�MSDie'�__init__ ���$������������
$���������������������$��"����������"#���'
��������������� ���� +6
C&������4����1����������
� !��������������� ��������������������������������"���������������die1 = MSDie(6)
� 0����������������������&������ ����%�MSDie �"#�����������������__init__����&���������������"#���'
� ����������������������die1.sides ����������5����die1.value �����������'
��������������� ���� +7
C&������4����1����������
� ��������$��"������������"��������������������������"#��� �����������������������"��������������������������������������"#���'
� ������������������������������������$��"��� �%�����$�������������%��������������������������'
��������������� ���� 8
��������������� ���� +8
C&�������������#�����������
� ����������%��������������������������������;����������$��"��� ���update ����������������������������������#������ ����getX���getY ��������������������������
����������������'
� ���������������� �������"������"���������������������������� �$������� ����������cball = Projectile(angle, vel, h0)
��������������� ���� 3�
C&�������������#�����������
� ����Projectile �����������$����__init__ �����������%��������������
$���������������;��������������$��"�������cball'
� ������$�����%����"����������������������������������"�����'
��������������� ���� 3�
C&�������������#�����������class Projectile:
def __init__(self, angle, velocity, height):self.xpos = 0.0self.ypos = heighttheta = pi * angle / 180.0self.xvel = velocity * cos(theta)self.yvel = velocity * sin(theta)
� 0�2$����������������������$��"����)self.???*'�����������$�������theta ������������������ �����������������������$���'
��������������� ���� 3
C&�������������#�����������
� �������������������������L����:����������������������%��'
def getY(self):
return self.ypos
def getX(self):
return self.xpos
��������������� ���� 3(
C&�������������#�����������
� ������������������update �%�����%�2����-����������������$�����������������������L����:�$����'
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
� yvel1 �������������$��"��'
��������������� ���� 3+
�������������%��������
� ���������������������������������1%������"#����%����������&�"��$���'
� ������������������������"#�����������������������������������������������������"������������������'
� C' ������������������-�������-���������������"��������������)��Employee �����%����
��������������������������2����� �������������������"�� ������� ����� ����'*
��������������� ���� ��
��������������� ���� 33
�������������%��������
� @��������������������-�����������������������������'
� <��’����������������������������&����A
� ������������$����������������������������������������������� ��������������$���������������������+������������%�������“�” ���+������� ��“M” �������� ����'
��������������� ���� 35
�������������%��������
� @����������$������������������������������/������������'�������������%�����(�����������������������������������“�” ��������������������()+*�?�� �/������������'����������������@�� �%����$�������������/�������������"���������"����������������������������'
��������������� ���� 36
�������������%��������
� ��������%���$���������������������������������������������'
� C����������������������������������������’����� �������1����� ����/������������'Adams, Henry 127 228Comptewell, Susan 100 400DibbleBit, Denny 18 41.5Jones, Jim 48.5 155Smith, Frank 37 125.33
��������������� ���� 37
�������������%��������
� !���#�"�������%���������������������������������������������������%��������"����@������������������������� �������1����� ����@��'
� ����������������N���������Student����A
� 0����������Student �"#���������������������������������������$��"���'
��������������� ���� 38
�������������%��������� class Student:
def __init__(self, name, hours, qpoints):self.name = nameself.hours = float(hours)self.qpoints = float(qpoints)
� ����$���������hours ������$���������float �������������������������"������� ����� ����������'
� �������������������������aStudent = Student(“Adams, Henry”, 127, 228)
� ������������������������%�����������������������������"�����������������������$��"��A
��������������� ���� 5�
�������������%��������
� 0����������"��"��������������������������� ����%�������������������������������� �������'
� def getName(self):return self.name
def getHours(self):return self.hours
def getQPoints(self):return self.qpoints
def gpa(self):return self.qpoints/self.hours
� O����&���� ������������������2����������������%�����print aStudent.getName()
��������������� ���� ��
��������������� ���� 5�
�������������%��������
� >�%����%��������������������������������������%��������"����@��N
� 0�����������������������������������������&����� ���"���A�0�����������-���������������������"����� �-���������-��������"����������������������A
��������������� ���� 5
�������������%��������
Get the file name from the user
Open the file for reading
Set best to be the first student
For each student s in the file
if s.gpa() > best.gpa
set best to s
Print out information about best
��������������� ���� 5(
�������������%��������P��'��P��������������������������%�����������@��������������
�������������
����KK����KK)���� ���� ������ �/������*�����'��� ?��������'����� ?�����)�����*����'/������ ?�����)/������*
������9��)����*������������'���
������>����)����*������������'�����
������,������)����*������������'/������
�����)����*������������'/�����������'�����
�����-��������)�������*���� ������ �/������ ?������'�����)������� QR�Q*��������������)��� ������ �/������*
�������)*���������?��%K�����)QC���� ������������������Q*������ ?�����)������� �S�S*"����?��-��������)������'�������)**�������������������
��?��-��������)����*����'�)*�B�"���'�)*�
"����?��������'�����)*������Q����"���������������Q �"���'��9��)*������Q������Q �"���'��>����)*������Q@���Q �"���'�)*
���KK���KK�??�SKK���KKS����)*
��������������� ���� 5+
C����������=�������"���������
� ����������%��������)��-��Projectile���Student*����"�������%������������;��������'
� !�����������������"#������������������ ��������������������������������������������"����$�������������"������������������'
��������������� ���� 53
C����������=�������"���������
� �������������������������%�����"����%����"#���������� �����"������%��������������������'
� ������������������� ������������������������������-��%���������������'
� �������������������������������"#���������������������������������������� �%������������������������������������������$����������%��������'
��������������� ���� 55
C����������=�������"���������
� !�����������������������������"#����������������������������������&��������������"#���������������������������������'
� O�������������������� ���������������%�������"#�������"����������������������������$�����"�������������'
��������������� ���� �
��������������� ���� 56
C����������=�������"���������
� !����$��������������������������������%����������������������$�����������������������%�������%�������"����“"��-��” ����������������������� ����$�������������������������$�����"���������������������������'
��������������� ���� 57
�����������������4������
� ����������%�����������������������������������������������������'
� ������������"�������������������� ������������������%��������������%��������������������������"����%�������������"������������������%��’���$�����������������������������������������������-�������������A
��������������� ���� 58
4������������������
� :�������������������%����“P” ���������������������&�������%��’������������������������'
� ����������������������-��������������������$�������������������������'�:����������������������������������������������������������� ����� ��������������������������������������'
��������������� ���� 6�
4������������������
� 0����������������N
� !��������������������������"��������
� ��������� ���������"������������������"����������__doc__'
� 4�������������"�������������$���&�����$�����������'�O����&���� ����������’�������"�����%��������random�>>> import random>>> print random.random.__doc__random() -> x in the interval [0, 1).
��������������� ���� 6�
4������������������
� ��������� ������������"�����������������������������������"�������������������������������������"�������������������������������������'�:���������������������������������-�������>>> import random>>> help(random.random)Help on built-in function random:
random(...)random() -> x in the interval [0, 1).
��������������� ���� 6
4������������������
� ������������������������������������������� �����������help(module_name)A
� ���������%�������������������#������������������������'
��������������� ���� �(
��������������� ���� 6(
4������������������# projectile.py
"""projectile.pyProvides a simple class for modeling the flight of projectiles."""
from math import pi, sin, cos
class Projectile:
"""Simulates the flight of simple projectiles near the earth'ssurface, ignoring wind resistance. Tracking is done in twodimensions, height (y) and distance (x)."""
def __init__(self, angle, velocity, height):"""Create a projectile with given launch angle, initialvelocity and height."""self.xpos = 0.0self.ypos = heighttheta = pi * angle / 180.0self.xvel = velocity * cos(theta)self.yvel = velocity * sin(theta)
��������������� ���� 6+
4������������������def update(self, time):
"""Update the state of this projectile to move it time secondsfarther into its flight"""self.xpos = self.xpos + time * self.xvelyvel1 = self.yvel - 9.8 * timeself.ypos = self.ypos + time * (self.yvel + yvel1) / 2.0self.yvel = yvel1
def getY(self):"Returns the y position (height) of this projectile."return self.ypos
def getX(self):"Returns the x position (distance) of this projectile."return self.xpos
��������������� ���� 63
0��-���%����4��������4������
� !�����������������������������������#�����������������������������$����������������"���A
# cball4.py# Simulation of the flight of a cannon ball (or other projectile)# This version uses a separate projectile module file
from projectile import Projectile
def getInputs():a = input("Enter the launch angle (in degrees): ")v = input("Enter the initial velocity (in meters/sec): ")h = input("Enter the initial height (in meters): ")t = input("Enter the time interval between position calculations: ")return a,v,h,t
def main():angle, vel, h0, time = getInputs()cball = Projectile(angle, vel, h0)while cball.getY() >= 0:
cball.update(time) print "\nDistance traveled: %0.1f meters." % (cball.getX())
��������������� ���� 65
0��-���%����4��������4������
� �����������������������1������������������� �������������"��%������������������������������"��$���������&����'
� 0���������������������������$��������� ��������������������"#���������������������������������������������������)��������*'���������������������������������)�������&�������* ���"��/������������������������������������'�C$����������������������������������������"��������� ���1���������������������������$����������%�����������������������$������'
��������������� ���� 66
0��-���%����4��������4������
� �����������%��– ��������%���������$���������������������%����$�����������������������$��$�����������������������������'������%�����’����������������������������������������������������������’�������'
��������������� ���� 67
0�����
� !���$�������������������"#����������������������������������������������)@=��*'
� M�-�����������3�%����-���"����@=���"����������������$���������������"#�����-��%����%�����'
� ����Entry �"#�������������������graphics ��"������������&���������
%����'
��������������� ���� �+
��������������� ���� 68
C&�����������������.�����
� <��’��"��������������������%�����A� ������������������������������������&1����������'
� ����������%�����������������������������������$�����%��"������ ������������������������������������/����������������'
��������������� ���� 7�
C&�����������������.�����
� ����������%��-��������%�������"��������������'
� �����%��"�������%����"���&�������������Button ���� �%�����
��������������%����"�����$�����"��dieView'
��������������� ���� 7�
M�������M������
� 4�����������@=����$��"�������%����(1��������������-��������'�!�������������������-������������$��������������������������"�����������������������������������������-��'
� ����%������������������"�-�%���������������%������-����������������-����"�������������'
��������������� ���� 7
M�������M������
� !���"�������%����"�����������������������������%����%�%��������������-�������������������"��$���������������������������'
� 0��������%���������������%��������"���������"��������-��'
� ���%�����"����������"��"���������$�����������$���)��1���*�����$�����"������'
��������������� ���� 7(
M�������M������
� ����������� – �������"����������%����%'�0��%����������������%����% �����������;���������"����� ���������"����������"�����'
� ����� – ��������������������"������������$�'
� � ������ – ��������������������"��������������$�'
��������������� ���� 7+
M�������M������
� ���� �– ���������������"������%������-��'��������"������������$� �������������%�������������������������������-�����������������"�����������'�����������%�����$�����"�������������������������������'
� � ���� �– .������������"�������������"�����'������������$������������%������������������������"�����'
��������������� ���� �3
��������������� ���� 73
M�������M������
� �������������������������� �����"�������%�������������"��������������$��"���'
� O����&���� �"������������%�������������%�����������&����������������'���$�-�������activate ���deactivate ��������%�������������
���������������"������'
��������������� ���� 75
M�������M������
� �$�������Rectangle ���Text�"#���������������$��"���������%��%����"��"������������������%�������������������������������������"��'
� <��2������%�����������������������"����������������������"�����������$��"���A�!����%���$���������� �%�����%�������������������������������;������'
��������������� ���� 76
M�������M������
� ���activate �%�����������"������������$��"���-�������������������-�������-��������"�����&��"��-'
� def activate(self):"Sets this button to 'active'. " self.label.setFill('black')self.rect.setWidth(2)self.active = True
� .����"�� �self ��������������"�������"#���'� !���������������%�����$������������;��self.label ��
������������Text �"#�������self.rect ������������"#���'
� Self.active ��������M��������������$��"�����������"���%������������������"������������������������$�'
��������������� ���� 77
M�������M������
� �������������deactivate ���$����
�������def deactivate(self):
"Sets this button to 'inactive'."self.label.setFill('darkgrey')self.rect.setWidth(1)self.active = 0
��������������� ���� 78
M�������M������
� <��’��%��-��������clicked ������'
� ����graphics ��-���������getMouse ��������������������%������������������"��������-��'
� ����������������������������"����������- ����%�����$���������������getMouse������������%�����"����� ������ �����������������������'
��������������� ���� 8�
M�������M������pt = win.getMouse()
if button1.clicked(pt):# Do button1 stuff
elif button2.clicked(pt):# Do button2 stuff
elif button3.clicked(pt):# Do button3 stuff
…
� ��������#�"��������clicked �����������������������%���������$��������������������������������"�����'
��������������� ���� �5
��������������� ���� 8�
M�������M������
� ������������������������"�������������&���� ���������������"��%���������&������& ���� $�������������������'
� �����%�����"����������������"�������"#�������������������&�$��������&���� ����������$��"���'
��������������� ���� 8
M�������M������� def clicked(self, p):
"RETURNS true if button active and p is inside“return self.active and \
self.xmin <= p.getX() <= self.xmax and \self.ymin <= p.getY() <= self.ymax
� O���������������������������True ����������������������M�������&��������������"������'
� ����������������������������������$��"�������%���������������������$��"��������-��'
� ����������������������������������������&���� $����������������������%������-�������"��%��������"�����������������������'
��������������� ���� 8(
M�������M������
� �������������������������������%���������������������def __init__(self, win, center, width, height, label):
""" Creates a rectangular button, eg:qb = Button(myWin, Point(30,25), 20, 10, 'Quit') """
w,h = width/2.0, height/2.0x,y = center.getX(), center.getY()self.xmax, self.xmin = x+w, x-wself.ymax, self.ymin = y+h, y-hp1 = Point(self.xmin, self.ymin)p2 = Point(self.xmax, self.ymax)self.rect = Rectangle(p1,p2)self.rect.setFill('lightgray')self.rect.draw(win)self.label = Text(center, label)self.label.draw(win)self.deactivate()
� M���������������������"�����$������������������ �%���� ���������'
��������������� ���� 8+
M�����������
� �������������������DieView �����������
��������������������$�����������'
� ������������������������/����������� ����������������������������������������'
� ���"����� �����DieView �����%�����$��
�����������������������'
��������������� ���� 83
M�����������
� ����������� – ���������������%����%'�0��%����������������%����% ���������������������������� ����������;�����������������������'
� � ����� – ���������$��%�������%���$���$���'�����$��������������%����"������������������'
��������������� ���� 85
M�����������
� ������ ������������������������%����"����������������������������������������������������������$��������������'
� !�������������������1������������� �����-������������������������������'�0������������������������� ����%����"������%��%�������-��������'
��������������� ���� �6
��������������� ���� 86
M�����������
� �������������%�����������$��������11 ������������������������������������������ ���������������������'
� ����������������%��������������"�-�������/�������������$����������'�setValue %����������������������
������������"�����������$��������������'
��������������� ���� 87
M�����������# dieview.py# A widget for displaying the value of a die
from graphics import *
class DieView:""" DieView is a widget that displays a graphical representationof a standard six-sided die."""
def __init__(self, win, center, size):"""Create a view of a die, e.g.:
d1 = GDie(myWin, Point(40,50), 20)creates a die centered at (40,50) having sidesof length 20."""
# first defind some standard valuesself.win = winself.background = "white" # color of die faceself.foreground = "black" # color of the pipsself.psize = 0.1 * size # radius of each piphsize = size / 2.0 # half of sizeoffset = 0.6 * hsize # distance from center to outer pip
��������������� ���� 88
M�����������# create a square for the facecx, cy = center.getX(), center.getY()p1 = Point(cx-hsize, cy-hsize)p2 = Point(cx+hsize, cy+hsize)rect = Rectangle(p1,p2)rect.draw(win)rect.setFill(self.background)
# Create 7 circles for standard pip locationsself.pip1 = self.__makePip(cx-offset, cy-offset)self.pip2 = self.__makePip(cx-offset, cy)self.pip3 = self.__makePip(cx-offset, cy+offset)self.pip4 = self.__makePip(cx, cy)self.pip5 = self.__makePip(cx+offset, cy-offset)self.pip6 = self.__makePip(cx+offset, cy)self.pip7 = self.__makePip(cx+offset, cy+offset)
self.setValue(1)
��������������� ���� ���
M�����������def __makePip(self, x, y):
"""Internal helper method to draw a pip at (x,y)"""pip = Circle(Point(x,y), self.psize)pip.setFill(self.background)pip.setOutline(self.background)pip.draw(self.win)return pip
def setValue(self, value):""" Set this die to display value."""# turn all pips offself.pip1.setFill(self.background)self.pip2.setFill(self.background)self.pip3.setFill(self.background)self.pip4.setFill(self.background)self.pip5.setFill(self.background)self.pip6.setFill(self.background)self.pip7.setFill(self.background)
��������������� ���� ���
M�����������# turn correct pips on
if value == 1:self.pip4.setFill(self.foreground)
elif value == 2:self.pip1.setFill(self.foreground)self.pip7.setFill(self.foreground)
elif value == 3:self.pip1.setFill(self.foreground)self.pip7.setFill(self.foreground) self.pip4.setFill(self.foreground)
elif value == 4:self.pip1.setFill(self.foreground)self.pip3.setFill(self.foreground)self.pip5.setFill(self.foreground)self.pip7.setFill(self.foreground)
elif value == 5:self.pip1.setFill(self.foreground)self.pip3.setFill(self.foreground)self.pip4.setFill(self.foreground)self.pip5.setFill(self.foreground)self.pip7.setFill(self.foreground)
else:self.pip1.setFill(self.foreground)self.pip2.setFill(self.foreground)self.pip3.setFill(self.foreground)self.pip5.setFill(self.foreground)self.pip6.setFill(self.foreground)self.pip7.setFill(self.foreground)
��������������� ���� ��
M�����������
� ����������������
� ������;���������������"������������������;�������������%�������������"���������������'
� 0��������������������$����������"����������������������������������������������������������������������������������%�������������������������%��%������������������������ ����������$����������������������%����������������������� �����������������������������'
��������������� ���� �7
��������������� ���� ��(
M�����������
� __makePip �������������������������%�
�������������$������������������'�������������������������%������DieView ���2����������������-�����������������'���2������������%����__ �����������������������
���F���$��G ����������������������������������"�����������������������'
��������������� ���� ��+
����4��������# roller.py# Graphics program to roll a pair of dice. Uses custom widgets# Button and GDie.
from random import randrangefrom graphics import GraphWin, Point
from button import Buttonfrom dieview import DieView
def main():
# create the application windowwin = GraphWin("Dice Roller")win.setCoords(0, 0, 10, 10)win.setBackground("green2")
# Draw the interface widgetsdie1 = DieView(win, Point(3,7), 2)die2 = DieView(win, Point(7,7), 2)rollButton = Button(win, Point(5,4.5), 6, 1, "Roll Dice")rollButton.activate()quitButton = Button(win, Point(5,1), 2, 1, "Quit")
��������������� ���� ��3
����4��������# Event loop
pt = win.getMouse()while not quitButton.clicked(pt):
if rollButton.clicked(pt):value1 = randrange(1,7)die1.setValue(value1)value2 = randrange(1,7)die2.setValue(value2)quitButton.activate()
pt = win.getMouse()
# close up shopwin.close()
main()
��������������� ���� ��5
����4��������
� ����$�����������������"�����"��������������%��DieView� ����%��Button�'
� ���������"���������������������$� �"�������/����"��������������$���'��������������������������������������������������'
� �����$���������������������������������������������-���������������������������������������-���������/����"�����'
��������������� ���� ��6
����4��������
� ����if %�������������������������������
��������������������%�����������������-�����������"�����'
� ����-�� ����������������������������"�������������������������������%������������������'