forked from hhhrrrttt222111/PyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCatch-The-Ball.html
More file actions
332 lines (247 loc) · 16.4 KB
/
Catch-The-Ball.html
File metadata and controls
332 lines (247 loc) · 16.4 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta property="og:title" content="PyBox - Python Games">
<meta property="og:type" content="website">
<meta property="og:description" content="Python Games to increase your skill and experience in Python">
<!-- <meta property="og:image" content="">
<meta property="og:url" content=""> -->
<meta name="twitter:title" content="PyBox - Python Games">
<meta name="twitter:site" content="@hhhrrrttt222111">
<meta name="twitter:creator" content="@hhhrrrttt222111">
<meta name="twitter:description" content="Python Games to increase your skill and experience in Python">
<link rel = "icon" href ="..\assets\icons\logo-png.png" type = "image/x-icon">
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"
integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="games.css">
<title>PyBox - Catch The Ball </title>
</head>
<body>
<nav class="navbar static-top">
<div class="container">
<a class="navbar-brand" href="../index.html"><img src="..\assets\icons\logo-png.png" class="nav-logo" alt="PyBox"></a>
<button onclick="goBack()" class="btn btn-dark">Go Back</button>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-sm-4">
<h1 class="game-title">Catch The Ball</h1>
<p class="game-desc">As straightforward as a name can get. The objective is to catch the
ball in the plank and let it bounce back up. If it falls to the ground, you lose the game.</p>
<ul class="nav nav-pills flex-column">
<li class="nav-item">
<a class="fa fa-2x fa-github" href="https://github.com/hhhrrrttt222111/PyBox/blob/master/Games/Catch-The-Ball.py" target="_blank" rel="noopener noreferrer"></a>
<a href="https://github.com/hhhrrrttt222111/PyBox/stargazers" target="_blank" rel="noopener noreferrer" class="git-star"><img alt="GitHub stars" src="https://img.shields.io/github/stars/hhhrrrttt222111/PyBox?style=social"></a>
</li>
</ul>
<hr class="d-sm-none">
</div>
<div class="col-sm-8">
<div class="download">
<h4 class="dwnld">Download</h4>
<a class="btn" href="Text/Catch-The-Ball.txt" download><i class="fa fa-2x fa-file"></i></a>
<a class="btn" href="../Games/Catch-The-Ball.py" download><img src="../assets/icons/python-icon.png" alt="python-icon" class="python-icon"></i></a>
</div>
<!-- CODE -->
<pre class="code">
<span class="comment"># import modules</span>
<span class="in-built">from</span> tkinter <span class="in-built">import</span> <span class="function">Tk</span>, <span class="function">Button</span>, <span class="function">Label</span>
<span class="in-built">from</span> tkinter <span class="in-built">import</span> <span class="function">Canvas</span>
<span class="in-built">from</span> random <span class="in-built">import</span> <span class="function">randint</span>
<span class="comment"># defining Tk <span class="in-built">from</span> Tkinter </span>
<span class="variable">tk</span> = <span class="function">Tk</span>()
<span class="comment"># Set window title and icon</span>
<span class="variable">tk</span>.<span class="function">title</span>("Catch-The-Ball")
<span class="variable">tk</span>.<span class="function">iconbitmap</span>("logo-ico.ico")
<span class="variable">tk</span>.<span class="function">resizable</span>(False, False)
<span class="comment"># For defining the canvas</span>
<span class="variable">canvas</span> = <span class="function">Canvas</span>(<span class="variable">tk</span>, width=600, height=600, bg='#ffff99')
<span class="variable">canvas</span>.<span class="function">pack</span>()
<span class="variable">limit</span> = 0
<span class="variable">dist</span> = 5
<span class="variable">score</span> = 0
<span class="comment"># Class for the Creating and moving ball</span>
class Ball:
<span class="comment"># for creation of ball on the canvas</span>
<span class="in-built">def</span> __init__(self, <span class="variable">canvas</span>, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.<span class="variable">canvas</span> = <span class="variable">canvas</span>
self.ball = <span class="variable">canvas</span>.create_oval(self.x1, self.y1, self.x2, self.y2,
fill="red", tags='dot1')
<span class="comment"># for moving the ball</span>
<span class="in-built">def</span> move_ball(self):
<span class="comment"># defining offset</span>
<span class="variable">offset</span> = 10
<span class="in-built">global</span> <span class="variable">limit</span>
<span class="comment"># checking if ball lands ground or bar</span>
<span class="in-built">if</span> <span class="variable">limit</span> >= 510:
<span class="in-built">global</span> <span class="variable">dist</span>, <span class="variable">score</span>, next
<span class="comment"> # checking that ball falls on the bar</span>
<span class="in-built">if</span> (<span class="variable">dist</span> - <span class="variable">offset</span> <= self.x1 and
<span class="variable">dist</span> + 40 + <span class="variable">offset</span> >= self.x2):
<span class="comment"># incrementing the score</span>
<span class="variable">score</span> += 10
<span class="comment"># dissappear the ball</span>
<span class="variable">canvas</span>.delete('dot1')
<span class="function">ball_set</span>()
<span class="in-built">else</span>:
<span class="comment"># dissappear the ball</span>
<span class="variable">canvas</span>.delete('dot1')
bar.<span class="function">delete_bar</span>(self)
<span class="function">score_board</span>()
<span class="in-built">return</span>
<span class="variable">limit</span> += 1
<span class="comment">
# moving the ball in vertical direction
# by taking x=0 and y=deltay
</span>
self.<span class="variable">canvas</span>.move(self.ball, 0, 1)
<span class="comment"># for continuous moving of ball again call move_ball</span>
self.<span class="variable">canvas</span>.after(10, self.move_ball)
class bar:
<span class="comment"># method for creating bar</span>
<span class="in-built">def</span> __init__(self, <span class="variable">canvas</span>, x1, y1, x2, y2):
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
self.<span class="variable">canvas</span> = <span class="variable">canvas</span>
self.rod = <span class="variable">canvas</span>.<span class="function">create_rectangle</span>(self.x1, self.y1, self.x2, self.y2,
fill="#0000ff", tags='dot2')
<span class="comment"> # method for moving the bar</span>
<span class="in-built">def</span> <span class="function">move_bar</span>(self, num):
<span class="in-built">global</span> <span class="variable">dist</span>
<span class="comment"># checking the forward or backward button</span>
<span class="in-built">if</span> (num == 1):
<span class="comment">
# moving the bar in forward direction by
# taking x-axis positive distance and
# taking vertical distance y=0
</span>
self.<span class="variable">canvas</span>.move(self.rod, 20, 0)
<span class="comment"># incrementing the distance of bar from x-axis</span>
<span class="variable">dist</span> += 20
<span class="in-built">else</span>:
<span class="comment">
# moving the bar in backward direction by taking x-axis
# negative distance and taking vertical distance y=0
</span>
self.<span class="variable">canvas</span>.move(self.rod, -20, 0)
<span class="comment"># decrementing the distance of bar from x-axis</span>
<span class="variable">dist</span> -= 20
<span class="in-built">def</span> <span class="function">delete_bar</span>(self):
<span class="variable">canvas</span>.delete('dot2')
# Function to define the dimensions of the ball
<span class="in-built">def</span> <span class="function">ball_set</span>():
<span class="in-built">global</span> <span class="variable">limit</span>
<span class="variable">limit</span> = 0
value = <span class="function">randint</span>(0, 570)
<span class="variable">ball1</span> = Ball(<span class="variable">canvas</span>, value, 20, value + 30, 50)
<span class="variable">ball1</span>.move_ball()
<span class="comment">
# Function for displaying the score
# after getting over of the game
</span>
<span class="in-built">def</span> <span class="function">score_board</span>():
<span class="variable">root2</span> = <span class="function">Tk</span>()
<span class="variable">root2</span>.<span class="function">title</span>("Catch-The-Ball")
<span class="variable">root2</span>.<span class="function">resizable</span>(False, False)
<span class="variable">canvas2</span> = <span class="function">Canvas</span>(<span class="variable">root2</span>, width=300, height=300)
<span class="variable">canvas2</span>.<span class="function">pack</span>()
w = <span class="function">Label</span>(<span class="variable">canvas2</span>, text="\nOOPS...GAME IS OVER\n\nYOUR SCORE = "
+ str(<span class="variable">score</span>) + "\n\n")
w.<span class="function">pack</span>()
button3 = <span class="function">Button</span>(<span class="variable">canvas2</span>, text="PLAY AGAIN", bg="#ffb31a",
command=lambda: play_again(<span class="variable">root2</span>))
button3.<span class="function">pack</span>()
button4 = <span class="function">Button</span>(<span class="variable">canvas2</span>, text="EXIT", bg="#ffb31a",
command=lambda: <span class="function">exit_handler</span>(<span class="variable">root2</span>))
button4.<span class="function">pack</span>()
<span class="comment"># Function for handling the play again request</span>
<span class="in-built">def</span> play_again(<span class="variable">root2</span>):
<span class="variable">root2</span>.<span class="function">destroy</span>()
<span class="function">main</span>()
<span class="comment"># Function for handling exit request</span>
<span class="in-built">def</span> <span class="function">exit_handler</span>(<span class="variable">root2</span>):
<span class="variable">root2</span>.<span class="function">destroy</span>()
<span class="variable">tk</span>.<span class="function">destroy</span>()
<span class="comment"># Main function</span>
<span class="in-built">def</span> <span class="function">main</span>():
<span class="in-built">global</span> <span class="variable">score</span>, <span class="variable">dist</span>
<span class="variable">score</span> = 0
<span class="variable">dist</span> = 0
<span class="comment"># defining the dimensions of bar</span>
bar1 = bar(<span class="variable">canvas</span>, 5, 560, 45, 575)
button = <span class="function">Button</span>(<span class="variable">canvas</span>, text="==>", bg="#00ff00",
command=lambda: bar1.<span class="function">move_bar</span>(1))
<span class="comment"># placing the buttons at suitable location on the canvas</span>
button.place(x=300, y=580)
button2 = <span class="function">Button</span>(<span class="variable">canvas</span>, text="<==", bg="#00ff00",
command=lambda: bar1.<span class="function">move_bar</span>(0))
button2.place(x=260, y=580)
<span class="function">ball_set</span>()
<span class="variable">tk</span>.<span class="function">mainloop</span>()
<span class="comment"># defining the dimensions of bar</span>
<span class="in-built">if</span> (__name__ == "__main__"):
<span class="function">main</span>()
</pre>
<!-- CODE -->
<br>
<div class="images">
<div id="carouselExampleFade" class="carousel slide carousel-fade" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleCaptions" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleCaptions" data-slide-to="1"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img src="img/catch-the-ball-1.PNG" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="img/catch-the-ball-2.PNG" class="d-block w-100" alt="...">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleFade" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleFade" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<div class="buyme">
<a href="https://ko-fi.com/hhhrrrttt222111" target="_blank" rel="noopener noreferrer"><span class="buy">Buy Me A Coffee</span><img src="../assets/icons/kofi.png" alt="ko-fi" class="coffee"></a>
</div>
</div>
</div>
</div>
<div class="footer">
<p class="footer-heart">
Made with <g-emoji class="g-emoji" alias="heart" fallback-src="https://raw.githubusercontent.com/hhhrrrttt222111/etudia/master/assets/necro.png?token=AKLVDPY75U5YNHZ7PRA4Y4K6TRB2I">
<img class="emoji" alt="heart" height="20" width="20" src="https://raw.githubusercontent.com/hhhrrrttt222111/etudia/master/assets/necro.png?token=AKLVDPY75U5YNHZ7PRA4Y4K6TRB2I">
</g-emoji> by <a href="http://hhhrrrttt222111.me/" target="_blank">HRT</a>
</p>
</div>
<script>
function goBack() {
window.history.back();
}
</script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>