Catch The Ball

As straightforward as a name can get. The objective is to catch the ball in the plank and let it bounce back up. If it falls to the ground, you lose the game.


Download

python-icon
                        
    # import modules
    from tkinter import Tk, Button, Label
    from tkinter import Canvas
    from random import randint

    # defining Tk from Tkinter 
    tk = Tk()
    # Set window title and icon
    tk.title("Catch-The-Ball")
    tk.iconbitmap("logo-ico.ico")
    tk.resizable(False, False)

    # For defining the canvas
    canvas = Canvas(tk, width=600, height=600, bg='#ffff99')
    canvas.pack()

    limit = 0
    dist = 5
    score = 0

    # Class for the Creating and moving ball
    class Ball:
    # for creation of ball on the canvas
    def __init__(self, canvas, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.canvas = canvas
        
        self.ball = canvas.create_oval(self.x1, self.y1, self.x2, self.y2,
                                        fill="red", tags='dot1')

    # for moving the ball
    def move_ball(self):

        # defining offset
        offset = 10
        global limit

        # checking if ball lands ground or bar
        if limit >= 510:
            global dist, score, next

             # checking that ball falls on the bar
            if (dist - offset <= self.x1 and
                    dist + 40 + offset >= self.x2):

                # incrementing the score
                score += 10

                # dissappear the ball
                canvas.delete('dot1')

                ball_set()

            else:
                # dissappear the ball
                canvas.delete('dot1')
                bar.delete_bar(self)

                score_board()
            return

        limit += 1
        
        # moving the ball in vertical direction
        # by taking x=0 and y=deltay
        
        self.canvas.move(self.ball, 0, 1)

        # for continuous moving of ball again call move_ball
        self.canvas.after(10, self.move_ball)

    class bar:
    # method for creating bar
    def __init__(self, canvas, x1, y1, x2, y2):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.canvas = canvas

        self.rod = canvas.create_rectangle(self.x1, self.y1, self.x2, self.y2,
                                            fill="#0000ff", tags='dot2')

     # method for moving the bar
    def move_bar(self, num):
        global dist
        # checking the forward or backward button
        if (num == 1):
            
            # moving the bar in forward direction by
            # taking x-axis positive distance and
            # taking vertical distance y=0
            
            self.canvas.move(self.rod, 20, 0)

            # incrementing the distance of bar from x-axis
            dist += 20
        else:
            
            # moving the bar in backward direction by taking x-axis
            # negative distance and taking vertical distance y=0
            
            self.canvas.move(self.rod, -20, 0)

            # decrementing the distance of bar from x-axis
            dist -= 20

    def delete_bar(self):
        canvas.delete('dot2')

    # Function to define the dimensions of the ball


    def ball_set():
    global limit
    limit = 0
    value = randint(0, 570)

    ball1 = Ball(canvas, value, 20, value + 30, 50)

    ball1.move_ball()

    
    # Function for displaying the score
    # after getting over of the game 
    
    def score_board():
    root2 = Tk()
    root2.title("Catch-The-Ball")
    root2.resizable(False, False)
    canvas2 = Canvas(root2, width=300, height=300)
    canvas2.pack()

    w = Label(canvas2, text="\nOOPS...GAME IS OVER\n\nYOUR SCORE = "
                            + str(score) + "\n\n")
    w.pack()

    button3 = Button(canvas2, text="PLAY AGAIN", bg="#ffb31a",
                        command=lambda: play_again(root2))
    button3.pack()

    button4 = Button(canvas2, text="EXIT", bg="#ffb31a",
                        command=lambda: exit_handler(root2))
    button4.pack()


    # Function for handling the play again request
    def play_again(root2):
    root2.destroy()
    main()


    # Function for handling exit request
    def exit_handler(root2):
    root2.destroy()
    tk.destroy()


    # Main function
    def main():
    global score, dist
    score = 0
    dist = 0

    # defining the dimensions of bar
    bar1 = bar(canvas, 5, 560, 45, 575)

    button = Button(canvas, text="==>", bg="#00ff00",
                    command=lambda: bar1.move_bar(1))

    # placing the buttons at suitable location on the canvas
    button.place(x=300, y=580)

    button2 = Button(canvas, text="<==", bg="#00ff00",
                        command=lambda: bar1.move_bar(0))
    button2.place(x=260, y=580)


    ball_set()
    tk.mainloop()

    # defining the dimensions of bar
    if (__name__ == "__main__"):
    main()