0% found this document useful (0 votes)
14K views

Function Return: //setam Proprietatile Fiecarui Obiect

This document defines functions and objects for a game involving controlling a tide object to collect food while avoiding bombs. It includes: 1) Functions for generating random numbers, drawing to a canvas, and the game loop. 2) Objects like Tide, Food, Health and Score that inherit properties and have draw/move methods to control on screen. 3) A Hunger namespace that initializes the game, controls the title/game over screens, and spawns/updates food objects over increasing levels as the player collects points but loses health from bombs.

Uploaded by

Krom Vladimir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14K views

Function Return: //setam Proprietatile Fiecarui Obiect

This document defines functions and objects for a game involving controlling a tide object to collect food while avoiding bombs. It includes: 1) Functions for generating random numbers, drawing to a canvas, and the game loop. 2) Objects like Tide, Food, Health and Score that inherit properties and have draw/move methods to control on screen. 3) A Hunger namespace that initializes the game, controls the title/game over screens, and spawns/updates food objects over increasing levels as the player collects points but loses health from bombs.

Uploaded by

Krom Vladimir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

function rand(min, max)

{
return Math.random() * (max-min) + min;
}
var canvas;
var context;
var keyOn = [];
var Movable = function(data)
{
if (data === undefined)
return;
for (var i = 0; i < data.length; i++)
{
var setting = data[i];
//Setam proprietatile fiecarui obiect
this[setting[0]] = setting[1];
}
this.alive = true;
}
Movable.prototype = {
update: function()
{
if (this.alive)
{
this.move();
this.draw();
}
},
draw: function()
{
context.fillStyle = this.color;
context.fillRect(this.x, this.y, this.width, this.height);
}
};
var Countable = function()
{
this.x = 0;
this.y = 0;
this.speed = 2;
this.value = 0;
this.targetValue = 0;
}
Countable.prototype = {
update: function()
{
this.move();
this.draw();

},
change: function(amount)
{
this.targetValue += amount;
},
move: function()
{
//Schimbam pozitia obiectului doar daca este diferita de pozitia in
care se afla
if (this.value === this.targetValue)
return;

if (Math.abs(this.value - this.targetValue) < this.speed)


this.value = this.targetValue;
else if (this.targetValue > this.value)
this.value += this.speed;
else
this.value -= this.speed;

};
//------------------------------------------------------------------------------//Initializarea imaginilor
var tideSprites = new Array;
tideSprites[0] = new Image();
tideSprites[0].src = 'images/tideL.png';
tideSprites[1] = new Image();
tideSprites[1].src = 'images/tideR.png';
var foodSprites = new Array;
foodSprites[0] = new Image();
foodSprites[0].src = 'images/tango.png';
foodSprites[1] = new Image();
foodSprites[1].src = 'images/mana.png';
foodSprites[2] = new Image();
foodSprites[2].src = 'images/salve.png';
foodSprites[3] = new Image();
foodSprites[3].src = 'images/mine2.png';
foodSprites[4] = new Image();
foodSprites[4].src = 'images/inversemine.png';
//------------------------------------------------------------------------------var tideImage = tideSprites[1];
var foodImage = foodSprites[0];
var Tide = function(data)
{
Movable.call(this, data);
}
Tide.prototype = new Movable();

Tide.prototype.reset = function()
{
//Resetarea pozitiei
this.x = canvas.width / 2 - this.width / 2;
this.y = canvas.height - this.height;
}
Tide.prototype.draw = function()
{
//Afisare
context.drawImage(tideImage, this.x, this.y, this.width, this.height);
}
Tide.prototype.move = function()
{
//37 == Left
if (keyOn[37])
{
//Schimbam animatia si pozitia
tideImage = tideSprites[0];
this.x -= this.xSpeed;
}
//39 == Right
if (keyOn[39])
{
tideImage = tideSprites[1];
this.x += this.xSpeed;
}
//Daca pozitia pe axa Ox este < 0 inseamna ca obiectul iese din ecran
if (this.x < 0)
this.x = 0;
//Daca Daca pozitia pe axa Ox + lungimea obiectului este > lungimea
"ecranului":
//Inseamna ca obiectul iese din ecran
if (this.x + this.width > canvas.width)
this.x = canvas.width - this.width;
}
var Food = function(data)
{
Movable.call(this, data);
var foodImg;

this.initPosition();
this.initImage();

//Initializam fiecare obiect din clasa Movable


Food.prototype = new Movable();
Food.prototype.initPosition = function()
{
//Permite setarea pozitie Y o singura data

if (this.x !== undefined || this.y !== undefined)


return;
//Generam o pozitie aleatorie pe axa oX
this.x = Math.round(rand(0, canvas.width - this.width));
this.y = 0 - this.height;
}
Food.prototype.draw = function()
{
context.drawImage(this.foodImg, this.x, this.y, this.width, this.height);
}
Food.prototype.initImage = function()
{
var r = Math.round(rand(0, (foodSprites.length-1)));
if ( r == 3 || r == 4)
this.isBomb = 1;
this.foodImg = foodSprites[r];
}
Food.prototype.move = function()
{
//Miscarea in jos pe axa oY
this.y += this.ySpeed;
}
var Health = function()
{
Countable.call(this);
this.x = canvas.width - 52 - 10;
this.y = 10;
}
Health.prototype = new Countable();
Health.prototype.reset = function()
{
//Initializam viata cu care incepe jucatorul
this.value = 1;
this.targetValue = 100;
}
Health.prototype.draw = function()
{
//Containerul
context.fillStyle = '#fff';
context.strokeRect(this.x, this.y, 50 + 2, 5 + 2);
//Bara ce reprezinta viata ramasa
if (this.value >= 50)
context.fillStyle = '#00ff00';
else if (this.value >= 25)
context.fillStyle = '#fa6600';
else if (this.value >= 0)
context.fillStyle = '#ff0000';
context.fillRect(this.x + 1, this.y + 1, this.value * (50/100), 5);

//Textul
context.fillStyle = '#000';
context.textBaseline = 'top';
context.fillText('HP', this.x - 25, this.y -3);

var Score = function()


{
Countable.call(this);
this.x = canvas.width - 52 - 10;
this.y = 10 + 7 + 5;

}
Score.prototype = new Countable();

Score.prototype.reset = function()
{
this.value = this.targetValue = 0;
}
Score.prototype.draw = function()
{
context.textBaseline = 'top';
context.fillStyle = '#000';
context.fillText(this.value, this.x, this.y);
context.fillText('PTS', this.x - 25, this .y);
}
Hunger = new function()
{
var tideData = [
['width', 150],
['height', 120],
['xSpeed', 4]
];
var foodData = [
['width', 25],
['height', 25],
['ySpeed', 1],
['strength', 30],
['isBomb', 0]
];
var
var
var
var
var

foodPerLevel = 4;
foodSpawnSec = 0;
foodSpawned = 0;
foodOnScreen = 0;
food = [];

var level;
var levelUp;
var tide;
var health;

var score;
var info;
var infoScreenChange = true;
var startTime;
var frameTime;
this.run = function()
{
//Initializam fereastra pentru afisaj
canvas = document.getElementById('canvas');
info = document.getElementById('info');
context = canvas.getContext('2d');
var bgImg = new Image();
bgImg.src = 'images/bg.jpg';
context.drawImage(bgImg, 0, 0);
//Retinem orice tasta apasata
document.addEventListener('keydown', function(event)
{
//Marcam in vectorul keyOn pozitia tastei apasate cu true
keyOn[event.keyCode] = true;
}, false);
//Stergem din vectorul keyOn tastele ce au fost eliberate
document.addEventListener('keyup', function(event)
{
keyOn[event.keyCode] = false;
}, false);
//Instantierea obiectelor
tide = new Tide(tideData);
health = new Health();
score = new Score();
//Setam cate cadre pe secunda se vor afisa
interval = setInterval(titleScreen, 40/1000);
}
function resetGame()
{
tide.reset();
health.reset();
score.reset();
foodSpawnSec = 2.5;
foodSpawned = 0;
foodOnScreen = 0;
foods = [];
level = 1;
startTime = new Date().getTime();

}
function resetLevel()
{
tide.reset();
health.reset();

foodSpawned = 0;
foodOnScreen = 0;
foods = [];

function titleScreen()
{
if (infoScreenChange)
{
info.innerHTML = '<p class="text">Hunger In The Depths</p> <p
class="text">Press <b>spacebar</b> to start</p>';
}

infoScreenChange = false;

if (keyOn[32])
{
infoScreenChange = true;
info.style.display = 'none';
clearInterval(interval);
resetGame();
}

interval = setInterval(gameLoop, 1000/40)

}
function gameOverScreen()
{
frameTime = new Date().getTime();
if (infoScreenChange)
{
info.innerHTML = '<p>Game over!</p>';
info.style.display = 'block';
}

infoScreenChange = false;

if (frameTime > startTime + (3*1000))


{
infoScreenChange = true;
startTime = frameTime;

clearInterval(interval);
interval = setInterval(titleScreen, 30/1000);
}

function gameLoop()
{
frameTime = new Date().getTime();
if (health.value < 1)
{
tide.alive = false;
clearInterval(interval);
interval = setInterval(gameOverScreen, 30/1000);
}

return;

if (levelUp)
{
if (infoScreenChange)
{

info.innerHTML = '<p class="text">Level ' + (level-1) + '


cleared!</p><p class="text">Get ready for level ' + level + '!</p>';
info.style.display = 'block';
infoScreenChange = false;
}
if (frameTime >= startTime + (3*1000))
{
levelUp = false;
info.style.display = 'none';
infoScreenChange = true;
}
}

startTime = frameTime;

return;

context.clearRect(0, 0, canvas.width, canvas.height);


var bgImg = new Image();
bgImg.src = 'images/bg.jpg';
context.drawImage(bgImg, 0, 0);
tide.update();
health.update();
score.update();
updateObjects();

if (frameTime >= startTime + (foodSpawnSec*1000))


{
if (addObjects() === false)
{
levelUp = true;
level++;
foodSpawnSec *= 0.99;
foodData['ySpeed'] *= 1.01;
tideData['xSpeed'] *= 1.02;
resetLevel();
}
startTime = frameTime;
}

function updateObjects()
{
for (var i = 0; i < foods.length; i++)
{
//Copiem obiectul intr-o var locala
var food = foods[i];
food.update();
checkCollision(food);
}

//Verificam daca exista coliziuni intre obiecte si player


function checkCollision(food)
{
if (food === undefined || food.alive === false)
return;
//Daca obiectele nu au ajuns la nivelul jucatorului nu verificam
pentru ciocniri
if (food.y + food.height < tide.y)
return;
//Daca coordonatele obiectului sunt in intervalul coordonatelor
jucatorul inseamna ca avem o ciocnire
if (food.x >= tide.x &&
food.x + food.width <= tide.x + tide.width)
{
if (food.alive == true && food.isBomb == 0 )
{
food.alive = false;
foodOnScreen--;
score.change(food.strength);
}
else
{
food.alive = false;

foodOnScreen--;
score.change(-food.strength);
health.change(-food.strength);

}
else
{

if (food.y + food.height > tide.y + tide.height


> 0 && food.isBomb == 0)
{
health.change(- food.strength);

&& food.strength

food.strength = 0;
}
if (food.alive === true && food.y > canvas.height)
{
food.alive = false;
foodOnScreen--;
}
}

function addObjects()
{
if (foodSpawned != foodPerLevel * level)
{
foods[foods.length] = new Food(foodData);
foodSpawned++;
foodOnScreen++;

}
else
{

if (foodOnScreen == 0)
return false;
}
return true;
}

window.onload = function()
{
Hunger.run();
}

You might also like