forked from kbenavides1312/games_PyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPong.html
More file actions
244 lines (182 loc) · 11.4 KB
/
Pong.html
File metadata and controls
244 lines (182 loc) · 11.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
<!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 - Pong</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">Pong</h1>
<p class="game-desc">Very similar to table tennis, you can control a paddle by moving it vertically across the left or right side of
the screen and hitting a ball back and forth. Points are earned when one fails to return the ball to the other.
The goal? Reach eleven points before the opponent.</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/Pong.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/Pong.txt" download><i class="fa fa-2x fa-file"></i></a>
<a class="btn" href="../Games/Pong.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">
# This game is from the official documentation of freegames
# https://pypi.org/project/freegames/
# pip install freegames
# Player 1 Controls - Move Up: 'w', Move Down: 's'
# Player 2 Controls - Move Up: 'i', Move Down: 'k'
# import modules </span>
<span class="in-built">from</span> random <span class="in-built">import</span> choice, random
<span class="in-built">import</span> turtle as t
<span class="in-built">from</span> freegames <span class="in-built">import</span> vector
<span class="comment"># Set window title, color and icon</span>
t.<span class="function">title</span>("Pong")
<span class="variable">root</span> = t.<span class="function">Screen</span>()._root
<span class="variable">root</span>.<span class="function">iconbitmap</span>("logo-ico.ico")
t.<span class="function">bgcolor</span>('#cc66ff')
<span class="comment"> #Functions
# Randomly generate value between (-5, -3) or (3, 5) </span>
<span class="in-built">def</span> value():
<span class="in-built">return</span> (3 + random() * 2) * choice([1, -1])
<span class="variable">ball</span> = vector(0, 0)
<span class="variable">aim</span> = vector(value(), value())
<span class="variable">state</span> = {1: 0, 2: 0}
<span class="comment"># Move player position by change</span>
<span class="in-built">def</span> <span class="function">move</span>(player, change):
<span class="variable">state</span>[player] += change
<span class="comment"># Draw rectangle at (x, y) with given width and height</span>
<span class="in-built">def</span> <span class="function">rectangle</span>(x, y, width, height):
t.<span class="function">up</span>()
t.<span class="function">goto</span>(x, y)
t.down()
t.begin_fill()
<span class="in-built">for</span> count in range(2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.end_fill()
<span class="comment"># Draw game and move pong ball</span>
<span class="in-built">def</span> draw():
t.clear()
<span class="function">rectangle</span>(-200, <span class="variable">state</span>[1], 10, 50)
<span class="function">rectangle</span>(190, <span class="variable">state</span>[2], 10, 50)
<span class="variable">ball</span>.<span class="function">move</span>(<span class="variable">aim</span>)
x = <span class="variable">ball</span>.x
y = <span class="variable">ball</span>.y
t.<span class="function">up</span>()
t.<span class="function">goto</span>(x, y)
t.<span class="function">dot</span>(10)
t.<span class="function">update</span>()
<span class="in-built">if</span> y < -200 or y > 200:
<span class="variable">aim</span>.y = -<span class="variable">aim</span>.y
<span class="in-built">if</span> x < -185:
<span class="variable">low</span> = <span class="variable">state</span>[1]
<span class="variable">high</span> = <span class="variable">state</span>[1] + 50
<span class="in-built">if</span> <span class="variable">low</span> <= y <= <span class="variable">high</span>:
<span class="variable">aim</span>.x = -<span class="variable">aim</span>.x
<span class="in-built">else</span>:
<span class="in-built">return</span>
<span class="in-built">if</span> x > 185:
<span class="variable">low</span> = <span class="variable">state</span>[2]
<span class="variable">high</span> = <span class="variable">state</span>[2] + 50
<span class="in-built">if</span> <span class="variable">low</span> <= y <= <span class="variable">high</span>:
<span class="variable">aim</span>.x = -<span class="variable">aim</span>.x
<span class="in-built">else</span>:
<span class="in-built">return</span>
t.<span class="function">ontimer</span>(draw, 50)
t.<span class="function">setup</span>(420, 420, 370, 0)
t.<span class="function">hideturtle</span>()
t.<span class="function">tracer</span>(False)
t.<span class="function">listen</span>()
<span class="comment"># Set Keyboard Controls</span>
t.<span class="function">onkey</span>(lambda: <span class="function">move</span>(1, 20), 'w')
t.<span class="function">onkey</span>(lambda: <span class="function">move</span>(1, -20), 's')
t.<span class="function">onkey</span>(lambda: <span class="function">move</span>(2, 20), 'i')
t.<span class="function">onkey</span>(lambda: <span class="function">move</span>(2, -20), 'k')
draw()
t.<span class="function">done</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/Pong-1.PNG" class="d-block w-100" alt="...">
</div>
<div class="carousel-item">
<img src="img/Pong-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>