In: Computer Science
Without using the graphics module, write a program to simulate an archery score tracker. Your program should use a Tkinter canvas to draw a target. Your program should allow the user to click the target to mark where their arrow landed on the target (or off).
When the user clicks the target, the program should draw a dot at that location and compute the score. Scores for each ring are as follows: yellow 5, red 4, blue 3, black 2, and white 1. Use Tkinter labels to display the list scores for each arrow, along with the total score. Also, provide a Quit button that closes the window.
ODE:-
import tkinter
import math
from graphics import *
def square(x):
return x * x
def distance(p1, p2):
dist = math.sqrt(square(p2.getX() - p1.getX())
+ square(p2.getY() - p1.getY()))
return dist
win = GraphWin("Archery",400,400)
center = Point(200,200)
color = ["white","green","blue","red","yellow"]
radius = 100;
for i in range(0,5):
cir = Circle(center, radius)
cir.draw(win)
cir.setFill(color[i])
radius -=20
message = Text(Point(80, 20), "Total Score : 0")
message.draw(win)
message2 = Text(Point(80, 40), "")
message2.draw(win)
totalScore = 0;
for i in range(0,5):
pt = win.getMouse()
cir = Circle(pt, 5)
cir.draw(win)
cir.setFill("brown")
dist = distance(center, pt)
if(dist<=20):
score = 9
elif(dist<=40):
score = 7
elif(dist<=60):
score = 5
elif(dist<=80):
score = 3
elif(dist<=100):
score = 1
else:
score = 0
message2.setText("Hit Score : "+str(score))
totalScore+=score
message.setText("Total Score : "+str(totalScore))
win.getMouse()
win.close()
code snippet::
output: