GTA 5 fan made game script.js code
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game map (placeholder city map)
const mapWidth = 1600;
const mapHeight = 1200;
// Player object
const player = {
x: 400,
y: 300,
width: 30,
height: 30,
speed: 4,
color: 'aqua'
};
// Handle keypresses
const keys = {};
document.addEventListener('keydown', e => keys[e.key] = true);
document.addEventListener('keyup', e => keys[e.key] = false);
// Game loop
function update() {
if (keys['ArrowUp'] || keys['w']) player.y -= player.speed;
if (keys['ArrowDown'] || keys['s']) player.y += player.speed;
if (keys['ArrowLeft'] || keys['a']) player.x -= player.speed;
if (keys['ArrowRight'] || keys['d']) player.x += player.speed;
draw();
requestAnimationFrame(update);
}
// Draw map and player
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Simulate a mini Los Santos grid
for (let i = 0; i < mapWidth; i += 100) {
ctx.strokeStyle = "#555";
ctx.beginPath();
ctx.moveTo(i - player.x + 400, 0);
ctx.lineTo(i - player.x + 400, canvas.height);
ctx.stroke();
}
for (let j = 0; j < mapHeight; j += 100) {
ctx.beginPath();
ctx.moveTo(0, j - player.y + 300);
ctx.lineTo(canvas.width, j - player.y + 300);
ctx.stroke();
}
// Draw player
ctx.fillStyle = player.color;
ctx.fillRect(400 - player.width / 2, 300 - player.height / 2, player.width, player.height);
}
// Start game
update();
Comments
Post a Comment