kontra
Version:
Kontra HTML5 game development library
44 lines (40 loc) • 1.02 kB
HTML
<html>
<head>
<title>Moving A Sprite</title>
<script src="../../kontra.js"></script>
</head>
<body>
<canvas id="game" width="600" height="400" style="background: #333331"></canvas>
<script id="code">
// initialize the game and setup the canvas
let { canvas, context } = kontra.init();
// create a basic sprite with a velocity
window.sprite = kontra.Sprite({
x: 290,
y: 180,
dx: 3,
dy: 0,
width: 20,
height: 40,
color: 'red'
});
// create the game loop to update and render the sprite
window.loop = kontra.GameLoop({
update: function() {
sprite.update();
// reset the sprites position when it reaches the edge of the game
if (sprite.x > canvas.width) {
sprite.x = -sprite.width;
}
},
render: function() {
sprite.render();
}
});
// start the loop
loop.start();
</script>
<script src="../prism/codeOutput.js"></script>
</body>
</html>