Python Mini Project Football Game with Source Code

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:

MethodParameterDescription
Turtle()NoneCreates and returns a new turtle object
forward()amountMoves the turtle forward by the specified amount
backward()amountMoves the turtle backward by the specified amount
right()angleTurns the turtle clockwise
left()angleTurns the turtle counterclockwise
penup()NonePicks up the turtle’s Pen
pendown()NonePuts down the turtle’s Pen
up()NonePicks up the turtle’s Pen
down()NonePuts down the turtle’s Pen
color()Color nameChanges the color of the turtle’s pen
fillcolor()Color nameChanges the color of the turtle will use to fill a polygon
heading()NoneReturns the current heading
position()NoneReturns the current position
goto()x, yMove the turtle to position x,y
begin_fill()NoneRemember the starting point for a filled polygon
end_fill()NoneClose the polygon and fill with the current fill color
dot()NoneLeave the dot at the current position
stamp()NoneLeaves an impression of a turtle shape at the current location
shape()shapenameShould be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’

Football 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 football game, the fundamentals are mandatory. If you really think about it our football 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 kick.
  • Changeable background color.
  • Realistic goal post 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 kick():
    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() < post.xcor() + 30) and (ball.xcor() > post.xcor() - 30) and (ball.ycor() == post.ycor()):
            global goal1
            goal1 += 1
            ball.sety(120)
            wn.update()
            time.sleep(0.02)
            ball.sety(80)
            wn.update()
            pen.clear()
            pen.write("Goal:{}".format(goal1), font=("Verdana", 30, "bold"))
            break
    global kicks1
    ball.goto(0, -180)
    kicks1 += 1
    pen2.clear()
    pen2.write("Kicks:{}".format(kicks1), font=("Verdana", 30, "bold"))
    if goal1 > 0:
        accuracy = goal1/kicks1*100
        pen3.clear()
        pen3.write("Accuracy: {:.2f}%" .format(accuracy), font=("Verdana", 30, "bold")) 
    
    if kicks1 >= 5 and goal1 <=2 :
        accuracy = goal1/kicks1*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():
    kick()

The following is the full source code for making a football 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("green")
wn.tracer(0)
wn.title("Football Game by ABK for Follow Tutorials")

# post turtle
post = turtle.Turtle()
post.showturtle()
post.penup()
post.goto(0, 150)
wn.register_shape("goal_post1.gif")
post.shape("goal_post1.gif")
post.color("red")
post.shapesize(stretch_len=10, stretch_wid=5)


# ball turtle
ball = turtle.Turtle()
ball.penup()
ball.shape("circle")
ball.color("black")
ball.shapesize(2)
ball.goto(0, -180)

# pen turtle
pen = turtle.Turtle()
pen.hideturtle()
pen.penup()
pen.goto(-330, -230)
pen.write("Goal:0", font=("Verdana", 30, "bold"))

# kicks turtle
pen2 = turtle.Turtle()
pen2.penup()
pen2.hideturtle()
pen2.goto(100, -230)
pen2.write("Kicks: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 kick():
    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() < post.xcor() + 30) and (ball.xcor() > post.xcor() - 30) and (ball.ycor() == post.ycor()):
            global goal1
            goal1 += 1
            ball.sety(120)
            wn.update()
            time.sleep(0.02)
            ball.sety(80)
            wn.update()
            pen.clear()
            pen.write("Goal:{}".format(goal1), font=("Verdana", 30, "bold"))
            break
    global kicks1
    ball.goto(0, -180)
    kicks1 += 1
    pen2.clear()
    pen2.write("Kicks:{}".format(kicks1), font=("Verdana", 30, "bold"))
    if goal1 > 0:
        accuracy = goal1/kicks1*100
        pen3.clear()
        pen3.write("Accuracy: {:.2f}%" .format(accuracy), font=("Verdana", 30, "bold")) 
    
    if kicks1 >= 5 and goal1 <=2 :
        accuracy = goal1/kicks1*100
        pen3.clear()        
        pen3.goto(-320,0)
        pen3.write("GAME OVER /w Acc: {:.2f}%" .format(accuracy), font=("Verdana", 30, "bold"))
        turtle.done()

def yay1():
    kick()


# key bindings
wn.listen()
wn.onkeypress(yay1, "space")

goal1 = 0
kicks1 = 0


# main game loop
while True:
    wn.update()

    # hoop movement
    x = random.randint(1, 150)
    post.setx(post.xcor() + x)
    time.sleep(0.1)
    x = random.randint(1, 150)
    time.sleep(0.1)
    post.setx(post.xcor() - x)

    # border checking
    if post.xcor() > 150:
        post.setx(150)

    if post.xcor() < -150:
        post.setx(-150)

turtle.done()

Output

The output for the code given above is:

You may also Like

SHARE Python Mini Project Football Game with Source Code

You may also like...

Leave a Reply

Your email address will not be published.

Share