Python Turtle Mini Project Basketball Game with Source Code
Contents
Introduction
The Logo programming language is frequently linked to turtle graphics. In the late 1960s, Seymour Papert added turtle graphics support to Logo to support his version of the turtle robot, which is a simple robot controlled from the user’s workstation and designed to carry out the drawing functions assigned to it using a small retractable pen set into or attached to the robot’s body.
The standard library of the Python programming language now contains a Turtle graphics module. Turtle in Python, like its Logo ancestor, allows programmers to manipulate one or more turtles in a two-dimensional space.
Overview of the syntax
A location, an orientation (or direction), and a pen are the three qualities of the turtle. Color, width, and on/off state are all properties of the pen (also called down and up).
“Move ahead 10 spaces” and “turn left 90 degrees” are orders that the turtle responds to based on its current location. The turtle’s pen can also be managed by enabling it, changing its color, and adjusting its breadth. By visualizing what they would do if they were the turtle, a pupil may comprehend (and forecast and reason about) the turtle’s motion. This is referred to as “body syntonic” reasoning by Seymour Papert.
Basic syntax:
import turtle // start of the program
//body
//of the main
//code
turtle.done() //end of the program
Inorder to understand the codes to draw various shapes given below, Getting Started with Powerful yet Easy Python Graphics Module, Turtle.
Methods
A Python method is a label that can be applied to an object and is a piece of code that may be run on that object.
The most frequently, used turtle methods are:
Method | Parameter | Description |
---|---|---|
Turtle() | None | Creates and returns a new turtle object |
forward() | amount | Moves the turtle forward by the specified amount |
backward() | amount | Moves the turtle backward by the specified amount |
right() | angle | Turns the turtle clockwise |
left() | angle | Turns the turtle counterclockwise |
penup() | None | Picks up the turtle’s Pen |
pendown() | None | Puts down the turtle’s Pen |
up() | None | Picks up the turtle’s Pen |
down() | None | Puts down the turtle’s Pen |
color() | Color name | Changes the color of the turtle’s pen |
fillcolor() | Color name | Changes the color of the turtle will use to fill a polygon |
heading() | None | Returns the current heading |
position() | None | Returns the current position |
goto() | x, y | Move the turtle to position x,y |
begin_fill() | None | Remember the starting point for a filled polygon |
end_fill() | None | Close the polygon and fill with the current fill color |
dot() | None | Leave the dot at the current position |
stamp() | None | Leaves an impression of a turtle shape at the current location |
shape() | shapename | Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’ |
Basketball Game
The principles governs everything in every subject. They’re the basis on which all incredible exploits are built. To execute acts that appear magical to us simple humans, great scientists and artists equally rely on their most basic skills. Same is the case for python turtle module.
In order to make any games using python turtle, for this instance a basketball game, the fundamentals are mandatory. If you really think about it our basketball game is just a randomly moving .gif upon which if our ball strikes then score and shot is incremented by one and if missed only the shot is incremented.
Features
- Enter space to shoot.
- Changeable background color.
- Realistic hoop and ball.
- Scalable difficulty.
- Accuracy calculated.
- For every five shoot, two miss game finish.
Downloads
Download the .gif file given below:
Source Code
- The main function for our game to operate smoothly is given below. Other than that everything in our source code is self-explanatory.
def shoot():
for say in range(30):
y = ball.ycor()
y += 15 #ball speed
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()
pen.write("Score:{}".format(score1), font=("Verdana", 30, "bold"))
break
global shots1
ball.goto(0, -180)
shots1 += 1
pen2.clear()
pen2.write("Shots:{}".format(shots1), font=("Verdana", 30, "bold"))
if score1 > 0:
accuracy = score1/shots1*100
pen3.clear()
pen3.write("Accuracy: {:.2f}%" .format(accuracy), font=("Verdana", 30, "bold"))
if shots1 >= 5 and score1 <=2 :
accuracy = score1/shots1*100
pen3.clear()
pen3.goto(-320,0)
pen3.write("GAME OVER /w Acc: {:.2f}%" .format(accuracy), font=("Verdana", 30, "bold"))
turtle.done()
- Calling our main function:
def yay1():
shoot()
The following is the full source code for making a basket ball game using python turtle module:
import turtle
import random
import time
# Set up the screen
wn = turtle.Screen()
wn.setup(width =700, height =500)
wn.bgcolor("pink")
wn.tracer(0)
wn.title("Basketball Game by ABK for Follow Tutorials")
# basket turtle
basket = turtle.Turtle()
basket.showturtle()
basket.penup()
basket.goto(0, 150)
wn.register_shape("basketball_board.gif")
basket.shape("basketball_board.gif")
basket.color("red")
basket.shapesize(stretch_len=10, stretch_wid=5)
# ball turtle
ball = turtle.Turtle()
ball.penup()
ball.shape("circle")
ball.color("orange")
ball.shapesize(3)
ball.goto(0, -180)
# pen turtle
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-330, -230)
pen.write("Score:0", font=("Verdana", 30, "bold"))
# shots turtle
pen2 = turtle.Turtle()
pen2.penup()
pen2.hideturtle()
pen2.goto(100, -230)
pen2.write("Shots:0", font=("Verdana", 30, "bold"))
# accuracy turtle
pen3 = turtle.Turtle()
pen3.penup()
pen3.hideturtle()
pen3.goto(-330, 200)
pen3.write("Accuracy:0", font=("Verdana", 30, "bold"))
# Functions
def shoot():
for say in range(30):
y = ball.ycor()
y += 15 #ball speed
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()
pen.write("Score:{}".format(score1), font=("Verdana", 30, "bold"))
break
global shots1
ball.goto(0, -180)
shots1 += 1
pen2.clear()
pen2.write("Shots:{}".format(shots1), font=("Verdana", 30, "bold"))
if score1 > 0:
accuracy = score1/shots1*100
pen3.clear()
pen3.write("Accuracy: {:.2f}%" .format(accuracy), font=("Verdana", 30, "bold"))
if shots1 >= 5 and score1 <=2 :
accuracy = score1/shots1*100
pen3.clear()
pen3.goto(-320,0)
pen3.write("GAME OVER /w Acc: {:.2f}%" .format(accuracy), font=("Verdana", 30, "bold"))
turtle.done()
def yay1():
shoot()
# key bindings
wn.listen()
wn.onkeypress(yay1, "space")
score1 = 0
shots1 = 0
# main game loop
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
if basket.xcor() > 150:
basket.setx(150)
if basket.xcor() < -150:
basket.setx(-150)
turtle.done()
Output
The output for the code given above is:
You may also Like
- Galactic Flower Python Turtle Mini Project with Source Code
- Python Turtle Mini Project Pong Game
- Drawing Tally Marks using Python Turtle Module
- Drawing a Christmas Tree using Python Turtle Module
- Drawing a Sun using Python Turtle Module
- Drawing a Chessboard using Python Turtle Module
- Drawing a Snowman using Python Turtle Module
- Making a Tic-Tac-Toe Game Board using Python Turtle Module
- Drawing a Car using Python Turtle Module
- Drawing Awesome Spiral Shapes using Python Turtle Module
- Drawing Colored and Uncolored Spider Web using Python Turtle Module
- Drawing a Multi Color Hut using Python Turtle Module
- Drawing Olympics and Audi logo with Python Turtle Module
- Drawing Circle Spirograph using turtle in Python
- Drawing a Star, Hippie Flower and Astro Flower with Python Turtle module