JAVASCRIPT Tutorial
Game Development is the process of designing, developing, and maintaining video games.
// Create a canvas
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions
canvas.width = 500;
canvas.height = 500;
// Create a player object
const player = {
x: 0,
y: 0,
speed: 5,
};
// Handle user input
document.addEventListener('keydown', e => {
if (e.key === 'ArrowUp') player.y -= player.speed;
if (e.key === 'ArrowDown') player.y += player.speed;
if (e.key === 'ArrowLeft') player.x -= player.speed;
if (e.key === 'ArrowRight') player.x += player.speed;
});
// Draw the player
function drawPlayer() {
ctx.fillStyle = 'blue';
ctx.fillRect(player.x, player.y, 10, 10);
}
// Game loop
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
requestAnimationFrame(gameLoop);
}
// Start the game
gameLoop();
This example demonstrates:
requestAnimationFrame
.