forked from asweigart/inventwithpython3rded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.html
More file actions
247 lines (214 loc) · 6.27 KB
/
Copy pathanimation.html
File metadata and controls
247 lines (214 loc) · 6.27 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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Animation</title>
<script src="../jsgame0.js"></script>
<style type="text/css" media="screen">
body {
background-color: white;
color: black;
}
.hidden {
display: none;
}
#original {
margin-left: 1em;
}
</style>
</head>
<body>
<main>
<h1>Animation</h1>
<canvas id="screen">
The game screen appears here if your browser supports the Canvas API.
</canvas>
<section id="controls">
<button type="button" id="reset">Reset</button>
<button type="button" id="pause">Pause</button>
</section>
<h2>Attribution</h2>
<p>From chapter 17.</p>
<p>Licensed under <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/us/legalcode">Creative Commons BY-NC-SA</a>.</p>
<h2>Original Python code</h2>
<pre id="original"><code>
import pygame, sys, time
from pygame.locals import *
# set up pygame
pygame.init()
# set up the window
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Animation')
# set up direction variables
DOWNLEFT = 1
DOWNRIGHT = 3
UPLEFT = 7
UPRIGHT = 9
MOVESPEED = 4
# set up the colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# set up the block data structure
b1 = {'rect':pygame.Rect(300, 80, 50, 100), 'color':RED, 'dir':UPRIGHT}
b2 = {'rect':pygame.Rect(200, 200, 20, 20), 'color':GREEN, 'dir':UPLEFT}
b3 = {'rect':pygame.Rect(100, 150, 60, 60), 'color':BLUE, 'dir':DOWNLEFT}
blocks = [b1, b2, b3]
# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# draw the black background onto the surface
windowSurface.fill(BLACK)
for b in blocks:
# move the block data structure
if b['dir'] == DOWNLEFT:
b['rect'].left -= MOVESPEED
b['rect'].top += MOVESPEED
if b['dir'] == DOWNRIGHT:
b['rect'].left += MOVESPEED
b['rect'].top += MOVESPEED
if b['dir'] == UPLEFT:
b['rect'].left -= MOVESPEED
b['rect'].top -= MOVESPEED
if b['dir'] == UPRIGHT:
b['rect'].left += MOVESPEED
b['rect'].top -= MOVESPEED
# check if the block has move out of the window
if b['rect'].top < 0:
# block has moved past the top
if b['dir'] == UPLEFT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = DOWNRIGHT
if b['rect'].bottom > WINDOWHEIGHT:
# block has moved past the bottom
if b['dir'] == DOWNLEFT:
b['dir'] = UPLEFT
if b['dir'] == DOWNRIGHT:
b['dir'] = UPRIGHT
if b['rect'].left < 0:
# block has moved past the left side
if b['dir'] == DOWNLEFT:
b['dir'] = DOWNRIGHT
if b['dir'] == UPLEFT:
b['dir'] = UPRIGHT
if b['rect'].right > WINDOWWIDTH:
# block has moved past the right side
if b['dir'] == DOWNRIGHT:
b['dir'] = DOWNLEFT
if b['dir'] == UPRIGHT:
b['dir'] = UPLEFT
# draw the block onto the surface
pygame.draw.rect(windowSurface, b['color'], b['rect'])
# draw the window onto the screen
pygame.display.update()
time.sleep(0.02)
</code></pre>
</main>
<script>
// set up the window
WIDTH = 400;
HEIGHT = 400;
TITLE = 'Animation';
// set up direction variables
const Direction = Object.freeze({
DOWNLEFT: 1,
DOWNRIGHT: 3,
UPLEFT: 7,
UPRIGHT: 9
});
const MOVESPEED = 4;
// set up the block data structure
var blocks;
function reset() {
let b1 = new Rect(300, 80, 50, 100),
b2 = new Rect(200, 200, 20, 20),
b3 = new Rect(100, 150, 60, 60);
b1.color = 'red';
b1.dir = Direction.UPRIGHT;
b2.color = 'green';
b2.dir = Direction.UPLEFT;
b3.color = 'blue';
b3.dir = Direction.DOWNLEFT;
blocks = [b1, b2, b3];
}
function draw() {
// draw the black background onto the surface
screen.fill('black');
for (let b of blocks) {
// draw the block onto the surface
screen.draw.filled_rect(b, b.color);
}
}
function update() {
for (let b of blocks) {
// move the block data structure
if (b.dir === Direction.DOWNLEFT) {
b.left -= MOVESPEED;
b.top += MOVESPEED;
}
if (b.dir === Direction.DOWNRIGHT) {
b.left += MOVESPEED;
b.top += MOVESPEED;
}
if (b.dir === Direction.UPLEFT) {
b.left -= MOVESPEED;
b.top -= MOVESPEED;
}
if (b.dir === Direction.UPRIGHT) {
b.left += MOVESPEED;
b.top -= MOVESPEED;
}
// check if the block has move out of the window
if (b.top < 0) {
// block has moved past the top
if (b.dir === Direction.UPLEFT) {
b.dir = Direction.DOWNLEFT;
}
if (b.dir === Direction.UPRIGHT) {
b.dir = Direction.DOWNRIGHT;
}
}
if (b.bottom > HEIGHT) {
// block has moved past the bottom
if (b.dir === Direction.DOWNLEFT) {
b.dir = Direction.UPLEFT;
}
if (b.dir === Direction.DOWNRIGHT) {
b.dir = Direction.UPRIGHT;
}
}
if (b.left < 0) {
// block has moved past the left side
if (b.dir === Direction.DOWNLEFT) {
b.dir = Direction.DOWNRIGHT;
}
if (b.dir === Direction.UPLEFT) {
b.dir = Direction.UPRIGHT;
}
}
if (b.right > WIDTH) {
// block has moved past the right side
if (b.dir === Direction.DOWNRIGHT) {
b.dir = Direction.DOWNLEFT;
}
if (b.dir === Direction.UPRIGHT) {
b.dir = Direction.UPLEFT;
}
}
}
}
window.addEventListener('load', (event) => {
screen.init();
});
</script>
</body>
</html>