Animation Using Turtle Grapics

Updated jib 1 Tallied Votes 6K Views Share

• 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.

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()

Dani AI

Generated

A common pitfall with turtle games is using a blocking while-loop, which prevents click events from firing. Drive your animation with the event loop instead: disable automatic drawing with tracer(0), schedule frames using ontimer, and call update() once per frame. Use Turtle.onclick on the shooter so the click handler runs even while the animation is active. For collision detection, Turtle.distance between the bullet and ball is sufficient; a threshold near the default shape size (about 20) works well. The official docs for tracer, ontimer, onclick, and distance explain these APIs.

Here is a compact, runnable pattern that ties everything together and avoids globals by storing state in a dict:

import turtle as t, random

screen = t.Screen()
screen.setup(640, 480)
screen.title("Turtle Shooter")
screen.tracer(0)

half_w = screen.window_width() // 2 - 10
half_h = screen.window_height() // 2 - 10

ball = t.Turtle("circle"); ball.color("blue"); ball.penup()
ball.goto(random.randint(-half_w, half_w), random.randint(0, half_h))
ball.dx = 5; ball.dy = 5

shooter = t.Turtle("square"); shooter.color("red"); shooter.penup()
shooter.goto(0, -half_h + 20)

bullet = t.Turtle("circle"); bullet.color("black"); bullet.penup(); bullet.hideturtle()
state = {"bullet_moving": False, "running": True}
msg = t.Turtle(visible=False); msg.penup(); msg.goto(0, 0)

def fire(x=None, y=None):
    if not state["bullet_moving"]:
        bullet.goto(shooter.xcor(), shooter.ycor() + 20)
        bullet.showturtle()
        state["bullet_moving"] = True

shooter.onclick(fire)

def step():
    if not state["running"]:
        return
    ball.setx(ball.xcor() + ball.dx); ball.sety(ball.ycor() + ball.dy)
    if abs(ball.xcor()) > half_w: ball.setx(max(min(ball.xcor(), half_w), -half_w)); ball.dx *= -1
    if abs(ball.ycor()) > half_h: ball.sety(max(min(ball.ycor(), half_h), -half_h)); ball.dy *= -1
    if state["bullet_moving"]:
        bullet.sety(bullet.ycor() + 12)
        if bullet.ycor() > half_h:
            bullet.hideturtle(); state["bullet_moving"] = False
    if state["bullet_moving"] and bullet.distance(ball) < 20:
        msg.write("You win!", align="center", font=("Arial", 16, "bold"))
        state["running"] = False
    screen.update()
    if state["running"]: screen.ontimer(step, 20)

screen.listen(); step(); screen.mainloop()

Tip: compute boundaries from window_width()/window_height() so your bounce logic remains correct if the window size changes.

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.