Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var canvas;
- var context;
- var game;
- function initialize() {
- canvas = document.getElementById("canvas");
- context = canvas.getContext("2d");
- game = new theGame();
- document.addEventListener('keydown', function(evt) {
- var key = evt.which;
- game.input(key);
- }, false);
- mainLoop();
- }
- function theGame() {
- this.currentKey = null;
- this.lastKey = null;
- this.w = 20;
- this.score = 0;
- this.starting = { x: Math.floor((canvas.width / this.w) * 0.5) * this.w, y: Math.floor((canvas.height / this.w) * 0.5) * this.w }
- this.snake = new Rectangle(0, 0, this.w, this.w); this.snake.boxColor = "green";
- this.snake.x = this.starting.x
- this.snake.y = this.starting.y
- this.tail = new Array();
- this.tail.push(this.snake);
- this.re = 5;
- this.limit = this.re;
- this.spawnFood();
- }
- theGame.prototype.spawnFood = function() {
- var x = Math.floor(Math.random() * (canvas.width / this.w)) * this.w;
- var y = Math.floor(Math.random() * (canvas.height / this.w)) * this.w;
- this.food = new Rectangle(x, y, this.w, this.w); this.food.boxColor = "red";
- }
- theGame.prototype.reset = function() {
- this.currentKey = null;
- this.lastKey = null;
- this.snake.x = this.starting.x
- this.snake.y = this.starting.y
- this.tail = new Array();
- this.score = 0;
- this.tail.push(this.snake);
- }
- theGame.prototype.input = function(key) {
- switch(key) {
- case 38 : if (this.lastKey != "down") { this.currentKey = "up"; }
- break;
- case 40 : if (this.lastKey != "up") { this.currentKey = "down"; }
- break;
- case 37 : if (this.lastKey != "right") { this.currentKey = "left"; }
- break;
- case 39 : if (this.lastKey != "left") { this.currentKey = "right"; }
- break;
- }
- }
- theGame.prototype.update = function() {
- this.limit--;
- if (this.limit <= 0) {
- for (var i = this.tail.length - 1; i > 0; i--) {
- if (i != 0) { this.tail[i].x = this.tail[i - 1].x; this.tail[i].y = this.tail[i - 1].y; }
- }
- if (this.currentKey == "up") { this.lastKey = "up";
- this.snake.y -= this.w; if (this.snake.y < 0) { this.reset(); }
- }
- if (this.currentKey == "down") { this.lastKey = "down";
- this.snake.y += this.w; if (this.snake.y > canvas.height - this.w) { this.reset(); }
- }
- if (this.currentKey == "left") { this.lastKey = "left";
- this.snake.x -= this.w; if (this.snake.x < 0) { this.reset(); }
- }
- if (this.currentKey == "right") { this.lastKey = "right";
- this.snake.x += this.w; if (this.snake.x > canvas.width - this.w) { this.reset(); }
- }
- for (var i = 0; i < this.tail.length; i++) {
- if (i != 0 && this.snake.contains(this.tail[i].middle())) {
- this.reset();
- }
- }
- if (this.snake.contains(this.food.middle())) {
- rect = new Rectangle(-100, -100, this.w, this.w); rect.boxColor = "green";
- this.tail.push(rect);
- this.spawnFood();
- }
- this.limit = this.re;
- }
- }
- theGame.prototype.draw = function() {
- context.clearRect(0,0,canvas.width, canvas.height);
- for (var i = 0; i < this.tail.length; i++) {
- this.tail[i].draw();
- }
- this.food.draw();
- context.fillStyle = "white";
- context.font = "15px Arial";
- context.fillText(this.score, canvas.width * 0.5, 16);
- }
- function mainLoop() {
- game.update();
- game.draw();
- requestAnimationFrame(mainLoop);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement