• shooter_clicked(x,y): This function is called when the shooter turtle is clicked on the screen. If the bullet not already moving, this function sets a global flag to cause the bullet to start moving upward.
• Set the window dimensions and title. Call setup() to draw the window.
• Set random x and y coordinates for the ball using random.randrange().
• Create the ball as a new blue, circular turtle. Set penup(), and move the ball to its starting location in the window.
• Set the x and y movement increments for the ball to 5.
• Draw the shooter image on the screen with the “shooter” turtle. Make it a red square positioned at the bottom middle of the screen.
• Create the “bullet” turtle as a black circle, and draw it on the screen sitting on top of the shooter turtle.
• Set the bullet_moving flag to False to indicate that the bullet is not moving yet.
Inside the while-loop you can use the bouncing ball code from bouncing_ball.py. Then add code to the while-loop to do the following:
• If the bullet is also moving, move it to its new position.
• If the bullet is close to the moving ball (use turtle.distance() to check the distance), end the game and the while-loop.
• Congratulate the player for winning the game.
Animation Using Turtle Grapics
from turtle import *
from random import *
import winsound
import math
def shooter_clicked(clickx,clicky):
global start
start=True
return
setup(600,500)
maxx = 300
maxy = 250
minx= -maxx
miny= - maxy
title ("shooting ball")
bgcolor('grey')
ball=Turtle()
ball.penup()
ball.shape("circle")
ball.shapesize(3,3,3)
ball.color("blue")
bounce_point = 20
shooter=Turtle()
shooter.hideturtle()
shooter.penup()
shooter.shape("square")
shooter.shapesize(3,3,3)
shooter.color("red")
shooter.goto(-10,-220)
bullet=Turtle()
bullet.hideturtle()
bullet.penup()
bullet.shape("circle")
bullet.shapesize(1,1,1)
bullet.goto(-20,-100)
shooter.onclick(shooter_clicked)
x = randint(minx + bounce_point, maxx + bounce_point)
y = randint(miny + bounce_point, maxy + bounce_point)
ball.goto(x,y)
ball.showturtle()
bullet.showturtle()
shooter.showturtle()
dx = 10
dy = 10
bdy=30
by=-140
endloop = False
start=False
while not endloop:
xx = x + dx
if xx < minx+bounce_point:
xx = minx + bounce_point
dx = -dx
if xx > maxx - bounce_point:
xx = maxx- bounce_point
dx = -dx
yy=y+dy
if yy < miny + bounce_point:
yy= miny + bounce_point
dy = -dy
if yy > maxy + bounce_point:
yy = maxy + bounce_point
dy = -dy
x = xx
y = yy
ball.goto(x,y)
shooter.goto(-by,-bdy)
if start==True:
by=by+bdy
if by>maxy:
by=-220
start= False
if bullet.distance(ball)<40:
print"target affirmed"
print"You win!!!!!!"
endloop=True
start=False
bullet.goto(0,by)
bye()
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.