Python is a multipurpose language and can be used in almost every field of development. Python can also be used to develop different type of game. Let’s try to develop a simple Catching the ball game using Python and TKinter.
Game is very simple. There is one bar at the bottom of game window which can be moved left or right using the buttons that are in the game window. Red ball will continuously fall from top to bottom and can start from any random x-axis distance. The task is to bring that bar to a suitable location by moving left or right so that the red ball will fall on that bar(catch the ball onto the bar) not on the ground. If player catches the ball onto the bar then score will get increase and that ball will disappear and again a new red ball will start falling from top to bottom starting from random x-axis distance. If player miss the ball from catching it on the bar then you will lose the game and then finally scorecard will appear on the game window.
Approach:
- Use Tkinter package in python for building GUI(Graphical user interface).
- Use Canvas for drawing objects in Python – Canvas is a rectangular area intended for drawing pictures or other complex layouts. We can place graphics, text, widgets or frames on Canvas.
Syntax: w = Canvas ( master, option=value, ... ) Parameters: master - This represents the parent window. options - List of most commonly used options for this widget. These options can be used as key-value pairs separated by commas. Example- width, height etc.
- Use canvas.create_oval for creating the ball.
create_oval creates a circle or an ellipse at the given coordinates. It takes two pairs of coordinates; the top left and bottom right corners of the bounding rectangle for the oval.
Syntax: oval = canvas.create_oval(x0, y0, x1, y1, options)
- Use canvas.create_rectangle for creating the bar.
create_rectangle creates a rectangle at the given coordinates. It takes two pairs of coordinates; the top left and bottom right coordinates.
Syntax: rod = canvas.create_rectangle(x0, y0, x1, y1, options)
- Use canvas.move for moving the ball or bar.
canvas.move enables the object to move with the specified (x, y) coordinates.
Syntax: move=canvas.move(name of object, x, y)
Note: *Take x=0 for moving the ball in vertical direction only and take y=0 for moving the bar in horizontal direction only. *Disappear the ball when it touches the ground or the bar using canvas.delete(object).
- Use Button for moving the bar in forward or backward and then apply action event on it. Refer Python gui Tkinter
Example
Python3
# Python code for catching the ball game # importing suitable packages from tkinter import Tk,Button,Label from tkinter import Canvas from random import randint # defining Tk from Tkinter root = Tk() root.title( "Catch the ball Game" ) root.resizable( False , False ) # for defining the canvas canvas = Canvas(root, width = 600 , height = 600 ) canvas.pack() # variable for the vertical distance # travelled by ball limit = 0 # variable for horizontal distance # of bar from x-axis dist = 5 # variable for score 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 # for creation of ball object 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 # disappear the ball canvas.delete( 'dot1' ) # calling the function for again # creation of ball object ball_set() else : # disappear the ball canvas.delete( 'dot1' ) bar.delete_bar( self ) # display the score score_board() return # incrementing the vertical distance # travelled by ball by deltay 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 for creating and moving bar 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 # for creating bar using create_rectangle self .rod = canvas.create_rectangle( self .x1, self .y1, self .x2, self .y2, fill = "yellow" ,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 # for random x-axis distance from # where the ball starts to fall value = randint( 0 , 570 ) # define the dimensions of the ball ball1 = Ball(canvas,value, 20 ,value + 30 , 50 ) # call function for moving of the ball ball1.move_ball() # Function for displaying the score # after getting over of the game def score_board(): root2 = Tk() root2.title( "Catch the ball Game" ) 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 = "green" , command = lambda :play_again(root2)) button3.pack() button4 = Button(canvas2,text = "EXIT" ,bg = "green" , 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() root.destroy() # Main function def main(): global score,dist score = 0 dist = 0 # defining the dimensions of bar bar1 = bar(canvas, 5 , 560 , 45 , 575 ) # defining the text,colour of buttons and # also define the action after click on # the button by calling suitable methods button = Button(canvas,text = "==>" , bg = "green" , 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 = "green" , command = lambda :bar1.move_bar( 0 )) button2.place(x = 260 ,y = 580 ) # calling the function for defining # the dimensions of ball ball_set() root.mainloop() # Driver code if (__name__ = = "__main__" ): main() |
Output:
Note:The above code can’t be run on online IDE as Tkinter package is imported.