-
Notifications
You must be signed in to change notification settings - Fork 66
/
game.pyw
191 lines (166 loc) · 6.4 KB
/
game.pyw
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from tkinter import *
import time
import random
root = Tk()
root.title("Bounce")
root.geometry("500x570")
root.resizable(0, 0)
root.wm_attributes("-topmost", 1)
canvas = Canvas(root, width=500, height=500, bd=0, highlightthickness=0, highlightbackground="Red", bg="Black")
canvas.pack(padx=10, pady=10)
score = Label(height=50, width=80, text="Score: 00", font="Consolas 14 bold")
score.pack(side="left")
root.update()
class Ball:
def __init__(self, canvas, color, paddle, bricks, score):
self.bricks = bricks
self.canvas = canvas
self.paddle = paddle
self.score = score
self.bottom_hit = False
self.hit = 0
self.id = canvas.create_oval(10, 10, 25, 25, fill=color, width=1)
self.canvas.move(self.id, 230, 461)
start = [4, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6]
random.shuffle(start)
#print(start)
self.x = start[0]
self.y = -start[0]
self.canvas.move(self.id, self.x, self.y)
self.canvas_height = canvas.winfo_height()
self.canvas_width = canvas.winfo_width()
def brick_hit(self, pos):
for brick_line in self.bricks:
for brick in brick_line:
brick_pos = self.canvas.coords(brick.id)
#print(brick_pos)
try:
if pos[2] >= brick_pos[0] and pos[0] <= brick_pos[2]:
if pos[3] >= brick_pos[1] and pos[1] <= brick_pos[3]:
canvas.bell()
self.hit += 1
self.score.configure(text="Score: " + str(self.hit))
self.canvas.delete(brick.id)
return True
except:
continue
return False
def paddle_hit(self, pos):
paddle_pos = self.canvas.coords(self.paddle.id)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
if pos[3] >= paddle_pos[1] and pos[1] <= paddle_pos[3]:
#print("paddle hit")
return True
return False
def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
#print(pos)
start = [4, 3.8, 3.6, 3.4, 3.2, 3, 2.8, 2.6]
random.shuffle(start)
if self.brick_hit(pos):
self.y = start[0]
if pos[1] <= 0:
self.y = start[0]
if pos[3] >= self.canvas_height:
self.bottom_hit = True
if pos[0] <= 0:
self.x = start[0]
if pos[2] >= self.canvas_width:
self.x = -start[0]
if self.paddle_hit(pos):
self.y = -start[0]
class Paddle:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
self.canvas.move(self.id, 200, 485)
self.x = 0
self.pausec=0
self.canvas_width = canvas.winfo_width()
self.canvas.bind_all("<Left>", self.turn_left)
self.canvas.bind_all("<Right>", self.turn_right)
self.canvas.bind_all("<space>", self.pauser)
def draw(self):
pos = self.canvas.coords(self.id)
#print(pos)
if pos[0] + self.x <= 0:
self.x = 0
if pos[2] + self.x >= self.canvas_width:
self.x = 0
self.canvas.move(self.id, self.x, 0)
def turn_left(self, event):
self.x = -3.5
def turn_right(self, event):
self.x = 3.5
def pauser(self,event):
self.pausec+=1
if self.pausec==2:
self.pausec=0
class Bricks:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(5, 5, 25, 25, fill=color, width=2)
playing = False
def start_game(event):
global playing
if playing is False:
playing = True
score.configure(text="Score: 00")
canvas.delete("all")
BALL_COLOR = ["red", "yellow", "white"]
BRICK_COLOR = ["PeachPuff3", "dark slate gray", "rosy brown", "light goldenrod yellow", "turquoise3", "salmon",
"light steel blue", "dark khaki", "pale violet red", "orchid", "tan", "MistyRose2",
"DodgerBlue4", "wheat2", "RosyBrown2", "bisque3", "DarkSeaGreen1"]
random.shuffle(BALL_COLOR)
paddle = Paddle(canvas, "blue")
bricks = []
for i in range(0, 5):
b = []
for j in range(0, 19):
random.shuffle(BRICK_COLOR)
tmp = Bricks(canvas, BRICK_COLOR[0])
b.append(tmp)
bricks.append(b)
for i in range(0, 5):
for j in range(0, 19):
canvas.move(bricks[i][j].id, 25 * j, 25 * i)
ball = Ball(canvas, BALL_COLOR[0], paddle, bricks, score)
root.update_idletasks()
root.update()
time.sleep(1)
while 1:
if paddle.pausec !=1:
try:
canvas.delete(m)
del m
except:
pass
if not ball.bottom_hit:
ball.draw()
paddle.draw()
root.update_idletasks()
root.update()
time.sleep(0.01)
if ball.hit==95:
canvas.create_text(250, 250, text="YOU WON !!", fill="yellow", font="Consolas 24 ")
root.update_idletasks()
root.update()
playing = False
break
else:
canvas.create_text(250, 250, text="GAME OVER!!", fill="red", font="Consolas 24 ")
root.update_idletasks()
root.update()
playing = False
break
else:
try:
if m==None:pass
except:
m=canvas.create_text(250, 250, text="PAUSE!!", fill="green", font="Consolas 24 ")
root.update_idletasks()
root.update()
root.bind_all("<Return>", start_game)
canvas.create_text(250, 250, text="Press Enter to start Game!!", fill="red", font="Consolas 18")
root.mainloop()