Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------------------Variables
- var gamePieceRed;
- var gamePieceBorder;
- var gameObstacle;
- //-----------------------------------------
- //-----------------------------------------Main game function
- function startGame()
- {
- gamePieceRed = new component(22, 22, "rgb(255, 132, 156)", 10, 120);
- gamePieceBorder = new component(24, 24, "black", 9, 119);
- obstacle = new component(10, 200, "rgb(64, 0 ,12)", 300, 120)
- gameArea.start();
- }
- //-----------------------------------------
- //-----------------------------------------Creating game area and applying controls
- var gameArea =
- {
- canvas : document.createElement("canvas"), start : function()
- {
- this.canvas.width = 510;
- this.canvas.height = 280;
- this.context = this.canvas.getContext("2d");
- document.body.insertBefore(this.canvas, document.body.childNodes[0]);
- this.interval = setInterval(gameUpdate, 20);
- window.addEventListener("keydown", function (e)
- {
- gameArea.keys = (gameArea.keys || []);
- gameArea.keys[e.keyCode] = true;
- }, true)
- window.addEventListener("keyup", function (e)
- {
- gameArea.keys[e.keyCode] = false;
- }, true)
- },
- clear : function()
- {
- this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
- },
- stop : function()
- {
- clearInterval(this.interval);
- },
- keyboard: function() {
- if (this.keys) {
- if (this.keys[37]) {gamePieceBorder.speedX = gamePieceRed.speedX = -2;}
- else if (this.keys[39]) {gamePieceBorder.speedX = gamePieceRed.speedX = 2;}
- else {gamePieceBorder.speedX = gamePieceRed.speedX = 0;}
- if (this.keys[38]) {gamePieceBorder.speedY = gamePieceRed.speedY = -2;}
- else if (this.keys[40]) {gamePieceBorder.speedY = gamePieceRed.speedY = 2;}
- else {gamePieceBorder.speedY = gamePieceRed.speedY = 0;}
- }
- }
- }
- //-----------------------------------------
- //-----------------------------------------Game component
- function component(width, height, color, x, y)
- {
- this.width = width;
- this.height = height;
- this.x = x;
- this.y = y;
- this.speedX = 0;
- this.speedY = 0;
- this.update = function()
- {
- ctx = gameArea.context;
- ctx.fillStyle = color;
- ctx.fillRect(this.x, this.y, this.width, this.height)
- }
- this.move = function()
- {
- this.x += this.speedX;
- this.y += this.speedY;
- }
- this.crashGame = function(obj)
- {
- var left = this.x;
- var right = this.x + (this.width);
- var top = this.y;
- var bottom = this.y + (this.height);
- var otherLeft = obj.x;
- var otherRight = obj.x + (obj.width);
- var otherTop = obj.y;
- var otherBottom = obj.y + (obj.height);
- var crash = true;
- if (bottom < otherTop || top > otherBottom || right < otherLeft || left > otherRight)
- {
- crash = false;
- }
- return crash;
- }
- }
- //-----------------------------------------
- //-----------------------------------------Game area updater
- function gameUpdate()
- {
- if(gamePieceBorder.crashGame(obstacle) || gamePieceRed.crashGame(obstacle))
- {
- gameArea.stop();
- }
- else
- {
- gameArea.clear();
- obstacle.update();
- gameArea.keyboard();
- gamePieceBorder.move();
- gamePieceBorder.update();
- gamePieceRed.move();
- gamePieceRed.update();
- }
- }
- //-----------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement