Egg-Catcher using Python

In this article let’s see how to create an egg catcher game using Python.

Introduction:

To create an eggs catcher game using Python, our first step will be to design a ground, basket, and eggs.

what is an egg catcher game?

Eggs Catcher is a classic game where the goal is to catch as many eggs as possible. whenever we catch an egg then our score increases and if we miss three eggs then we will lose the game.

Steps involved in an egg catcher game?

firstly, we have to create new eggs.

Secondly, we have to check if the catcher has caught an egg.

Thirdly, we have to move eggs and check if the eggs touched the ground

Outline:

To run this code we need to import three modules. we can simply create this game we have to know a little bit about the usage of these modules in this code.

Project Prerequisites:

1. tkinter:

It is a module in Python, it is used and the most basic GUI framework is used in python programming. tkinter provides a powerful object-oriented interface to the Tk GUI toolkit.

2. Itertools:

It is a module in Python, it is used to iterate over data structures that can be stepped over using a for-loop. Such data structures are also known as iterables. This module works as a fast, memory-efficient tool that is used either by itself or in combination to form iterator algebra.

3. random:

It is a module in Python,randint(), which Returns a random number between the given range; choice(), which Returns a random element from the given sequence.

Code Implementation:

In this way, we have to import these modules.

from itertools import cycle

See also  Machine Learning Free E-book

from random import randrange

from tkinter import Canvas, Tk, messagebox, font

from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font

we have to set the height and width for the rectangle shape we can create this rectangle with the help of the canvas method in Tkinter.

In this way, we have to set the height and weight of the egg.

canvas_width = 800

canvas_height = 400

root = Tk()

root.title(“Egg Catcher”)

c = Canvas(root, width=canvas_width, height=canvas_height, background=”deep sky blue”)

c.create_rectangle(-5, canvas_height-100, canvas_width+5, canvas_height+5, fill=”sea green”, width=0)

c.create_oval(-80, -80, 120, 120, fill=’orange’, width=0)

c.pack()

color_cycle = cycle([“light blue”, “light green”, “light pink”, “light yellow”, “light cyan”])

egg_width = 45

egg_height = 55

egg_score = 10

egg_speed = 500

egg_interval = 4000

difficulty = 0.95

catcher_color = “blue”

catcher_width = 100

catcher_height = 100

catcher_startx = canvas_width / 2 – catcher_width / 2

catcher_starty = canvas_height – catcher_height – 20

catcher_startx2 = catcher_startx + catcher_width

catcher_starty2 = catcher_starty + catcher_height

catcher = c.create_arc(catcher_startx, catcher_starty, catcher_startx2, catcher_starty2, start=200, extent=140, style=”arc”, outline=catcher_color, width=3)

game_font = font.nametofont(“TkFixedFont”)

game_font.config(size=18)

score = 0

score_text = c.create_text(10, 10, anchor=”nw”, font=game_font, fill=”darkblue”, text=”Score: “+ str(score))

lives_remaining = 3

lives_text = c.create_text(canvas_width-10, 10, anchor=”ne”, font=game_font, fill=”darkblue”, text=”Lives: “+ str(lives_remaining))

eggs = []

root = Tk()
root.title("Egg Catcher")
c = Canvas(root, width=canvas_width, height=canvas_height, background="deep sky blue")
c.create_rectangle(-5, canvas_height-100, canvas_width+5, canvas_height+5, fill="sea green", width=0)
c.create_oval(-80, -80, 120, 120, fill='orange', width=0)
c.pack()

color_cycle = cycle(["light blue", "light green", "light pink", "light yellow", "light cyan"])
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 500
egg_interval = 4000
difficulty = 0.95
catcher_color = "blue"
catcher_width = 100
catcher_height = 100
catcher_startx = canvas_width / 2 - catcher_width / 2
catcher_starty = canvas_height - catcher_height - 20
catcher_startx2 = catcher_startx + catcher_width
catcher_starty2 = catcher_starty + catcher_height

catcher = c.create_arc(catcher_startx, catcher_starty, catcher_startx2, catcher_starty2, start=200, extent=140, style="arc", outline=catcher_color, width=3)
game_font = font.nametofont("TkFixedFont")
game_font.config(size=18)


score = 0
score_text = c.create_text(10, 10, anchor="nw", font=game_font, fill="darkblue", text="Score: "+ str(score))

lives_remaining = 3
lives_text = c.create_text(canvas_width-10, 10, anchor="ne", font=game_font, fill="darkblue", text="Lives: "+ str(lives_remaining))

eggs = []

Here, we define eight different functions.

1.create_egg(): we use this function t create eggs.

2.move_eggs(): we use this function to move the eggs.

3.egg_dropped(egg): This function is used to remove and delete the eggs.

4. lose_a_life(): This function tells us how many remaining lives are left to play the game.

See also  CARD GAME | PYTHON

5. check_catch(): This function is used to check the no. of catches.

6.increase_score(points): This function is used to tell us how much we scored.

7.move_left(event): This function is used to move the basket to the left side.

8.move_right(event): This function is used to move the basket to the left side.

def create_egg():
    x = randrange(10, 740)
    y = 40
    new_egg = c.create_oval(x, y, x+egg_width, y+egg_height, fill=next(color_cycle), width=0)
    eggs.append(new_egg)
    root.after(egg_interval, create_egg)

def move_eggs():
    for egg in eggs:
        (eggx, eggy, eggx2, eggy2) = c.coords(egg)
        c.move(egg, 0, 10)
        if eggy2 > canvas_height:
            egg_dropped(egg)
    root.after(egg_speed, move_eggs)

def egg_dropped(egg):
    eggs.remove(egg)
    c.delete(egg)
    lose_a_life()
    if lives_remaining == 0:
        messagebox.showinfo("Game Over!", "Final Score: "+ str(score))
        root.destroy()

def lose_a_life():
    global lives_remaining
    lives_remaining -= 1
    c.itemconfigure(lives_text, text="Lives: "+ str(lives_remaining))

def check_catch():
    (catcherx, catchery, catcherx2, catchery2) = c.coords(catcher)
    for egg in eggs:
        (eggx, eggy, eggx2, eggy2) = c.coords(egg)
        if catcherx < eggx and eggx2 < catcherx2 and catchery2 - eggy2 < 40:
            eggs.remove(egg)
            c.delete(egg)
            increase_score(egg_score)
    root.after(100, check_catch)

def increase_score(points):
    global score, egg_speed, egg_interval
    score += points
    egg_speed = int(egg_speed * difficulty)
    egg_interval = int(egg_interval * difficulty)
    c.itemconfigure(score_text, text="Score: "+ str(score))

def move_left(event):
    (x1, y1, x2, y2) = c.coords(catcher)
    if x1 > 0:
        c.move(catcher, -20, 0)

def move_right(event):
    (x1, y1, x2, y2) = c.coords(catcher)
    if x2 < canvas_width:
        c.move(catcher, 20, 0)

c.bind("<Left>", move_left)
c.bind("<Right>", move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()

Source Code:

Here is the complete source code for the project.

from itertools import cycle
from random import randrange
from tkinter import Canvas, Tk, messagebox, font
root = Tk()
root.title("Egg Catcher")
c = Canvas(root, width=canvas_width, height=canvas_height, background="deep sky blue")
c.create_rectangle(-5, canvas_height-100, canvas_width+5, canvas_height+5, fill="sea green", width=0)
c.create_oval(-80, -80, 120, 120, fill='orange', width=0)
c.pack()

color_cycle = cycle(["light blue", "light green", "light pink", "light yellow", "light cyan"])
egg_width = 45
egg_height = 55
egg_score = 10
egg_speed = 500
egg_interval = 4000
difficulty = 0.95
catcher_color = "blue"
catcher_width = 100
catcher_height = 100
catcher_startx = canvas_width / 2 - catcher_width / 2
catcher_starty = canvas_height - catcher_height - 20
catcher_startx2 = catcher_startx + catcher_width
catcher_starty2 = catcher_starty + catcher_height

catcher = c.create_arc(catcher_startx, catcher_starty, catcher_startx2, catcher_starty2, start=200, extent=140, style="arc", outline=catcher_color, width=3)
game_font = font.nametofont("TkFixedFont")
game_font.config(size=18)


score = 0
score_text = c.create_text(10, 10, anchor="nw", font=game_font, fill="darkblue", text="Score: "+ str(score))

lives_remaining = 3
lives_text = c.create_text(canvas_width-10, 10, anchor="ne", font=game_font, fill="darkblue", text="Lives: "+ str(lives_remaining))

eggs = []

def create_egg():
    x = randrange(10, 740)
    y = 40
    new_egg = c.create_oval(x, y, x+egg_width, y+egg_height, fill=next(color_cycle), width=0)
    eggs.append(new_egg)
    root.after(egg_interval, create_egg)

def move_eggs():
    for egg in eggs:
        (eggx, eggy, eggx2, eggy2) = c.coords(egg)
        c.move(egg, 0, 10)
        if eggy2 > canvas_height:
            egg_dropped(egg)
    root.after(egg_speed, move_eggs)

def egg_dropped(egg):
    eggs.remove(egg)
    c.delete(egg)
    lose_a_life()
    if lives_remaining == 0:
        messagebox.showinfo("Game Over!", "Final Score: "+ str(score))
        root.destroy()

def lose_a_life():
    global lives_remaining
    lives_remaining -= 1
    c.itemconfigure(lives_text, text="Lives: "+ str(lives_remaining))

def check_catch():
    (catcherx, catchery, catcherx2, catchery2) = c.coords(catcher)
    for egg in eggs:
        (eggx, eggy, eggx2, eggy2) = c.coords(egg)
        if catcherx < eggx and eggx2 < catcherx2 and catchery2 - eggy2 < 40:
            eggs.remove(egg)
            c.delete(egg)
            increase_score(egg_score)
    root.after(100, check_catch)

def increase_score(points):
    global score, egg_speed, egg_interval
    score += points
    egg_speed = int(egg_speed * difficulty)
    egg_interval = int(egg_interval * difficulty)
    c.itemconfigure(score_text, text="Score: "+ str(score))

def move_left(event):
    (x1, y1, x2, y2) = c.coords(catcher)
    if x1 > 0:
        c.move(catcher, -20, 0)

def move_right(event):
    (x1, y1, x2, y2) = c.coords(catcher)
    if x2 < canvas_width:
        c.move(catcher, 20, 0)

c.bind("<Left>", move_left)
c.bind("<Right>", move_right)
c.focus_set()
root.after(1000, create_egg)
root.after(1000, move_eggs)
root.after(1000, check_catch)
root.mainloop()

Output:

In this way, we can run the code.

See also  OBJECT ORIENTED PROGRAMMING (OOP’s) IN PYTHON

After running the above-given command you will get a new window opened and the egg catcher game will be started in that window.

This is the output of the code. we can see there is an egg coming toward the ground and there is a blue u-shaped basket. now we have to catch the egg into the basket by moving toward the left and right using the left arrow key and right arrow key.

 this is the final score we get after using three lives. here is my final score.I scored 130.

Successfully we made an egg catcher game using python.

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh