snoopy / index.html
dejanb's picture
Snoopy igrica: index.html + sprajtovi
d5ac3a9 verified
Raw History Blame Contribute Delete
6.83 kB
<!DOCTYPE html>
<html lang="sr">
<head>
<meta charset="UTF-8">
<title>Snoopy: Lovac na kosti</title>
<style>
* { margin:0; padding:0; box-sizing:border-box; }
body {
background:#1a2f1a; display:flex; flex-direction:column; align-items:center;
justify-content:center; min-height:100vh; font-family:'Segoe UI',Arial,sans-serif; color:#fff;
}
h1 { margin:10px 0 4px; font-size:26px; text-shadow:2px 2px 4px #000; }
#info { margin-bottom:8px; font-size:16px; }
canvas { border:6px solid #6b4226; border-radius:10px; box-shadow:0 10px 30px rgba(0,0,0,.6); }
#msg { margin-top:10px; font-size:18px; min-height:24px; color:#ffe082; }
</style>
</head>
<body>
<h1>🐶 Snoopy: Lovac na kosti</h1>
<div id="info">Kosti: <b id="score">0</b> / 19 &nbsp;•&nbsp; Kretanje: strelice ↑ ↓ ← → &nbsp;•&nbsp; R = nova igra</div>
<canvas id="game" width="800" height="560"></canvas>
<div id="msg"></div>
<script>
/* ===== SLIKE SE UČITAVAJU IZ FOLDERA (isti folder kao i ovaj fajl) =====
Potrebne slike:
snoopy_desno_1.png ... snoopy_desno_4.png (hodanje udesno)
snoopy_levo_1.png ... snoopy_levo_4.png (hodanje ulevo)
snoopy_gore_1.png, snoopy_gore_2.png (ledja)
snoopy_dole_1.png (spreda)
kost.png (kost)
======================================================================= */
function loadImg(src){ const i = new Image(); i.src = src; return i; }
const FR = {
right: [loadImg('snoopy_desno_1.png'), loadImg('snoopy_desno_2.png'),
loadImg('snoopy_desno_3.png'), loadImg('snoopy_desno_4.png')],
left: [loadImg('snoopy_levo_1.png'), loadImg('snoopy_levo_2.png'),
loadImg('snoopy_levo_3.png'), loadImg('snoopy_levo_4.png')],
up: [loadImg('snoopy_gore_1.png'), loadImg('snoopy_gore_2.png')],
down: [loadImg('snoopy_dole_1.png')]
};
const bonePic = loadImg('kost.png');
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const W = canvas.width, H = canvas.height;
const SCALE = 0.4; // <--- SAMO VELICINA SNUPIJA (1 = normalno, 0.5 = duplo manje, 2 = duplo vece). Kosti i cvece su uvek iste.
/* ===== TRAVA ===== */
const grassPattern = document.createElement('canvas');
grassPattern.width = grassPattern.height = 64;
const g = grassPattern.getContext('2d');
g.fillStyle = '#4a9b3c'; g.fillRect(0,0,64,64);
for (let i=0;i<90;i++){
const x=Math.random()*64, y=Math.random()*64;
g.strokeStyle = Math.random()<0.5 ? '#3e8a31' : '#5cb44c';
g.beginPath(); g.moveTo(x,y); g.lineTo(x+(Math.random()*2-1), y-3-Math.random()*3); g.stroke();
}
const grassFill = ctx.createPattern(grassPattern,'repeat');
const decor = [];
for (let i=0;i<14;i++)
decor.push({ x:30+Math.random()*(W-60), y:30+Math.random()*(H-60), t:Math.random()<0.5?'flower':'tuft' });
/* ===== SNOOPY ===== */
const snoopy = { x:W/2, y:H/2, dir:'down', speed:3.2, moving:false, frame:0, frameTimer:0 };
const keys = {};
window.addEventListener('keydown', e => {
keys[e.key]=true;
if(['ArrowUp','ArrowDown','ArrowLeft','ArrowRight'].includes(e.key)) e.preventDefault();
if((e.key==='r'||e.key==='R') && won) restart();
});
window.addEventListener('keyup', e => keys[e.key]=false);
/* ===== KOSTI ===== */
const bones = [];
function placeBones(){
bones.length = 0;
let tries = 0;
while (bones.length < 19 && tries < 5000){
tries++;
const x = 60+Math.random()*(W-120), y = 60+Math.random()*(H-120);
if (Math.hypot(x-snoopy.x, y-snoopy.y) < 80) continue;
if (bones.some(b => Math.hypot(b.x-x,b.y-y) < 60)) continue;
bones.push({ x, y, rot: Math.random()*Math.PI*2, taken:false, bob: Math.random()*Math.PI*2 });
}
}
let score=0, won=false, startTime=Date.now();
placeBones();
function restart(){
score=0; won=false;
snoopy.x=W/2; snoopy.y=H/2; snoopy.dir='down';
startTime=Date.now();
document.getElementById('score').textContent=0;
document.getElementById('msg').textContent='';
placeBones();
}
function update(){
if (won) return;
snoopy.moving=false;
if (keys['ArrowRight']){ snoopy.x+=snoopy.speed; snoopy.dir='right'; snoopy.moving=true; }
else if (keys['ArrowLeft']){ snoopy.x-=snoopy.speed; snoopy.dir='left'; snoopy.moving=true; }
if (keys['ArrowUp']){ snoopy.y-=snoopy.speed; snoopy.dir='up'; snoopy.moving=true; }
else if (keys['ArrowDown']){ snoopy.y+=snoopy.speed; snoopy.dir='down'; snoopy.moving=true; }
snoopy.x=Math.max(35*SCALE,Math.min(W-35*SCALE,snoopy.x));
snoopy.y=Math.max(35*SCALE,Math.min(H-35*SCALE,snoopy.y));
if (snoopy.moving){
snoopy.frameTimer++;
if (snoopy.frameTimer>5){ snoopy.frameTimer=0; snoopy.frame++; }
} else { snoopy.frame=0; snoopy.frameTimer=0; }
bones.forEach(b=>{
if (!b.taken && Math.hypot(b.x-snoopy.x,b.y-snoopy.y)<32){
b.taken=true; score++;
document.getElementById('score').textContent=score;
if (score===19){
won=true;
const secs=Math.floor((Date.now()-startTime)/1000);
document.getElementById('msg').textContent=
`🎉 Pobedio si! Snoopy je sakupio svih 19 kostiju za ${secs}s. Pritisni R za novu igru.`;
}
}
});
}
let tick=0;
function draw(){
tick++;
ctx.fillStyle=grassFill; ctx.fillRect(0,0,W,H);
ctx.strokeStyle='#8d6e63'; ctx.lineWidth=10; ctx.strokeRect(5,5,W-10,H-10);
decor.forEach(d=>{
if (d.t==='flower'){
ctx.fillStyle=['#ff6b81','#ffd93d','#ffffff'][Math.floor(d.x)%3];
for(let p=0;p<5;p++){ const a=p/5*Math.PI*2;
ctx.beginPath(); ctx.arc(d.x+Math.cos(a)*4, d.y+Math.sin(a)*4, 3, 0, Math.PI*2); ctx.fill(); }
ctx.fillStyle='#ff9800';
ctx.beginPath(); ctx.arc(d.x,d.y,2.5,0,Math.PI*2); ctx.fill();
} else {
ctx.strokeStyle='#3e8a31'; ctx.lineWidth=2;
for(let b=-1;b<=1;b++){ ctx.beginPath(); ctx.moveTo(d.x+b*4,d.y); ctx.lineTo(d.x+b*5,d.y-9); ctx.stroke(); }
}
});
bones.forEach(b=>{
if (b.taken) return;
const bobY=Math.sin(tick/20+b.bob)*2;
ctx.save();
ctx.translate(b.x, b.y+bobY);
ctx.rotate(b.rot + Math.sin(tick/40+b.bob)*0.08);
ctx.drawImage(bonePic, -30, -30*bonePic.height/Math.max(bonePic.width,1), 60, 60*bonePic.height/Math.max(bonePic.width,1));
ctx.restore();
});
ctx.fillStyle='rgba(0,0,0,0.18)';
ctx.beginPath(); ctx.ellipse(snoopy.x, snoopy.y+42*SCALE, 24*SCALE, 8*SCALE, 0, 0, Math.PI*2); ctx.fill();
const frames = FR[snoopy.dir];
const img = frames[snoopy.frame % frames.length];
if (img.complete && img.naturalWidth){
const bob = snoopy.moving ? Math.abs(Math.sin(tick/4))*3 : 0;
const w = img.naturalWidth*SCALE, h = img.naturalHeight*SCALE;
ctx.drawImage(img, snoopy.x - w/2, snoopy.y - h/2 - bob*SCALE, w, h);
}
}
function loop(){ update(); draw(); requestAnimationFrame(loop); }
loop();
</script>
</body>
</html>