Hello there,

I am trying to calculate the shot, the value, and the score in my Archery Target Program. But I have no such luck. Would anyone be able to help me?

Recommended Answers

All 4 Replies

I tried to run this locally but I do not have the package graphics and pip couldn't find it either. So I found something that said graphics.py had to be in the same folder as your app. I found a copy of it on my computer and copied it over. Then it complained about not having cairo.py so I found that and copied it in from wxdemo. Then it complained about something else.

You'll have to provide more information on your setup.

Also, your inline comments are of dubious help.

OK. Found my problem. I was using the wrong graphics.py. Once I had the correct one I was able to run the code. First suggestion is learn how to use loops instead of replicating the code. You can replace

p1 = win.getMouse()
p1.draw(win)

distance = sqrt((p1.getX() ** 2  + (p1.getY())  ** 2))        

p2 = win.getMouse()
p2.draw(win)

distance = sqrt((p2.getX() ** 2  + (p2.getY())  ** 2))

p3 = win.getMouse()
p3.draw(win)

distance = sqrt((p3.getX() ** 2  + (p3.getY())  ** 2))

p4 = win.getMouse()
p4.draw(win)

distance = sqrt((p4.getX() ** 2  + (p4.getY())  ** 2))

p5 = win.getMouse()
p5.draw(win)

distance = sqrt((p5.getX() ** 2  + (p5.getY())  ** 2))

with

for i in range(5):
    pt = win.getMouse()
    pt.draw(win)
    distance = sqrt((pt.getX() ** 2  + (pt.getY())  ** 2))

and you should be calculating the cumulative score after you calculate the distance. You should have code to redisplay the cumulative values inside the loop. It would look something like

for i in range(5):
    get next shot
    calculate distance from centre
    calculate score based on distance
    redisplay shot #, score for current shot, and cumulative score

As a further suggestion, I find it helps to write code as pseudo code, turn that pseudo code into comments, then add lines of code after each comment.

# Open a graphics windows object

# Initialize game variables

# Create game board

# Process five shots

    # Get a shot

    # Calculate distance from centre and score

    # Update display with current and cumulative values

# Pause for user to end game

From there you get

from graphics import *
from math import *

# Open a graphics windows object

screenWidth = 401  
screenHeight = 401
title = "Archery Scoring Program"
win = GraphWin(title,screenWidth,screenHeight)
gridSize = 10
win.setCoords(-7.0,-7.0, 7.0, 7.0)

# Initialize game variables

value = 0
score = 0

# Create game board of concentric coloured circles

circle = Circle(Point(0,0), 5)
circle.setFill("white")
circle.draw(win)

circle = Circle(Point(0,0), 4)
circle.setFill("black")
circle.draw(win)

circle = Circle(Point(0,0), 3)
circle.setFill("blue")
circle.draw(win)

circle = Circle(Point(0,0), 2)
circle.setFill("red")
circle.draw(win)

circle = Circle(Point(0,0), 1)
circle.setFill("yellow")
circle.draw(win)     

# Process five shots

for shot in range(1, 6):

    # Get a shot

    pt = win.getMouse()
    pt.draw(win)

    # Calculate distance from centre and score

    distance = sqrt((pt.getX() ** 2  + (pt.getY())  ** 2))

    if distance <= 1.0:
        current = 5
    elif distance <= 2.0:
        current = 4
    elif distance <= 3.0:
        current = 3
    elif distance <= 4.0:
        current = 2
    elif distance <= 5:
        current = 1

    score += current

    # Update display with current and cumulative values

    shotText = Text(Point(0,5.75), "Shot " + str (shot) + " - Value " + str(current))
    ScoringText = Text(Point(0,-5.75), "Score = " + str(score))
    shotText.draw(win)
    ScoringText.draw(win)
    message = Text(Point(0,-6.50), "")
    message.draw(win)

# Pause for user to click to end game

message.setText ("Click Mouse to Quit")
pt = win.getMouse() # getMouse() returns a point object
win.close() # close window

You should replace your literals with consts and put those consts at the top of your code (documented as to function). As a final iteration I get (minus a little explanation of the consts)

"""
Name:

    Archery.py

Description:

    Displays an archery target and accepts five left mouse clicks
    to represent five shots. Calculates a score for each shot, and
    a cumulative score.

Requirements:

    graphics.py to be in the same folder as this code. graphics.py
    can be downloaded from

    https://mcsp.wartburg.edu/zelle/python/graphics.py

Audit:

    2022-10-21  original code
"""

from graphics import *
from math import *

TITLE  = "Archery Scoring Program"
WIDTH  = 401
HEIGHT = 401

# Open a graphics windows object

win = GraphWin(TITLE, WIDTH, HEIGHT)
win.setCoords(-7.0,-7.0, 7.0, 7.0)

# Initialize game variables

total_score = 0

# Create game board of five concentric coloured circles

circles = (
    ("white" , 5),
    ("black" , 4),
    ("blue"  , 3),
    ("red"   , 2),
    ("yellow", 1)
)

# Draw circles

for c in circles:
    circle = Circle(Point(0,0), c[1])
    circle.setFill(c[0])
    circle.draw(win)

# Initialize text areas

txt_newscore = Text(Point(0,  5.75), '')
txt_newscore.draw(win)

txt_cumscore = Text(Point(0, -5.75), '')
txt_cumscore.draw(win)

txt_message  = Text(Point(0, -6.50), '')
txt_message.draw(win)

# Process five shots

for shot in range(1, 6):

    # Get a shot

    pt = win.getMouse()
    pt.draw(win)

    # Calculate distance from centre, and score

    distance = sqrt((pt.getX() ** 2  + (pt.getY())  ** 2))

    if distance <= 1.0:
        score = 5
    elif distance <= 2.0:
        score = 4
    elif distance <= 3.0:
        score = 3
    elif distance <= 4.0:
        score = 2
    elif distance <= 5:
        score = 1
    else:
        score = 0

    total_score += score

    # Update display with current and cumulative values

    txt_newscore.setText(f'Shot {shot} - Value {score}')
    txt_cumscore.setText(f'Score = {total_score}')

# Pause for user to click to end game

txt_message.setText ("Click Mouse to Quit")
pt = win.getMouse()
win.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.