Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var canvas;
- var ctx;
- var width = 600;
- var height = 600;
- var starfield;
- var starX = 0;
- var starY = 0;
- var starY2 = -600;
- var score = 0;
- var highscore = 10;
- var alive = true;
- var lives = 3;
- var gameStarted = false;
- livesimage = new Image()
- livesimage.src = 'hart.png';
- var levens3 = true;
- var levens2 = false;
- var levens1 = false;
- function livedraw()
- {
- if(lives == 3)
- {
- ctx.drawImage(livesimage,10,10,50,38);
- ctx.drawImage(livesimage,50,10,50,38);
- ctx.drawImage(livesimage,90,10,50,38);
- }
- if(lives == 2)
- {
- ctx.drawImage(livesimage,10,10,50,38);
- ctx.drawImage(livesimage,50,10,50,38);
- }
- if(lives == 1)
- {
- ctx.drawImage(livesimage,10,10,50,38);
- }
- }
- function clearCanvas() {
- ctx.clearRect(0, 0, width, height);
- }
- function init() {
- canvas = document.getElementById('canvas');
- ctx = canvas.getContext('2d');
- ship = new Image();
- ship.src = 'ship.png';
- starfield = new Image();
- starfield.src = 'starfield.jpg';
- document.addEventListener('keydown', keyDown, false);
- document.addEventListener('keyup', keyUp, false);
- canvas.addEventListener('click', gameStart, false);
- gameLoop();
- }
- function gameStart() {
- gameStarted = true;
- canvas.removeEventListener('click', gameStart, false);
- }
- function gameLoop() {
- clearCanvas();
- drawStarfield();
- if (alive && gameStarted && lives > 0) {
- hitTest();
- moveEnemies();
- moveLaser();
- drawEnemies();
- drawShip();
- drawLaser();
- shipCollision();
- livedraw();
- update();
- DeleteItems();
- console.log(highscore);
- }
- scoreTotal();
- setTimeout(gameLoop, 1000 / 30);
- }
- function scoreTotal() {
- ctx.font = 'bold 18px VT323';
- ctx.fillStyle = '#fff';
- ctx.fillText('Score: ', 10, 55);
- ctx.fillText(score, 70, 55);
- ctx.fillText('Lives: ', 10, 30);
- ctx.fillText(lives, 68, 30);
- if (!alive) {
- ctx.fillText('Game Over!', 245, (height / 2));
- ctx.fillRect((width / 2) - 65, (height / 2) + 10, 100, 40);
- ctx.fillStyle = '#000';
- ctx.fillText('Opnieuw?', 252, (height / 2) + 35);
- canvas.addEventListener('click', continueButton, false);
- }
- if (!gameStarted) {
- ctx.font = 'bold 50px VT323';
- ctx.fillText('Sander Gouman top down', (width / 2) - 210, (height / 2));
- ctx.font = 'bold 20px VT323';
- ctx.fillText('Klik om te spelen', (width / 2) - 56, (height / 2) + 30);
- ctx.fillText('Beweeg met de pijltjes', (width / 2) - 100, (height / 2) + 60);
- ctx.fillText('Gebruik de spatiebalk om te schieten', (width / 2) - 100, (height / 2) + 90);
- }
- }
- function continueButton(e) {
- var cursorPos = getCursorPos(e);
- if (cursorPos.x > (width / 2) - 53 && cursorPos.x < (width / 2) + 47 && cursorPos.y > (height / 2) + 10 && cursorPos.y < (height / 2) + 50) {
- alive = true;
- lives = 3;
- reset();
- canvas.removeEventListener('click', continueButton, false);
- }
- }
- function getCursorPos(e) {
- var x;
- var y;
- if (e.pageX || e.pageY) {
- x = e.pageX;
- y = e.pageY;
- } else {
- x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
- y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
- }
- x -= canvas.offsetLeft;
- y -= canvas.offsetTop;
- var cursorPos = new cursorPosition(x, y);
- return cursorPos;
- }
- function cursorPosition(x, y) {
- this.x = x;
- this.y = y;
- }
- function checkLives() {
- lives -= 1;
- if (lives > 0) {
- reset();
- } else if (lives == 0) {
- alive = false;
- if (score > parseInt(localStorage.getItem("highscore"))) {
- localStorage.setItem("highscore", score);
- }
- }
- }
- function reset() {
- var enemy_reset_x = 50;
- ship_x = (width / 2) - 25;
- ship_y = height - 75;
- ship_w = 50;
- ship_h = 57;
- for (var i = 0; i < enemies.length; i++) {
- enemies[i][0] = enemy_reset_x;
- enemies[i][1] = -45;
- enemy_reset_x = enemy_reset_x + enemy_w + 60;
- }
- }
- function drawStarfield() {
- ctx.drawImage(starfield, starX, starY);
- ctx.drawImage(starfield, starX, starY2);
- if (starY > 600) {
- starY = -599;
- }
- if (starY2 > 600) {
- starY2 = -599;
- }
- starY += 1;
- starY2 += 1;
- }
- window.onload = init;
Advertisement
Add Comment
Please, Sign In to add comment