Mini RDR2/Zombi 2D
body { margin: 0; overflow: hidden; background: #555; }
canvas { display: block; margin: auto; background: #888; }
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const ******** = new Audio('https://freesound.org/data/previews/320/320654_4939433-lq.mp3');
const bombSound = new Audio('https://freesound.org/data/previews/235/235968_4020406-lq.mp3');
const player = {
x: 400, y: 500, width: 50, height: 50, speed: 5,
health: 100, level: 1, xp: 0
};
class Enemy {
constructor(x,y,speed){
this.x = x;
this.y = y;
this.width = 50;
this.height = 50;
this.health = 50;
this.speed = speed;
}
}
let enemies = [];
const maxEnemies = 5;
let baseEnemySpeed = 1.5;
function spawnEnemy(){
enemies.push(new Enemy(Math.random()*(canvas.width-50), Math.random()*150, baseEnemySpeed));
}
for(let i=0;i<maxEnemies;i++) spawnEnemy();
let bullets = [];
const bulletSpeed = 10;
let bombs = [];
function placeBomb(x,y){
bombs.push({x,y,width:30,height:30,explode:false, timer:100});
}
let keys = {};
document.addEventListener('keydown',(e)=>{
keys[e.key] = true;
const fireDir = {
w:{dx:0,dy:-1},
a:{dx:-1,dy:0},
s:{dx:0,dy:1},
d:{dx:1,dy:0}
};
if(fireDir[e.key]){
bullets.push({
x: player.x + player.width/2,
y: player.y + player.height/2,
dx: fireDir[e.key].dx,
dy: fireDir[e.key].dy,
width:6,
height:6
});
********.currentTime=0;
********.play();
}
if(e.key==='b'){
placeBomb(player.x, player.y);
}
});
document.addEventListener('keyup',(e)=>{ keys[e.key]=false; });
function gameLoop(){
ctx.clearRect(0,0,canvas.width,canvas.height);
if(keys['ArrowLeft'] && player.x>0) player.x-=player.speed;
if(keys['ArrowRight'] && player.x<canvas.width-player.width) player.x+=player.speed;
if(keys['ArrowUp'] && player.y>0) player.y-=player.speed;
if(keys['ArrowDown'] && player.y<canvas.height-player.height) player.y+=player.speed;
ctx.fillStyle='saddlebrown';
ctx.fillRect(player.x, player.y, player.width, player.height);
bullets.forEach((b,i)=>{
b.x += (b.dx || 0) * bulletSpeed;
b.y += (b.dy || -1) * bulletSpeed;
ctx.fillStyle='red';
ctx.fillRect(b.x,b.y,b.width,b.height);
if(b.x<0 || b.x>canvas.width || b.y<0 || b.y>canvas.height){
bullets.splice(i,1);
}
});
enemies.forEach((eIndex,e)=>{
if(eIndex.x<player.x) eIndex.x+=eIndex.speed;
else if(eIndex.x>player.x) eIndex.x-=eIndex.speed;
if(eIndex.y<player.y) eIndex.y+=eIndex.speed;
else if(eIndex.y>player.y) eIndex.y-=eIndex.speed;
ctx.fillStyle='green';
ctx.fillRect(eIndex.x,eIndex.y,eIndex.width,eIndex.height);
// 💥 Oyuncuya çarpınca hasar
if(player.x < eIndex.x + eIndex.width &&
player.x + player.width > eIndex.x &&
player.y < eIndex.y + eIndex.height &&
player.y + player.height > eIndex.y){
player.health -= 0.2;
if(player.health <= 0){
alert("Öldün!");
location.reload();
}
}
bullets.forEach((b,bi)=>{
if(b.x < eIndex.x+eIndex.width && b.x+b.width > eIndex.x &&
b.y < eIndex.y+eIndex.height && b.y+b.height > eIndex.y){
let damage = (b.y < eIndex.y+eIndex.height/3) ? 50 : 25;
eIndex.health-=damage;
bullets.splice(bi,1);
if(eIndex.health<=0){
enemies.splice(eIndex,1);
player.xp+=20;
if(player.xp>=player.level*100){
player.level+=1;
player.health=100;
player.xp=0;
baseEnemySpeed += 0.5;
}
setTimeout(spawnEnemy,2000);
}
}
});
});
bombs.forEach((b,i)=>{
b.timer--;
if(b.timer<=0 && !b.explode){
b.explode=true;
bombSound.currentTime=0;
bombSound.play();
enemies.forEach((e,eIndex)=>{
if(e.x < b.x+100 && e.x+e.width > b.x &&
e.y < b.y+100 && e.y+e.height > b.y){
enemies.splice(eIndex,1);
player.xp+=20;
setTimeout(spawnEnemy,2000);
}
});
}
ctx.fillStyle=b.explode?'orange':'black';
ctx.fillRect(b.x,b.y,b.width,b.height);
if(b.explode) bombs.splice(i,1);
});
ctx.fillStyle='white';
ctx.font='20px Arial';
ctx.fillText(`Can: ${player.health.toFixed(0)}`,10,20);
ctx.fillText(`Seviye: ${player.level}`,10,45);
ctx.fillText(`XP: ${player.xp}/${player.level*100}`,10,70);
ctx.fillText(`Zombi Hızı: ${baseEnemySpeed.toFixed(1)}`,10,95);
requestAnimationFrame(gameLoop);
}
gameLoop();
Sign in to FutureMe
or use your email address
Create an account
or use your email address
FutureMe uses cookies, read how
Share this FutureMe letter
Copy the link to your clipboard:
Or share directly via social media:
Why is this inappropriate?