How To Create Bouncing Ball Game Using Python (Mouse)

Posted by Suraj Singh on January 14, 2017 · 10 mins read
Hello readers,
                           Today, We will Discuss About How we Can Create Bouncing Ball Game Using Python Mouse Version. readers, I Know My Some Python Examples Are looks like Very Hard For Beginners but readers, Believe Me My All Example Are Easy For Understand. They looks Hard Because I Am Using OOP (Object Orient Programming). So, If Want To Be Expert Then don't Lose Your Courage or guts.

So, Let's Talk About This Example


First, Screen Shots:


Example Code:

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python

# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This is an Example Of Tkinter Canvas Graphics
# This Script Is Created For http://www.bitforestinfo.com
# This Script is Written By
__author__='''

######################################################
By
######################################################

Suraj Singh


surajsinghbisht054@gmail.com
http://www.bitforestinfo.com/


######################################################
'''
print __author__
# ----------------------------------------------------------------------

try:
import Tkinter
except:
import tkinter as Tkinter
import time
BALL_SPEED=5

class GameCanvas(Tkinter.Canvas):
def __init__(self, *args, **kwargs):
Tkinter.Canvas.__init__(self, *args, **kwargs)
self.create_bouncing_ball()
self.create_moving_bat()

def create_moving_bat(self):
self.bat=Tkinter.Canvas.create_rectangle(self,0,570,100,580, fill='lightslateblue')
self.bind('<Motion>', self.update_bat_moves)
return

def update_bat_moves(self, event=None):
x=event.x
x1,y1,x2,y2=self.coords(self.bat)
gap=(x2-x1)/2
center=x1+gap
move=x-center
self.move(self.bat,move,0)
return

def create_bouncing_ball(self):
self.ball=Tkinter.Canvas.create_oval(self, 0,0,20,20, fill='cornflowerblue')
self.x=BALL_SPEED
self.y=BALL_SPEED
return

def update_board(self):
width=self.winfo_width()
height=self.winfo_height()
x1,y1,x2,y2=self.coords(self.ball)
hit=len(self.find_overlapping(x1,y1,x2,y2))
if hit>=2:
self.y=-BALL_SPEED
self.move(self.ball,self.x,self.y)
elif x1<0:
self.x=BALL_SPEED
self.move(self.ball,self.x,self.y)
elif x2>width:
self.x=-BALL_SPEED
self.move(self.ball,self.x,self.y)
elif y1<0:
self.y=BALL_SPEED
self.move(self.ball,self.x,self.y)
elif y2>height:
x=width/2
y=height/2
self.create_text(x,y, text='Game Over', font=('arial 50 bold'), fill='red')
self.y=-BALL_SPEED
self.move(self.ball,self.x,self.y)
else:
self.move(self.ball,self.x,self.y)
return





# Gui Handler
def main():
root=Tkinter.Tk()
root.minsize(800,600)
root.maxsize(800,600)
board=GameCanvas(root, bg='lavender')
board.pack(expand='yes', fill='both')
# Program Loop
while True:
root.update()
root.update_idletasks()
board.update_board()
time.sleep(0.01)

# main Trigger
if __name__=='__main__':
main()

if Anyone Wants To Download Click Here



thanks for visiting my blog.


For any question or Suggestion or help
Suraj
Email:  surajsinghbisht054@gmail.com
Or post Comment.
please feel free to contact me.