JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="myCanvas" width="400" height="400">
Sorry, but your browser does not support the HTML5 canvas tag.
</canvas>
CSS
canvas {
border: 1px solid black;
}
JavaScript
// "Crappy PONG" -- step by step
//
// Step 13: Simplify
/*
Supporting timer-events (via setInterval) *and* frame-events (via requestAnimationFrame)
adds significant complexity to the the code.
I can simplify things a little by focusing on the latter case only (which is the
superior mechanism of the two), so let's try doing that...
The "MAINLOOP" code, inside g_main, is much simplified as a result.
*/
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
var g_canvas = document.getElementById("myCanvas");
var g_ctx = g_canvas.getContext("2d");
/*
0 1 2 3 4 5 6 7 8 9
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// =================
// KEYBOARD HANDLING
// =================
var g_keys = [];
function handleKeydown(evt) {
g_keys[evt.keyCode] = true;
}
function handleKeyup(evt) {
g_keys[evt.keyCode] = false;
}
// Inspects, and then clears, a key's state
//
// This allows a keypress to be "one-shot" e.g. for toggles
// ..until the auto-repeat kicks in, that is.
//
function eatKey(keyCode) {
var isDown = g_keys[keyCode];
g_keys[keyCode] = false;
return isDown;
}
window.addEventListener("keydown", handleKeydown);
window.addEventListener("keyup", handleKeyup);
// ============
// PADDLE STUFF
// ============
// COMMON PADDLE STUFF
// A generic contructor which accepts an arbitrary descriptor object
function Paddle(descr) {
for (var property in descr) {
this[property] = descr[property];
}
}
// Add these properties to the prototype, where they will server as
// shared defaults, in the absence of an instance-specific overrides.
Paddle.prototype.halfWidth = 10;
Paddle.prototype.halfHeight = 50;
Paddle.prototype.update = function (du) {
if (g_keys[this.GO_UP]) {
this.cy -= 5 * du;
} else if (g_keys[this.GO_DOWN]) {
this.cy += 5 * du;
...