import time
import math
from graphics import *
MISS = 0
BULLSEYE = 1
RING1 = 2
RING2 = 3
RING3 = 4
RING4 = 5
TARGET_CENTER = Point(100, 75)
RADIUS = [0, 15, 30, 45, 60, 75, 90]
RADIUS_COLOR = ['black', 'red', 'white', 'blue', 'white', 'red']
#... XXXXXXXXXX1 ......................................
# The animated a
ow is missing the target. Help me to adjust
# the code between the lines below so that the a
ow hits the target!
def main():
'''
Main Program to draw a target and a
ow onto a window
'''
# Build the window with the title and size specified
win = GraphWin('Target', 200, 150)
#-------------------------------------
a
owLocation = Point(200, 75)
for a
owX in range(int(a
owLocation.getX()), -1, -10):
XXXXXXXXXXbackground = Rectangle(Point(0, 0), Point(win.getWidth(), win.getHeight()))
XXXXXXXXXXbackground.setFill('light grey')
XXXXXXXXXXbackground.draw(win)
XXXXXXXXXXdrawTarget(win, MISS)
XXXXXXXXXXa
owLocation = Point (a
owX, a
owLocation.getY())
XXXXXXXXXXdrawA
ow(win, a
owLocation)
XXXXXXXXXXtime.sleep(1/30)
#-------------------------------------
#Draw it one list time to reflect where it hit
drawTarget(win, determineHit(a
owLocation))
drawA
ow(win, a
owLocation)
#..... XXXXXXXXXX2 .........................
# Replace the code between the lines with loop instead of the if/else logic
def determineHit(strikePoint):
'''
Determine where on the target the strike points hit
strikePoint - the impact of the a
ow
returns one of the constants above
'''
x = strikePoint.getX()
y = strikePoint.getY()
distance = math.sqrt((TARGET_CENTER.getX() - x)**2 + (TARGET_CENTER.getY() - y)**2)
#-------------------------------------
if distance < RADIUS[BULLSEYE]:
XXXXXXXXXXreturn BULLSEYE
elif distance < RADIUS[RING1]:
XXXXXXXXXXreturn RING1
elif distance < RADIUS[RING2]:
XXXXXXXXXXreturn RING2
elif distance < RADIUS[RING3]:
XXXXXXXXXXreturn RING3
elif distance < RADIUS[RING4]:
XXXXXXXXXXreturn RING4
else:
XXXXXXXXXXreturn MISS
#-------------------------------------
#..... XXXXXXXXXX3 .........................
# We can refactor the code we wrote last time to make it more flexible.
# Rewrite the drawTarget function to use a loop instead of a long if/else chain
def drawTarget(win, hit):
'''
Draw a ringed target onto the provided window
win - the target window for the a
ow
hit - the index of the ring that was hit (using constants above)
'''
ring4 = Circle(TARGET_CENTER, RADIUS[RING4])
if hit == RING4:
XXXXXXXXXXring4.setFill("green")
else:
XXXXXXXXXXring4.setFill("red")
ring4.draw(win)
ring3 = Circle(TARGET_CENTER, RADIUS[RING3])
if hit == RING3:
XXXXXXXXXXring3.setFill("green")
else:
XXXXXXXXXXring3.setFill("white")
ring3.draw(win)
ring2 = Circle(TARGET_CENTER, RADIUS[RING2])
if hit == RING2:
XXXXXXXXXXring2.setFill("green")
else:
XXXXXXXXXXring2.setFill("blue")
ring2.draw(win)
ring1 = Circle(TARGET_CENTER, RADIUS[RING1])
if hit == RING1:
XXXXXXXXXXring1.setFill("green")
else:
XXXXXXXXXXring1.setFill("white")
ring1.draw(win)
bullseye = Circle(TARGET_CENTER, RADIUS[BULLSEYE])
if hit == BULLSEYE:
XXXXXXXXXXbullseye.setFill("green")
else:
XXXXXXXXXXbullseye.setFill("red")
bullseye.draw(win)
def drawA
ow(win, startPoint):
'''
Draw an a
ow onto the provided window
win - the target window for the a
ow
startPoint - where to draw the tip of the a
ow
'''
x = startPoint.getX()
y = startPoint.getY()
head = Polygon(Point(x, y), Point(x + 13, y - 5), Point(x + 13, y + 5))
head.setFill("grey")
head.draw(win)
shaft = Line(Point(x + 60, y), Point(x + 13, y))
shaft.setWidth(3)
shaft.setOutline("
own")
shaft.draw(win)
fletching = Polygon(Point(x + 60, y), Point(x + 65, y - 5), Point(x + 85, y - 5),
XXXXXXXXXXPoint(x + 80, y), Point(x + 85, y + 5), Point(x + 65, y + 5))
fletching.setFill("yellow")
fletching.draw(win)
main()
def total(values):
'''
Add up all the numbers in the list
values - a list of numbers
returns the total
'''
return 0
def mean(values):
'''
Compute the mean of the values in the list
values - a list of numbers
returns the mean
'''
return 0
def count(values, find):
'''
Count the number of occurances of the provided number within the list
values - a list of numbers
find - the number to count
returns the number of instances
'''
return 0
def median(values):
'''
Compute the median value of the list
values - a list of numbers
'''
# The median is the 'middle value' of the list
# If the list is odd, it is that value directly
# If the list is even, it is the average of the middle two values
# Hint: to sort your list, use the line
# values.sort()
return 0
def largest(values):
'''
Find the largest value in the list
values - a list of numbers
'''
return 0
def smallest(values):
'''
Find the smallest value in the list
values - a list of numbers
'''
return 0
def tenTimes(values):
'''
See if any value in this list is 10 times greater than another value
in the list, but watch out for 0!
values - a list of numbers
'''
# Hint: The wrong logic will make any value look 10 times zero
return False
#=======================================================================
# Do not edit anything below here
def checkEquals(check, expected, inputList, message):
if check != expected:
XXXXXXXXXXprint(message, "your code returned ", check, " but should have returned", expected, "for input", inputList)
XXXXXXXXXXreturn False
return True
def tester():
totalStatus = True;
basic = [2, 5, 10, 15]
negative = [0, -5, -10]
mixed = [-5, -1, 0, 1, 5]
empty = []
testE
or = "total() failed:"
status = True
status &= checkEquals(total(basic), 32, basic, testE
or)
status &= checkEquals(total(negative), -15, negative, testE
or)
status &= checkEquals(total(mixed), 0, mixed, testE
or)
status &= checkEquals(total(empty), 0, empty, testE
or)
totalStatus = totalStatus and status
print("total() ", "passed" if status else "failed")
testE
or = "mean()"
status = True
status &= checkEquals(mean(basic), 8.0, basic, testE
or)
status &= checkEquals(mean(negative), -5.0, negative, testE
or)
status &= checkEquals(mean(mixed), 0.0, mixed, testE
or)
status &= checkEquals(mean(empty), 0.0, empty, testE
or)
totalStatus = totalStatus and status
print("mean() ", "passed" if status else "failed")
testE
or = "count()"
status = True
multiple = [0, 5, 2, 3, 5, 2, -5, 5]
status &= checkEquals(count(basic, 10), 1, basic, testE
or)
status &= checkEquals(count(negative, 7), 0, multiple, testE
or)
status &= checkEquals(count(multiple, 5), 3, multiple, testE
or)
status &= checkEquals(count(empty, 0), 0,