Professional Documents
Culture Documents
Basketball Code1
Basketball Code1
3 (Windows)
# import modules
import turtle
import random
import time
wn = turtle.Screen()
wn.setup(700,500)
wn.colormode(255)
wn.tracer(0)
# basket turtle
basket = turtle.Turtle()
basket.showturtle()
basket.penup()
basket.goto(0, 150)
wn.register_shape("insert_your_own_image.gif")
basket.shape("insert_your_own_image.gif")
# ball turtle
ball = turtle.Turtle()
ball.penup()
ball.shape("circle")
ball.color("orange")
ball.shapesize(2.5)
ball.goto(0, -180)
# pen turtle
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-330, -230)
# shots turtle
pen2 = turtle.Turtle()
pen2.penup()
pen2.hideturtle()
pen2.goto(100, -230)
# accuracy turtle
pen3 = turtle.Turtle()
pen3.penup()
pen3.hideturtle()
pen3.goto(-330, 200)
def shoot():
y = ball.ycor()
y += 15
ball.sety(y)
time.sleep(0.02)
wn.update()
# Collision check
if (ball.xcor() < basket.xcor() + 30) and (ball.xcor() > basket.xcor() - 30) and (ball.ycor() ==
basket.ycor()):
global score1
score1 += 1
ball.sety(120)
wn.update()
time.sleep(0.02)
ball.sety(80)
wn.update()
pen.clear()
break
global shots1
ball.goto(0, -180)
shots1 += 1
pen2.clear()
if score1 > 0:
accuracy = score1/shots1*100
pen3.clear()
def yay1():
shoot()
# key bindings
wn.listen()
wn.onkeypress(yay1, "s")
score1 = 0
shots1 = 0
while True:
wn.update()
# hoop movement
x = random.randint(1, 150)
basket.setx(basket.xcor() + x)
time.sleep(0.1)
x = random.randint(1, 150)
time.sleep(0.1)
basket.setx(basket.xcor() - x)
# border checking
basket.setx(220)
basket.setx(-220)