!DOCTYPE HTML - TXT Gun Game
!DOCTYPE HTML - TXT Gun Game
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Gun Game</title>
<style>
canvas {
border: 1px solid black;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="canvas" width="600" height="400"></canvas>
<script>
// Define the canvas and context
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
// Player object
const player = {
x: 50,
y: canvas.height / 2,
width: 50,
height: 30,
color: 'blue',
speed: 5
};
// Bullet object
const bullet = {
x: 0,
y: 0,
width: 10,
height: 5,
speed: 10,
color: 'red',
fire: false
};
// Update function
function update() {
// Move bullet
if(bullet.fire) {
bullet.x += bullet.speed;
if(bullet.x > canvas.width) {
bullet.fire = false;
}
}
// Draw everything
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawPlayer();
drawBullet();