Skip to content

Commit 30cc7e6

Browse files
committedJul 8, 2016
python_ball-game
1 parent 9d7915b commit 30cc7e6

File tree

3 files changed

+343
-0
lines changed

3 files changed

+343
-0
lines changed
 

‎.DS_Store

6 KB
Binary file not shown.

‎ball.py

+343
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
1+
from Tkinter import*
2+
from time import*
3+
from math import*
4+
import tkFont
5+
import random
6+
7+
root = Tk()
8+
c = Canvas(root, width = 470, height = 470, relief = "raised")
9+
ballplay_enable = 0
10+
11+
12+
13+
class Ball_Mode2:
14+
def __init__(self, canvas, paddle1, paddle2, color):
15+
self.canvas = canvas
16+
self.paddle1 = paddle1
17+
self.paddle2 = paddle2
18+
self.id = canvas.create_oval(10, 10, 25, 25, fill = color)
19+
self.canvas.move(self.id, 245, 100)
20+
starts = [-3, -2.5, -2, -1.5, 1, 1.5, 2, 2.5, 3]
21+
random.shuffle(starts)
22+
self.x = starts[0]
23+
self.y = -3
24+
self.canvas_height = self.canvas.winfo_height()
25+
self.canvas_width = self.canvas.winfo_width()
26+
self.hit_wall = False
27+
self.who_win = 0
28+
29+
def hit_paddle(self, pos):
30+
paddle_pos1 = self.canvas.coords(self.paddle1.id)
31+
paddle_pos2 = self.canvas.coords(self.paddle2.id)
32+
33+
if pos[3] >= paddle_pos1[1] and pos[1] <= paddle_pos1[3] and pos[0] <= paddle_pos1[2] and pos[0] >= paddle_pos1[0]:
34+
return 2
35+
elif pos[3] >= paddle_pos2[1] and pos[1] <= paddle_pos2[3] and pos[2] >= paddle_pos2[0] and pos[2] <= paddle_pos2[2]:
36+
return 1
37+
return 0
38+
39+
def draw(self):
40+
self.canvas.move(self.id, self.x, self.y)
41+
pos = self.canvas.coords(self.id)
42+
if pos[1] <= 0:
43+
self.y = 3
44+
if pos[3] >= self.canvas_height:
45+
self.y = -3
46+
if pos[0] <= 0 or pos[2] >= self.canvas_width:
47+
self.hit_wall = True
48+
if pos[0] <= 0:
49+
self.who_win = 1
50+
else:
51+
self.who_win = 0
52+
x = self.hit_paddle(pos)
53+
if x != 0:
54+
self.x = 3 *((-1) ** x)
55+
56+
class Paddle_Mode2:
57+
def __init__(self, canvas, x, y, left_or_right, color):
58+
self.canvas = canvas
59+
self.id = canvas.create_rectangle(x, y, x + 10, y + 100, fill = color)
60+
self.color = color
61+
self.speed = 0
62+
self.exist = True
63+
self.canvas_height = self.canvas.winfo_height()
64+
if left_or_right == 0:
65+
self.canvas.bind_all('<KeyPress-w>', self.turn_up)
66+
self.canvas.bind_all('<KeyPress-s>', self.turn_down)
67+
self.canvas.bind_all('<KeyRelease-w>', self.turn_up_stop)
68+
self.canvas.bind_all('<KeyRelease-s>', self.turn_down_stop)
69+
else:
70+
self.canvas.bind_all('<KeyPress-Up>', self.turn_up)
71+
self.canvas.bind_all('<KeyPress-Down>', self.turn_down)
72+
self.canvas.bind_all('<KeyRelease-Up>', self.turn_up_stop)
73+
self.canvas.bind_all('<KeyRelease-Down>', self.turn_down_stop)
74+
def draw(self):
75+
self.canvas.move(self.id, 0, self.speed)
76+
def turn_up(self, event):
77+
if self.exist == True:
78+
pos = self.canvas.coords(self.id)
79+
if pos[1] <= 0:
80+
self.speed = 0
81+
else:
82+
self.speed = -4
83+
def turn_down(self, event):
84+
if self.exist == True:
85+
pos = self.canvas.coords(self.id)
86+
if pos[3] >= self.canvas_height:
87+
self.speed = 0
88+
else:
89+
self.speed = 4
90+
def turn_up_stop(self, event):
91+
self.speed = 0
92+
def turn_down_stop(self, event):
93+
self.speed = 0
94+
95+
96+
class Ball:
97+
def __init__(self, canvas, paddle, color):
98+
self.canvas = canvas
99+
self.paddle = paddle
100+
101+
x = random.randrange(-6, 7)
102+
y = random.randrange(0, 7)
103+
b0 = canvas.create_rectangle(50 + x, 0, 90 + x, 10, fill = 'black')
104+
b1 = canvas.create_rectangle(250, 0, 290, 10, fill = 'black')
105+
b2 = canvas.create_rectangle(440, 0 + 2*y, 480, 10 + 2*y, fill = 'black')
106+
b3 = canvas.create_rectangle(200, 40, 240, 50, fill = 'black')
107+
b4 = canvas.create_rectangle(242 + x, 40 + y, 282 + x, 50 + y, fill = 'black')
108+
b5 = canvas.create_rectangle(0 , 120, 40, 130, fill = 'black')
109+
b6 = canvas.create_rectangle(150, 120 - y, 200, 130 - y, fill = 'black')
110+
b7 = canvas.create_rectangle(220 + x, 120, 270 + x, 130, fill = 'black')
111+
self.brick = [b0,b1,b2,b3,b4,b5,b6,b7]
112+
self.id = canvas.create_oval(10, 10, 25, 25, fill = color)
113+
self.canvas.move(self.id, 245, 100)
114+
starts = [-6, -5, -4.5, -4,-3.5, -3, -2.5, -2, -1.5, -1, 1, 1.5, 2, 2.5, 3, 3.5, 4,4.5, 5, 6]
115+
random.shuffle(starts)
116+
self.x = starts[0]
117+
self.y = -3
118+
self.canvas_height = self.canvas.winfo_height()
119+
self.canvas_width = self.canvas.winfo_width()
120+
self.hit_bottom = False
121+
self.win = 0
122+
self.flag = [0,0,0,0,0,0,0,0]
123+
124+
def hit_brick(self, pos):
125+
brick_pos = []
126+
for i in range(0, 8):
127+
brick_pos.append(self.canvas.coords(self.brick[i]))
128+
for i in range(0, 8):
129+
if self.flag[i] == 0:
130+
if pos[2] >= brick_pos[i][0] and pos[0] <= brick_pos[i][2]:
131+
if pos[1] >= brick_pos[i][1] and pos[1] <= brick_pos[i][3]:
132+
self.flag[i] = 1
133+
self.canvas.delete(self.brick[i])
134+
self.win += 1
135+
if self.y > 0:
136+
return 1
137+
else:
138+
return 2
139+
140+
return 0
141+
142+
def hit_paddle(self, pos):
143+
paddle_pos = self.canvas.coords(self.paddle.id) #get the paddle position
144+
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
145+
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
146+
return True
147+
return False
148+
149+
def draw(self):
150+
self.canvas.move(self.id,self.x,self.y)
151+
pos = self.canvas.coords(self.id)
152+
if pos[1] <= 0:
153+
self.y = 3
154+
if pos[3] >= self.canvas_height:
155+
self.hit_bottom = True
156+
x = self.hit_brick(pos)
157+
if x != 0:
158+
self.y = 3 * ((-1) ** x)
159+
if self.hit_paddle(pos) == True:
160+
self.y = -3
161+
if pos[0] <= 0:
162+
self.x = 3
163+
if pos[2] >= self.canvas_width:
164+
self.x = -3
165+
166+
class Paddle:
167+
def __init__(self, canvas, color):
168+
self.canvas = canvas
169+
self.id = canvas.create_rectangle(0, 0, 100, 10, fill = color)
170+
self.canvas.move(self.id, 200, 400)
171+
self.speed = 0
172+
self.paddle_exist = True
173+
self.canvas_width = self.canvas.winfo_width()
174+
self.canvas.bind_all('<KeyPress-Left>', self.turn_left)
175+
self.canvas.bind_all('<KeyPress-Right>', self.turn_right)
176+
self.canvas.bind_all('<KeyRelease-Left>', self.turn_left_stop)
177+
self.canvas.bind_all('<KeyRelease-Right>', self.turn_right_stop)
178+
def draw(self):
179+
self.canvas.move(self.id, self.speed, 0)
180+
#pos = self.canvas.coords(self.id) #get the paddle position
181+
def turn_left(self, event):
182+
if self.paddle_exist == True:
183+
pos = self.canvas.coords(self.id)
184+
if pos[0] <= 0:
185+
self.speed = 0
186+
else:
187+
self.speed = -4
188+
def turn_right(self, event):
189+
if self.paddle_exist == True:
190+
pos = self.canvas.coords(self.id)
191+
if pos[2] >= self.canvas_width:
192+
self.speed = 0
193+
else:
194+
self.speed = 4
195+
def turn_left_stop(self, event):
196+
self.speed = 0
197+
def turn_right_stop(self, event):
198+
self.speed = 0
199+
200+
def out(event):
201+
global root
202+
global c
203+
global flag
204+
global ballplay_enable
205+
dist1 = sqrt((event.x - 380) * (event.x - 380) + (event.y - 290) * (event.y - 290))
206+
#enter the Mode-1
207+
if dist1 <= 50.0 and event.y <= 290:
208+
ballplay_enable = 1
209+
root.title("Game Mode 1")
210+
c.delete(ALL)
211+
root.resizable(0,0)
212+
root.wm_attributes("-topmost", 1)
213+
root.update()
214+
paddle = Paddle(c, '#4682B4')
215+
ball = Ball(c, paddle, 'red')
216+
while 1:
217+
if ball.hit_bottom == False and ball.win != 8:
218+
ball.draw()
219+
paddle.draw()
220+
root.update_idletasks()
221+
root.update()
222+
sleep(0.01)
223+
elif ball.win == 8: #win the game
224+
sleep(0.1)
225+
paddle.paddle_exist = False
226+
c.delete(ALL)
227+
c.create_rectangle(150, 190, 320, 280, fill= 'yellow')
228+
Font = tkFont.Font(size = 13, family = 'Roboto')
229+
c.create_text(235, 235, text = 'you win the game', font = Font, fill = 'red')
230+
root.update()
231+
sleep(1.5)
232+
#back to the interface
233+
ballplay_enable = 0
234+
interface()
235+
236+
else: #lose the game
237+
sleep(0.1)
238+
paddle.paddle_exist = False
239+
c.delete(ALL)
240+
c.create_rectangle(150, 190, 320, 280, fill= 'white')
241+
Font = tkFont.Font(size = 13, family = 'Roboto')
242+
c.create_text(235, 235, text = 'you lose the game', font = Font, fill = 'black')
243+
root.update()
244+
sleep(1.5)
245+
#back to the interface
246+
ballplay_enable = 0
247+
interface()
248+
249+
#enter the Mode-2
250+
elif dist1 <= 50 and event.y >= 290:
251+
ballplay_enable = 1
252+
flag = 0
253+
root.title("Game Mode 2")
254+
c.delete(ALL)
255+
root.resizable(0,0)
256+
root.wm_attributes("-topmost", 1)
257+
root.update()
258+
paddle1 = Paddle_Mode2(c, 25, 235, 0, '#4682B4')
259+
paddle2 = Paddle_Mode2(c, 425, 235, 1, 'yellow')
260+
ball_2 = Ball_Mode2(c, paddle1, paddle2, 'red')
261+
262+
while True:
263+
if ball_2.hit_wall == False: #game continues
264+
root.update()
265+
paddle1.draw()
266+
paddle2.draw()
267+
ball_2.draw()
268+
root.update_idletasks()
269+
root.update()
270+
sleep(0.008)
271+
272+
else: #game over
273+
sleep(0.1)
274+
paddle1.exist = False
275+
paddle2.exist = False
276+
c.delete(ALL)
277+
c.create_rectangle(150, 190, 320, 280, fill= 'white')
278+
Font = tkFont.Font(size = 13, family = 'Roboto')
279+
if ball_2.who_win == 1:
280+
c.create_text(235, 235, text = 'right win the game', font = Font, fill = 'black')
281+
else:
282+
c.create_text(235, 235, text = 'left win the game', font = Font, fill = 'black')
283+
root.update()
284+
sleep(1.5)
285+
286+
ballplay_enable = 0
287+
interface()
288+
289+
290+
#draw the interface
291+
def interface():
292+
global root
293+
global c
294+
global ballplay_enable
295+
root.title("Game Interface")
296+
c.pack()
297+
c.create_rectangle(0,0,510,290, fill = "#00BCD4", outline = "")
298+
c.create_rectangle(0,0, 510,30, fill = "#4682B4", outline = "")
299+
c.create_rectangle(10, 40, 35, 44, fill = "#F5F5F5", outline = "")
300+
c.create_rectangle(10, 47, 35, 51, fill = "#F5F5F5", outline = "")
301+
c.create_rectangle(10, 54, 35, 58, fill = "#F5F5F5", outline = "")
302+
c.create_oval(330,240,430,340, fill = "#FF4081", outline = "")
303+
c.create_line(330, 290, 430, 290, fill = "white")
304+
305+
font1 = tkFont.Font(size = 28, family = "Roboto", weight = tkFont.BOLD, overstrike = 0)
306+
font2 = tkFont.Font(family = 'Roboto',size = 13)
307+
c.create_text(235, 150, text = "Bouncing Ball",font = font1, fill = "white")
308+
c.create_text(235, 180, text = "Powered by Python", font = font2, fill = "#B2EBF2")
309+
c.create_text(235, 360, text = "Copyright©(2016)Gao Chao\n\nchoose the mode to start", font = font2, fill = "#B6B6B6")
310+
311+
312+
c.create_text(380, 275, text = "Mode-1",font = font2, fill= "white")
313+
c.create_text(380, 305, text = "Mode-2",font = font2, fill= "white")
314+
315+
c.bind("<Button-1>", out)
316+
317+
sb = []
318+
for i in range(5):
319+
sb.append(c.create_oval(15 * i, 435, 15 * i + 10, 445, fill = "#00BCD4", outline = ""))
320+
321+
x = [5, 15, 25, 35, 45]
322+
speed = [0, 0, 0, 0, 0]
323+
#ball play image
324+
while ballplay_enable == 0:
325+
for i in range(5):
326+
speed[i] = (abs(250 - x[i]) + 200) / 35
327+
x[i] += speed[i]
328+
c.move(sb[i], speed[i], 0)
329+
c.update()
330+
331+
if x[0] > 480:
332+
for i in range(5):
333+
c.delete(sb[i])
334+
for i in range(5):
335+
sb[i] = c.create_oval(15 * i,435,15 * i + 10,445, fill = "#00BCD4", outline = "")
336+
x = [5, 15, 25, 35, 45]
337+
sleep(0.04)
338+
339+
root.mainloop()
340+
341+
interface()
342+
343+

‎introduction.pdf

144 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.