Online Book Reader

Home Category

HTML5 Canvas [180]

By Root 6577 0
> extraShipsEarned) {

playerShips++

extraShipsEarned++;

}

}

function checkForEndOfLevel(){

if (rocks.length==0) {

switchGameState(GAME_STATE_NEW_LEVEL);

}

}

function gameStatePlayerDie(){

if (particles.length >0 || playerMissiles.length>0) {

fillBackground();

renderScoreBoard();

updateRocks();

updateSaucers();

updateParticles();

updateSaucerMissiles();

updatePlayerMissiles();

renderRocks();

renderSaucers();

renderParticles();

renderSaucerMissiles();

renderPlayerMissiles();

frameRateCounter.countFrames();

}else{

playerShips--;

if (playerShips<1) {

switchGameState(GAME_STATE_GAME_OVER);

}else{

//resetPlayer();

switchGameState(GAME_STATE_PLAYER_START);

}

}

}

function gameStateGameOver() {

//ConsoleLog.log("Game Over State");

if (gameOverStarted !=true){

fillBackground();

renderScoreBoard();

setTextStyle();

context.fillText ("Game Over!", 160, 70);

context.fillText ("Press Space To Play", 130, 140);

gameOverStarted = true;

}else{

//wait for space key click

if (keyPressList[32]==true){

ConsoleLog.log("space pressed");

switchGameState(GAME_STATE_TITLE);

gameOverStarted = false;

}

}

}

function fillBackground() {

// draw background and text

context.fillStyle = '#000000';

context.fillRect(xMin, yMin, xMax, yMax);

}

function setTextStyle() {

context.fillStyle = '#ffffff';

context.font = '15px _sans';

context.textBaseline = 'top';

}

function setTextStyleTitle() {

context.fillStyle = '#54ebeb';

context.font = '20px _sans';

context.textBaseline = 'top';

}

function setTextStyleCredits() {

context.fillStyle = '#ffffff';

context.font = '12px _sans';

context.textBaseline = 'top';

}

function renderScoreBoard() {

context.fillStyle = "#ffffff";

context.fillText('Score ' + score, 10, 20);

renderPlayerShip(200,16,270,.75)

context.fillText('X ' + playerShips, 220, 20);

context.fillText('FPS: ' + frameRateCounter.lastFrameCount, 300,20)

}

function checkKeys() {

//check keys

if (keyPressList[38]==true){

//thrust

var angleInRadians = player.rotation * Math.PI / 180;

player.facingX = Math.cos(angleInRadians);

player.facingY = Math.sin(angleInRadians);

var movingXNew = player.movingX+player.thrustAcceleration*player.facingX;

var movingYNew = player.movingY+player.thrustAcceleration*player.facingY;

var currentVelocity = Math.sqrt ((movingXNew*movingXNew) +

(movingXNew*movingXNew));

if (currentVelocity < player.maxVelocity) {

player.movingX = movingXNew;

player.movingY = movingYNew;

}

player.thrust = true;

}else{

player.thrust = false;

}

if (keyPressList[37]==true) {

//rotate counterclockwise

player.rotation -= player.rotationalVelocity;

if (player.rotation <0) {

player.rotation = 350

}

}

if (keyPressList[39]==true) {

//rotate clockwise

player.rotation += player.rotationalVelocity;

if (player.rotation >350) {

player.rotation = 10

}

}

if (keyPressList[32]==true) {

if (player.missileFrameCount>player.missileFrameDelay){

playSound(SOUND_SHOOT,.5);

firePlayerMissile();

player.missileFrameCount = 0;

}

}

}

function update() {

updatePlayer();

updatePlayerMissiles();

updateRocks();

updateSaucers();

updateSaucerMissiles();

updateParticles();

}

function render() {

fillBackground();

renderScoreBoard();

renderPlayerShip(player.x,player.y,player.rotation,1);

renderPlayerMissiles();

renderRocks();

renderSaucers();

renderSaucerMissiles();

renderParticles();

}

function updatePlayer() {

player.missileFrameCount++;

player.x += player.movingX*frameRateCounter.step;

player.y += player.movingY*frameRateCounter.step;

if (player.x > xMax) {

player.x =- player.width;

}else if (player.x<-player.width){

player.x = xMax;

}

if (player.y > yMax) {

player.y =- player.height;

}else if (player.y<-player.height){

player.y = yMax;

}

}

function updatePlayerMissiles() {

var tempPlayerMissile = {};

var playerMissileLength=playerMissiles.length-1;

//ConsoleLog.log("update playerMissileLength=" + playerMissileLength);

for (var playerMissileCtr=playerMissileLength;playerMissileCtr>=0;

Return Main Page Previous Page Next Page

®Online Book Reader