CoolFace
Apppublic

Chronos234/test1

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
1likes
script.js64 linesDownload Raw Back to root
1const canvas = document.getElementById("gameCanvas");2const ctx = canvas.getContext("2d");3 4let width = window.innerWidth;5let height = window.innerHeight;6 7canvas.width = width;8canvas.height = height;9 10let star = {11    x: width / 2,12    y: height / 2,13    radius: 50,14    color: "yellow",15};16 17let towers = [];18let enemies = [];19 20function drawStar() {21    ctx.beginPath();22    ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2);23    ctx.fillStyle = star.color;24    ctx.fill();25}26 27function drawTowers() {28    towers.forEach(tower => {29        ctx.beginPath();30        ctx.arc(tower.x, tower.y, tower.radius, 0, Math.PI * 2);31        ctx.fillStyle = "blue";32        ctx.fill();33    });34}35 36function drawEnemies() {37    enemies.forEach(enemy => {38        ctx.beginPath();39        ctx.arc(enemy.x, enemy.y, enemy.radius, 0, Math.PI * 2);40        ctx.fillStyle = "red";41        ctx.fill();42    });43}44 45function spawnEnemies() {46    let enemy = {47        x: Math.random() * width,48        y: 0,49        radius: 20,50        speed: 2,51    };52    enemies.push(enemy);53}54 55function update() {56    ctx.clearRect(0, 0, canvas.width, canvas.height);57    drawStar();58    drawTowers();59    drawEnemies();60    spawnEnemies();61    requestAnimationFrame(update);62}63 64update();