Skip to content

Instantly share code, notes, and snippets.

@hogivano
Last active March 4, 2022 11:08
Show Gist options
  • Save hogivano/ed957cf2d93cd5cd701b892a22e71a89 to your computer and use it in GitHub Desktop.
Save hogivano/ed957cf2d93cd5cd701b892a22e71a89 to your computer and use it in GitHub Desktop.
Apple Canvas
<html>
<head>
</head>
<script type="text/javascript" src="index.js"></script>
<body onload="draw();">
<h1>Snake</h1>
<canvas id="snakeBoard" width="400" height="400" style="border:1px solid #000000;"></canvas>
<canvas id="score1Board" width="100" height="100" style="border:1px solid #000000;"></canvas>
<canvas id="score2Board" width="100" height="100" style="border:1px solid #000000;"></canvas>
</body>
</html>
// hapus tag image yang di index.html
let apple = {
color: "red",
position: initPosition(),
image: './apple.png',
}
function drawCell(ctx, x, y, color, img = null) {
if (img != null) {
image = new Image();
image.src = img;
image.onload = function(){
ctx.drawImage(image, x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
} else {
ctx.fillStyle = color;
ctx.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment