How To Create Bouncing Ball Game Using Python (Keyboard)

Posted by Suraj Singh on January 14, 2017 · 13 mins read
Hello readers,
                         


 Today,

We will Discuss About How we Can Create Bouncing Ball Game Using Python Keyboard Version. 


readers, 

                I Know, My Some Python Examples Are looks like Very Hard For Beginners but readers, 

Belive 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
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# ---------------- 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__
# ----------- Importing Module --------------------------
try:
import Tkinter, random, time, sys
except:
import tkinter as Tkinter,sys ,random, time

# -------- Gaming Display -----------------------------
class GameDisplay:
def __init__(self, canvas):
self.canvas=canvas
self.widthlimit=canvas.winfo_screenwidth()
self.heightlimit=canvas.winfo_screenheight()
y2=canvas.winfo_screenheight()-70
y1=y2-15
self.bat=Tkinter.Canvas.create_rectangle(canvas, 0, y1,200, y2, fill='steelblue')
self.ball=Tkinter.Canvas.create_oval(canvas, 0, 0,50,50, fill='lightgreen')
self.xvelocity=2
self.yvelocity=2

def update(self, event=None):
x1,y1,x2,y2=self.canvas.coords(self.ball)
a,b,c,d=self.canvas.coords(self.bat)
k=len(self.canvas.find_overlapping(a,b,c,d))
if x1<=0:
self.xvelocity=2
self.canvas.move(self.ball,self.xvelocity,0)
pass
elif y2>=self.heightlimit:
x=self.canvas.winfo_screenwidth()/2
y=self.canvas.winfo_screenheight()/2
self.canvas.create_text(x,y, text='Game Over', font=('arial 50 bold'), fill='red')
self.canvas.master.update()
self.canvas.master.update_idletasks()
time.sleep(2)
self.canvas.master.destroy()
sys.exit(0)
pass
elif x2>=self.widthlimit:
self.xvelocity=-2
self.canvas.move(self.ball,self.xvelocity,0)
elif y1<=0:
self.yvelocity=2
self.canvas.move(self.ball, 0, self.yvelocity)
elif k==2:
self.yvelocity=-2
self.canvas.move(self.ball, 0, self.yvelocity)
else:
self.canvas.move(self.ball, self.xvelocity, self.yvelocity)

pass

def update_bat(self, event=None):
direction=event.keysym
x1,x2,y1,y2=self.canvas.coords(self.bat)
if y1>self.widthlimit:
if direction=='Left':
self.canvas.move(self.bat, -20,0)
pass
elif x1<0:
if direction=='Right':
self.canvas.move(self.bat, 20,0)
pass

elif direction=='Right':
self.canvas.move(self.bat, 20,0)
elif direction=='Left':
self.canvas.move(self.bat, -20,0)
else:
pass


# --------- Main Window Graphic ----------------------
class main:
def __init__(self):
root=Tkinter.Tk(className='Bouncing Game')
w, h=root.winfo_screenwidth(), root.winfo_screenheight()
canvas=Tkinter.Canvas(root, background='grey', width=w, height=h)
canvas.pack()
game=GameDisplay(canvas)
for seq in ['<Any-KeyPress>']:
root.bind_all(seq,game.update_bat)
# Creating Transpareny
root.wait_visibility(canvas)
root.wm_attributes('-alpha',0.7)
# Custom Main loop
while True:
time.sleep(0.01)
game.update()
root.update()
root.update_idletasks()

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

if Anyone Want To Download This Example Then 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.