Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <body style = "margin-top: 0px;
- margin-bottom: 0px;
- margin-right: 0px;
- margin-left: 0px;">
- <canvas id = 'ctx' width = '640' height = '480'></canvas>
- </body>
- <script>
- // Set up canvas context and properties
- var canvas = document.getElementById('ctx');
- var ctx = canvas.getContext('2d');
- var room = {};
- room.width = 640;
- room.height = 480;
- setInterval(update, 1000/60);
- // Initialize Variables
- var hp;
- var score;
- hp = 100;
- score = 0;
- var traps = [];
- var state = 'menu';
- var shield = 0;
- var shield_text = false;
- var cooldown = 0;
- var shield_duration = 0;
- var menu = new Image();
- menu.src = 'menu.png';
- var shield_img = new Image();
- shield_img.src = 'shield.png';
- // Define colours and colour palette
- var colours = {};
- colours.umber = '#584D3D';
- colours.beaver = '#9F956C';
- colours.orange = '#FFD5C2';
- colours.dark_vanilla = '#D7BEA8';
- colours.snow = '#FCFAF9';
- colours.kiwi = '#A1E44D';
- colours.crimson = '#DC143C';
- colours.mint = '#77FF94';
- colours.black_olive = '#393E41';
- colours.blue_violet = '#8338EC';
- // Set up boundaries
- var boundary = {};
- boundary.left = {};
- boundary.right = {};
- boundary.up = {};
- boundary.down = {};
- boundary.left.x = 0;
- boundary.left.y = 48;
- boundary.left.width = 32;
- boundary.left.height = 448;
- boundary.right.x = 608;
- boundary.right.y = 48;
- boundary.right.width = 32;
- boundary.right.height = 448;
- boundary.up.x = 0;
- boundary.up.y = 48;
- boundary.up.width = 640;
- boundary.up.height = 32;
- boundary.down.x = 0;
- boundary.down.y = 448;
- boundary.down.width = 640;
- boundary.down.height = 32;
- // Define player
- var player = {};
- player.x = 304;
- player.y = 240;
- player.width = 32;
- player.height = 32;
- player.colour = '#FF6347';
- player.xspd = 0;
- player.yspd = 0;
- player.pressing_left = false;
- player.pressing_right = false;
- player.pressing_down = false;
- player.pressing_up = false;
- // Update function
- function update() {
- if(state == 'menu') {
- ctx.drawImage(menu, 0, 0);
- }
- if(state == 'game') {
- if(player.pressing_left || player.pressing_right || player.pressing_down || player.pressing_up)
- score++;
- cooldown--;
- shield_duration--;
- move_player();
- player.x += player.xspd;
- player.y += player.yspd;
- if(hp < 0) {
- hp = 0;
- state = 'game_over';
- }
- if(shield_duration <= 0) {
- player.colour = '#FF6347';
- } else {
- player.colour = colours.blue_violet;
- }
- boundary_collision_check();
- ctx.fillStyle = colours.umber;
- ctx.fillRect(0, 0, room.width, room.height);
- draw_boundary();
- draw_player();
- draw_score();
- draw_traps();
- }
- if(state == 'game_over') {
- ctx.fillStyle = colours.umber;
- ctx.fillRect(0, 0, room.width, room.height);
- ctx.fillStyle = colours.snow;
- ctx.font = '36px Arial';
- ctx.fillText('Game Over!\n Final Score: ' + score, 64, 180);
- ctx.fillText('Press ENTER to try again', 64, 240);
- }
- if(shield_text == true && state == 'game') {
- ctx.fillStyle = colours.black_olive;
- ctx.font = '28px Arial';
- ctx.fillText('Shield upgraded to level ' + shield, 4, 470);
- }
- }
- function draw_boundary() {
- ctx.fillStyle = colours.dark_vanilla;
- ctx.fillRect(boundary.left.x, boundary.left.y, boundary.left.width, boundary.left.height);
- ctx.fillRect(boundary.right.x, boundary.right.y, boundary.right.width, boundary.right.height);
- ctx.fillRect(boundary.up.x, boundary.up.y, boundary.up.width, boundary.up.height);
- ctx.fillRect(boundary.down.x, boundary.down.y, boundary.down.width, boundary.down.height);
- ctx.globalAlpha = (cooldown < 0 && shield > 0) ? 1:0.5;
- ctx.drawImage(shield_img, 340, 0);
- ctx.globalAlpha = 1;
- ctx.globalAlpha = 0.5
- ctx.fillStyle = 'white';
- ctx.fillRect(340, 0, 48, ((cooldown/720) * 48));
- ctx.globalAlpha = 1;
- }
- function draw_player() {
- ctx.fillStyle = player.colour;
- ctx.fillRect(player.x, player.y, player.width, player.height);
- }
- function draw_score() {
- ctx.fillStyle = colours.snow;
- ctx.font = '28px Arial';
- ctx.fillText('HP: ', 4, 28);
- ctx.fillText('Score: ' + score, 440, 28);
- ctx.fillStyle = colours.crimson;
- ctx.fillRect(64, 4, 200, 32);
- ctx.fillStyle = colours.kiwi;
- ctx.fillRect(64, 4, hp * 2, 32);
- }
- document.onkeydown = function(event) {
- if(event.keyCode == 37) { //left
- player.pressing_left = true;
- }
- if(event.keyCode == 38) { //up
- player.pressing_up = true;
- }
- if(event.keyCode == 39) { //right
- player.pressing_right = true;
- }
- if(event.keyCode == 40) { //down
- player.pressing_down = true;
- }
- }
- document.onkeyup = function(event) {
- if(event.keyCode == 37) { //left
- player.pressing_left = false;
- }
- if(event.keyCode == 38) { //up
- player.pressing_up = false;
- }
- if(event.keyCode == 39) { //right
- player.pressing_right = false;
- }
- if(event.keyCode == 40) { //down
- player.pressing_down = false;
- }
- if(event.keyCode == 83 && state == 'menu') {
- state = 'game';
- setInterval(generate_trap, Math.round(Math.random() * 10000));
- setInterval(shield_add, 20000);
- }
- if(event.keyCode == 68) {
- if(cooldown < 0 && shield > 0) {
- shield_duration = shield * 100;
- cooldown = 720;
- }
- }
- if(event.keyCode == 13 && state == 'game_over') {
- hp = 100;
- score = 0;
- traps = [];
- player.x = 304;
- player.y = 240;
- state = 'game';
- shield = 0;
- shield_text = 0;
- }
- }
- function boundary_collision_check() {
- if(player.y < 80) player.y = 80;
- if(player.x < 32) player.x = 32;
- if(player.x > 576) player.x = 576;
- if(player.y > 416) player.y = 416;
- }
- function move_player() {
- if(player.pressing_left) {
- player.x -= 5;
- }
- if(player.pressing_right) {
- player.x += 5;
- }
- if(player.pressing_up) {
- player.y -= 5;
- }
- if(player.pressing_down) {
- player.y += 5;
- }
- }
- function generate_trap() {
- if(state == 'game') {
- var trap = {};
- trap.x = Math.floor(Math.random() * 576) + 32;
- trap.y = Math.floor(Math.random() * 448) + 80;
- trap.width = 32;
- trap.height = 32;
- trap.xspd = Math.floor(Math.random() * 6) + 1;
- trap.yspd = Math.floor(Math.random() * 6) + 1;
- traps.push(trap);
- }
- }
- function draw_traps() {
- if(state == 'game') {
- ctx.fillStyle = colours.mint;
- for(i in traps) {
- traps[i].x += traps[i].xspd;
- traps[i].y += traps[i].yspd;
- if(traps[i].y < 80) {
- traps[i].y = 80;
- traps[i].yspd = -traps[i].yspd;
- }
- if(traps[i].x < 32) {
- traps[i].x = 32;
- traps[i].xspd = -traps[i].xspd;
- }
- if(traps[i].x > 576) {
- traps[i].x = 576;
- traps[i].xspd = -traps[i].xspd;
- }
- if(traps[i].y > 416) {
- traps[i].y = 416;
- traps[i].yspd = -traps[i].yspd;
- }
- if(player.x > traps[i].x && player.x < traps[i].x + traps[i].width && player.y > traps[i].y
- && player.y < traps[i].y + traps[i].height && shield_duration <= 0) {
- hp--;
- }
- ctx.fillRect(traps[i].x, traps[i].y, traps[i].width, traps[i].height);
- }
- }
- }
- function shield_add() {
- if(state == 'game') {
- shield += 1;
- shield_text = true;
- }
- }
- </script>
- <script>
- window.addEventListener("keydown", function(e) {
- if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
- e.preventDefault();
- }
- }, false);
- </script>
Add Comment
Please, Sign In to add comment