-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
244 lines (210 loc) · 7.27 KB
/
index.html
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.0">
<title>Rust Breakout</title>
<style type="text/css">
.div1 {
text-align: center;
}
.can1 {
width: 60%;
height: auto;
}
input {
font-size: 24px;
}
.con1 {
text-align: center;
}
.con2 {
text-align: center;
}
body {
background-color: #000000;
}
a {
color: rgb(255,255,255);
}
.span1 {
color: rgb(255,255,255);
font-size: 32px;
}
</style>
</head>
<body>
<script type="module">
// Function to generate a random number between min and max (inclusive)
export function random_range(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Import the WASM module
import init, { Breakout, Ball, Paddle, Grid, example_random_range_usage } from './web/breakout.js';
var breakout;
var paddle;
var WIDTH=960;
var HEIGHT=720;
var TILE_W=960/32;
var TILE_H=720/2/16;
async function run() {
await init();
breakout = new Breakout();
paddle = breakout.paddle;
main();
}
run();
var can = document.getElementById("can1");
var context = can.getContext("2d");
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
var randomColors = [];
function setRandom() {
randomColors = [];
for (let i = 0; i < 100; i++) {
randomColors.push(getRandomColor());
}
}
function main() {
setRandom();
breakout.new_game();
window.addEventListener("keydown", keyPressed, true);
window.addEventListener('touchstart', touchStart, true);
window.addEventListener('touchmove', touchMove, true);
window.addEventListener("mousedown", mouseDown, true);
window.addEventListener("mousemove", mouseMoveB, true);
// Start the animation loop
lastTimestamp = performance.now();
requestAnimationFrame(loop);
}
var downX = 0, downY = 0, upX = 0, upY = 0;
var old_down_X = 0;
var old_pos = 0;
var is_down = false;
var is_moved = false;
var touchStartX = 0;
var speedMultiplier = 3;
function moveLeft() {
if (paddle.x > 0) {
paddle.x -= 40;
breakout.set_paddle_xy(paddle.x, paddle.y);
}
}
function moveRight() {
if (paddle.x < 960-210) {
paddle.x += 40;
breakout.set_paddle_xy(paddle.x, paddle.y);
}
}
function touchStart(e) {
touchStartX = e.touches[0].clientX;
e.stopPropagation();
e.preventDefault();
}
function touchMove(e) {
var canvas = document.getElementById('can1');
var rect = canvas.getBoundingClientRect();
var touchCurrentX = e.touches[0].clientX;
var deltaX = (touchCurrentX - touchStartX) * speedMultiplier;
var newPaddleX = paddle.x + deltaX;
var paddleWidth = 200;
if (newPaddleX < 0) {
newPaddleX = 0;
} else if (newPaddleX + paddleWidth > canvas.width) {
newPaddleX = canvas.width - paddleWidth;
}
paddle.x = newPaddleX;
breakout.set_paddle_xy(paddle.x, paddle.y);
touchStartX = touchCurrentX;
e.stopPropagation();
e.preventDefault();
}
function mouseDown(e) {}
function mouseMove(e) {
var canvas = document.getElementById('can1');
var rect = canvas.getBoundingClientRect();
var xpos = e.clientX - rect.left;
var ypos = e.clientY - rect.top;
if (xpos > 0 && xpos + 200 < canvas.width) {
breakout.set_paddle_xy(xpos, paddle.y);
}
e.stopPropagation();
e.preventDefault();
}
function mouseMoveB(e) {
var canvas = document.getElementById('can1');
var rect = canvas.getBoundingClientRect();
var xpos = e.clientX - rect.left;
var ypos = e.clientY - rect.top;
if (xpos > 0 && xpos + 200 < canvas.width) {
breakout.set_paddle_xy(xpos, paddle.y);
}
e.stopPropagation();
e.preventDefault();
}
function keyPressed(key) {
if (key.keyCode == 37) {
if (paddle.x > 0) {
paddle.x -= 20;
breakout.set_paddle_xy(paddle.x, paddle.y);
}
} else if (key.keyCode == 39) {
if (paddle.x < 960-210) {
paddle.x += 20;
breakout.set_paddle_xy(paddle.x, paddle.y);
}
}
}
function drawScreen() {
context.fillStyle = "#000000";
context.fillRect(0, 0, WIDTH, HEIGHT);
let tile_w = WIDTH / 32;
let tile_h = HEIGHT / 2 / 16;
for (var x = 0; x < tile_w - 1; ++x) {
for (var y = 0; y < tile_h - 1; ++y) {
context.fillStyle = "#BD0000";
let pos = breakout.grid.index(x, y);
if (pos == 0) continue;
context.fillStyle = randomColors[pos];
context.fillRect(x * 32, y * 16, 32, 16);
}
}
context.fillStyle = "#FFFFFF";
context.fillRect(breakout.ball.x, breakout.ball.y, 16, 16);
context.fillStyle = "#FFFFFF";
context.fillRect(breakout.paddle.x, breakout.paddle.y, 200, 20);
context.fillStyle = randomColors[random_range(0, 10)];
context.font = "14px Verdana";
context.fillText("Score: " + breakout.score + " Lives: " + breakout.lives, 15, 690);
if (breakout.update()) {
setRandom();
}
}
let lastTimestamp = 0;
let accumulatedTime = 0;
const FRAME_DURATION = 25; // Desired frame duration in milliseconds
function loop(timestamp) {
const deltaTime = timestamp - lastTimestamp;
lastTimestamp = timestamp;
accumulatedTime += deltaTime;
if (accumulatedTime >= FRAME_DURATION) {
drawScreen();
accumulatedTime -= FRAME_DURATION;
}
requestAnimationFrame(loop);
}
</script>
<div class="con2">
<span class="span1"><b>Breakout Rust</b></span>
</div><br>
<div class="div1">
<canvas class="can1" id="can1" width="960" height="720"></canvas>
</div>
</body>
</html>