Archery Target

jib 1 Tallied Votes 4K Views Share

I've already povided the tutorials on how to do it above

###################################
# PROLOG SECTION
# archery_target.py
# Draws an archery target of three concentric circles using Turtle Graphics
# and then an animated arrow hitting the target.
#
# Tested with Python 2.6.1
# May 16, 2010
#
# 
###################################

###################################
# ENVIRONMENT SETUP SECTION
# Imports: turtle, time
###################################

# import turtle drawing routines
from turtle import *

# import the time routines
from time import sleep

###################################
# FUNCTION DEFINITION SECTION
# Definitions: draw_target(),
# erase_target(), move_arrow()
###################################

#
# define a function to draw a target at a location
# on the screen.
#
def draw_target(turtle,targetx,targety):
    turtle.speed("fast")
    #
    # draw the largest red circle
    #
    
    # lift the pen up (away from the window) so it doesn't draw a line as it moves
    turtle.up()
    
    #
    # move to the starting location
    #
    turtle.goto(targetx,targety+50)
    
    # the radius of the red circle is 150px
    turtle.sety(targety-150)
    # put the pen down (against the window) so it can begin to draw
    turtle.down()
    # set the color
    turtle.color("red")
    # set the pen width
    turtle.width(2)
    # call fill(1) before drawing the path
    turtle.fill(1)
    # draw the path
    turtle.circle(150)
    # call fill(0) after drawing the path
    turtle.fill(0)

    #
    # draw the middle blue circle
    #
    turtle.up()
    # the radius of the blue circle is 100px
    turtle.sety(targety-100)
    turtle.down()
    turtle.color("blue")
    turtle.fill(1)
    turtle.circle(100)
    turtle.fill(0)

    #
    # draw the green circle (the bulls-eye)
    #
    turtle.up()
    # the radius of the green circle is 50px
    turtle.sety(targety-50)
    turtle.down()
    turtle.color("green")
    turtle.fill(1)
    turtle.circle(50)
    turtle.fill(0)
    turtle.hideturtle()

    return

#
# define a function to erase a target at a location
# on the screen.
#
def erase_target(turtle,targetx,targety):
    sleep(1)
    
    # the radius of the circle is 150px
    #
    # move to the starting location
    #
    turtle.goto(targetx,targety-150)
    # put the pen down (against the window) so it can begin to draw
    turtle.down()
    # set the color for erasing the target
    turtle.color("white")
    # set the pen width
    turtle.width(2)
    # call fill(1) before drawing the path
    turtle.fill(1)
    # draw the path
    turtle.circle(150)
    # call fill(0) after drawing the path
    turtle.fill(0)
#
# define a function to move the arrow to the target
#
def move_arrow(arrow, ax, ay, targetx, targety):
    
    arrow.goto(ax,ay)
    arrow.setheading(arrow.towards(targetx,targety))
    arrow.showturtle()
    sleep(1)

    #
    # calculate steps to move the arrow to the center
    # of the target.
    #
    steps = 100
    dx = (targetx-ax)/float(steps)
    dy = (targety-ay)/float(steps)

    #
    # Move the arrow to the center of the target
    #
    for x in range(steps):
        ax = ax + dx
        ay = ay + dy
        arrow.goto(ax,ay)
        
    return

###################################
# PROCESSING INITIALIZATION SECTION
###################################

# create the drawing window
setup()
# create a title for the drawing window
title("archery target program")

# create a turtle to draw the target with
turtle = Turtle()

# set the location of the center of the target
targetx = 0
targety = 0

#
# Create an arrow as a turtle shape 
#
arrow = Turtle()
arrow.hideturtle()
arrow.penup()
# create an arrow shape
register_shape("myarrow",((-6,-96),(-6,0),(-18,0),(0,18),(18,0),(6,0),(6,-96),(-6,-96)))
arrow.shape("myarrow")

###################################
# PROCESSING SECTION
# Call functions to draw the target,
# move the arrow, and erase the target.
# 
###################################

# draw the target
draw_target(turtle,targetx,targety)

# now move the arrow to hit the target
ax = -200
ay = 0
move_arrow(arrow, ax, ay, targetx, targety)

# erase the target and draw a new one
erase_target(turtle,targetx,targety)

# set a new target location
targetx = 100
targety = 100
# draw the target
draw_target(turtle,targetx,targety)

# move the arrow to hit the target
move_arrow(arrow, ax, ay, targetx, targety)

###################################   
# CLEANUP, TERMINATION, AND EXIT SECTION
###################################

done()
TrustyTony 888 ex-Moderator Team Colleague Featured Poster

What is your question?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Code Snippets do not have questions associated. They are finished, working code.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

Ok, but what I can do with this code?

What means:
I've already povided the tutorials on how to do it above

Code snippet need description for which use it is for.

jib -2 Junior Poster

Sorry guys! forgive my mistake. This code is desdigned to help those people who have difficulty moving increments.

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.