forked from asweigart/inventwithpython3rded
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygameInput.html
More file actions
244 lines (203 loc) · 6.44 KB
/
Copy pathpygameInput.html
File metadata and controls
244 lines (203 loc) · 6.44 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-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Input</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>Input</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 18.</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, random
from pygame.locals import *
# set up pygame
pygame.init()
mainClock = pygame.time.Clock()
# set up the window
WINDOWWIDTH = 400
WINDOWHEIGHT = 400
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Input')
# set up the colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
WHITE = (255, 255, 255)
# set up the player and food data structure
foodCounter = 0
NEWFOOD = 40
FOODSIZE = 20
player = pygame.Rect(300, 100, 50, 50)
foods = []
for i in range(20):
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT - FOODSIZE), FOODSIZE, FOODSIZE))
# set up movement variables
moveLeft = False
moveRight = False
moveUp = False
moveDown = False
MOVESPEED = 6
# run the game loop
while True:
# check for events
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
# change the keyboard variables
if event.key == K_LEFT or event.key == ord('a'):
moveRight = False
moveLeft = True
if event.key == K_RIGHT or event.key == ord('d'):
moveLeft = False
moveRight = True
if event.key == K_UP or event.key == ord('w'):
moveDown = False
moveUp = True
if event.key == K_DOWN or event.key == ord('s'):
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_LEFT or event.key == ord('a'):
moveLeft = False
if event.key == K_RIGHT or event.key == ord('d'):
moveRight = False
if event.key == K_UP or event.key == ord('w'):
moveUp = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if event.key == ord('x'):
player.top = random.randint(0, WINDOWHEIGHT - player.height)
player.left = random.randint(0, WINDOWWIDTH - player.width)
if event.type == MOUSEBUTTONUP:
foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE))
foodCounter += 1
if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT - FOODSIZE), FOODSIZE, FOODSIZE))
# draw the black background onto the surface
windowSurface.fill(BLACK)
# move the player
if moveDown and player.bottom < WINDOWHEIGHT:
player.top += MOVESPEED
if moveUp and player.top > 0:
player.top -= MOVESPEED
if moveLeft and player.left > 0:
player.left -= MOVESPEED
if moveRight and player.right < WINDOWWIDTH:
player.right += MOVESPEED
# draw the player onto the surface
pygame.draw.rect(windowSurface, WHITE, player)
# check if the player has intersected with any food squares.
for food in foods[:]:
if player.colliderect(food):
foods.remove(food)
# draw the food
for i in range(len(foods)):
pygame.draw.rect(windowSurface, GREEN, foods[i])
# draw the window onto the screen
pygame.display.update()
mainClock.tick(40)
</code></pre>
</main>
<script>
// set up the window
WIDTH = 400;
HEIGHT = 400;
TITLE = 'Input';
const MOVESPEED = 6;
const NEWFOOD = 40;
const FOODSIZE = 20;
/*
* Return a random integer N such that min <= N < max.
*/
function getRandomInteger(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor((Math.random() * (max - min)) + min);
}
// set up the player and food data structure
var foodCounter, player, foods;
function reset() {
foodCounter = 0;
player = new Rect(300, 100, 50, 50);
foods = [];
for (let i = 0; i < 20; i++) {
foods.push(new Rect(getRandomInteger(0, WIDTH - FOODSIZE + 1), getRandomInteger(0, HEIGHT - FOODSIZE + 1), FOODSIZE, FOODSIZE));
}
}
function draw() {
// draw the black background onto the surface
screen.fill('black');
// draw the player onto the surface
screen.draw.filled_rect(player, 'white');
// draw the food
for (let f of foods) {
screen.draw.filled_rect(f, 'green');
}
}
function update() {
foodCounter += 1;
if (foodCounter >= NEWFOOD) {
// add new food
foodCounter = 0;
foods.push(new Rect(getRandomInteger(0, WIDTH - FOODSIZE + 1), getRandomInteger(0, HEIGHT - FOODSIZE + 1), FOODSIZE, FOODSIZE));
}
// move the player
if ((keyboard[keys.DOWN] || keyboard[keys.S]) && (player.bottom < HEIGHT)) {
player.top += MOVESPEED;
}
if ((keyboard[keys.UP] || keyboard[keys.W]) && (player.top > 0)) {
player.top -= MOVESPEED;
}
if ((keyboard[keys.LEFT] || keyboard[keys.A]) && (player.left > 0)) {
player.left -= MOVESPEED;
}
if ((keyboard[keys.RIGHT] || keyboard[keys.D]) && (player.right < WIDTH)) {
player.right += MOVESPEED;
}
if (keyboard[keys.X]) {
player.top = getRandomInteger(0, HEIGHT - player.height + 1);
player.left = getRandomInteger(0, WIDTH - player.width + 1);
}
// check if the player has intersected with any food squares.
foods = foods.filter(f => !player.colliderect(f));
}
function on_mouse_down(pos) {
foods.push(new Rect(pos[0], pos[1], FOODSIZE, FOODSIZE));
}
window.addEventListener('load', (event) => {
screen.init();
});
</script>
</body>
</html>