Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <title>Untitled Document</title>
- </head>
- <body>
- <canvas id="canvas" width="600" height="600"></canvas>
- <script src="https://code.jquery.com/jquery-2.1.0.js"></script>
- <script>
- var canvas = document.getElementById("canvas");
- var ctx = canvas.getContext("2d");
- var canvasWidth = canvas.width;
- var canvasHeight = canvas.height;
- Math.seed = 87965089;
- // in order to work 'Math.seed' must NOT be undefined,
- // so in any case, you HAVE to provide a Math.seed
- Math.seededRandom = function() {
- max = 18;
- min = 1;
- Math.seed = (Math.seed * 9301 + 49297) % 233280;
- var rnd = Math.seed / 233280;
- return Math.round(min + rnd * (max - min));
- };
- var TerrainGenerator = function() {
- this.y = 560;
- this.x = 700;
- this.height = 40;
- this.randomDigit = Math.seededRandom();
- if (this.randomDigit > 0) {
- this.biome = 1;
- }
- };
- TerrainGenerator.prototype.Create = function() {
- if (this.x <= 700) {
- //select biome
- if (this.biome == 1) {
- //select a landscape to build
- this.randomDigit = Math.seededRandom();
- if (this.randomDigit > 0 && this.randomDigit < 19) {
- //select hill height (between 3 and 4 atm)
- this.randomDigit = Math.seededRandom();
- if (this.randomDigit == 1 || this.randomDigit == 2 || this.randomDigit < 19) {
- var hillHeight = 4;
- console.log()
- //create hill with a height of 4
- this.randomDigit = Math.seededRandom();
- var firstHillBlockWidth = this.randomDigit * 40 * 2;
- var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
- var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
- this.randomDigit = Math.seededRandom();
- if (this.randomDigit > 9) {
- terrainTiles.push(new MissileShooter(this.x + hillIndent - 5,this.y - this.height + 10));
- }
- var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
- this.randomDigit = Math.seededRandom();
- if (this.randomDigit > 9) {
- terrainTiles.push(new MissileShooter(this.x + (hillIndent * 2) - 5,this.y - this.height * 2 + 10));
- }
- var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
- this.randomDigit = Math.seededRandom();
- if (this.randomDigit > 9) {
- terrainTiles.push(new MissileShooter(this.x + (hillIndent * 3) - 5,this.y - this.height * 3 + 10));
- }
- terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
- this.x += firstHillBlockWidth;
- } else { if (this.randomDigit == 3 || this.randomDigit == 4) {
- var hillHeight = 7;
- this.randomDigit = Math.seededRandom();
- var firstHillBlockWidth = this.randomDigit * 40 * 2;
- var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
- var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
- var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
- var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
- var fithHillBlockWidth = fourthHillBlockWidth - hillIndent * 2;
- var sixthHillBlockWidth = fithHillBlockWidth - hillIndent * 2;
- var seventhHillBlockWidth = sixthHillBlockWidth - hillIndent * 2;
- terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 4,this.y - this.height * 4,fithHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 5,this.y - this.height * 5,sixthHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 6,this.y - this.height * 6,seventhHillBlockWidth,40));
- this.x += firstHillBlockWidth;
- } else { if (this.randomDigit == 5 || this.randomDigit == 6) {
- //create a hill with a height of 3
- var hillHeight = 3;
- this.randomDigit = Math.seededRandom();
- var firstHillBlockWidth = this.randomDigit * 40;
- var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
- var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
- var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
- terrainTiles.push(new Terrain(this.x, this.y, firstHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent, this.y - this.height, secondHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
- console.log(firstHillBlockWidth);
- this.x += firstHillBlockWidth;
- } else { if (this.randomDigit == 7 || this.randomDigit == 8) {
- var hillHeight = 6;
- this.randomDigit = Math.seededRandom();
- var firstHillBlockWidth = this.randomDigit * 40 * 2;
- var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
- var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
- var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
- var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
- var fithHillBlockWidth = fourthHillBlockWidth - hillIndent * 2;
- var sixthHillBlockWidth = fithHillBlockWidth - hillIndent * 2;
- terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 4,this.y - this.height * 4,fithHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 5,this.y - this.height * 5,sixthHillBlockWidth,40));
- this.x += firstHillBlockWidth;
- } else { if (this.randomDigit == 9 || this.randomDigit == 10) {
- var hillHeight = 5;
- this.randomDigit = Math.seededRandom();
- var firstHillBlockWidth = this.randomDigit * 40 * 2;
- var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
- var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
- var thirdHillBlockWidth = secondHillBlockWidth - hillIndent * 2;
- var fourthHillBlockWidth = thirdHillBlockWidth - hillIndent * 2;
- var fithHillBlockWidth = fourthHillBlockWidth - hillIndent * 2;
- var sixthHillBlockWidth = fithHillBlockWidth - hillIndent * 2;
- terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 2,this.y - this.height * 2,thirdHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 3,this.y - this.height * 3,fourthHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent * 4,this.y - this.height * 4,fithHillBlockWidth,40));
- this.x += firstHillBlockWidth;
- } else { if (this.randomDigit == 11 || this.randomDigit == 12) {
- var hillHeight = 2;
- this.randomDigit = Math.seededRandom();
- var firstHillBlockWidth = this.randomDigit * 40 * 2;
- var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
- var secondHillBlockWidth = firstHillBlockWidth - hillIndent * 2;
- terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
- terrainTiles.push(new Terrain(this.x + hillIndent,this.y - this.height,secondHillBlockWidth,40));
- } else {
- var hillHeight = 1;
- this.randomDigit = Math.seededRandom();
- var firstHillBlockWidth = this.randomDigit * 40 * 4;
- var hillIndent = Math.floor(firstHillBlockWidth / hillHeight / 2);
- terrainTiles.push(new Terrain(this.x,this.y,firstHillBlockWidth,40));
- }
- }
- }
- }
- }
- }
- } else {
- //create a plains
- this.randomDigit = Math.seededRandom();
- //make size of plains
- var plainsSize = Math.floor((this.randomDigit / 2 )* 40);
- //create the base of plains
- terrainTiles.push(new Terrain(this.x,this.y,plainsSize,40));
- this.randomDigit = Math.seededRandom();
- //decide where the first ' bump will be'
- var firstIndent = Math.floor((this.randomDigit / 4) * 40);
- this.randomDigit = Math.seededRandom();
- //decide how wide the bump is and create it
- var blockWidth = Math.floor((this.randomDigit / 4) * 40);
- terrainTiles.push(new Terrain(this.x + firstIndent,this.y - this.height,blockWidth,40));
- this.randomDigit = Math.seededRandom();
- console.log('blockWidth = ' + blockWidth);
- //decide where the second bump will be
- var secondIndent = this.x + firstIndent + blockWidth + Math.floor(this.randomDigit / 4) * 40;
- this.randomDigit = Math.seededRandom();
- //decide how wide the second bump will be and draw it
- var blockWidth = Math.floor((this.randomDigit / 4) * 40);
- terrainTiles.push(new Terrain(this.x + secondIndent,this.y - this.height,blockWidth,40));
- console.log('blockWidth = ' + blockWidth);
- this.x += plainsSize;
- }
- }
- }
- };
- var Missiles = function (x,y) {
- this.x = x;
- this.y = y;
- this.width = 30;
- this.height = 20;
- this.speed = 5;
- this.deathSequence = false
- };
- Missiles.prototype.Draw = function () {
- if (this.x >= -200) {
- ctx.beginPath();
- ctx.fillStyle = "Black";
- ctx.rect(this.x,this.y,this.width,this.height);
- ctx.stroke();
- ctx.fill();
- } else {
- missileCount.splice(this,1);
- console.log('removed a missile')
- }
- };
- Missiles.prototype.Move = function () {
- if (this.deathSequence) {
- this.y += this.speed;
- } else {
- this.x -= this.speed;
- }
- // if (direction == 'left') {
- // this.x -= this.speed;
- // }
- };
- Missiles.prototype.Collisions = function () {
- for (var i = 0; i < terrainTiles.length; i++) {
- if (this.x < terrainTiles[i].x + terrainTiles[i].width && this.x > terrainTiles[i].x + terrainTiles[i].width / 2) {
- if (this.y < terrainTiles[i].y + terrainTiles[i].height && this.y + this.height > terrainTiles[i].y) {
- this.deathSequence = true;
- }
- }
- }
- };
- var MissileShooter = function (x,y) {
- this.x = x;
- this.y = y;
- this.width = 20;
- this.height = 20;
- this.direction = undefined;
- this.type = 'spawner';
- };
- MissileShooter.prototype.Draw = function () {
- ctx.beginPath();
- ctx.fillStyle = "Grey";
- ctx.rect(this.x,this.y,this.width,this.height);
- ctx.stroke();
- ctx.fill();
- };
- MissileShooter.prototype.Spawn = function () {
- if(~~(Math.random() * 50) == 3) {
- missileCount.push(new Missiles(this.x,this.y))
- }
- };
- var Terrain = function (x,y,width,height) {
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- this.type = 'static Terrain';
- this.imageSize = 40;
- this.img = new Image();
- this.img.src = "Assets/textures/grass.png";
- };
- Terrain.prototype.Draw = function () {
- if (this.x < - 1500) {
- terrainTiles.shift();
- }
- ctx.drawImage(this.img,0,0,this.width,this.height,this.x,this.y,this.width,this.height);
- };
- var Player = function (X,Y,Width,Height) {
- this.x = X;
- this.y = Y;
- this.width = Width;
- this.height = Height;
- this.speed = 8;
- this.upMove = false;
- this.downMove = false;
- this.leftMove = false;
- this.rightMove = false;
- this.jumpSpeed = 0;
- this.jumplock = false;
- };
- Player.prototype.Draw = function () {
- ctx.beginPath();
- ctx.fillStyle = "Red";
- ctx.rect(this.x,this.y,this.width,this.height);
- ctx.stroke();
- ctx.fill();
- };
- Player.prototype.Move = function (direction,type) {
- if (type === 'keydown') {
- if (direction === 'w') {
- this.upMove = true;
- } else {
- if (direction === 's') {
- this.downMove = true;
- } else {
- if (direction === 'a') {
- this.leftMove = true;
- } else {
- if (direction === 'd') {
- this.rightMove = true;
- }
- }
- }
- }
- }
- if (type === 'keyup') {
- if (direction === 'w') {
- this.upMove = false;
- } else {
- if (direction === 's') {
- this.downMove = false;
- } else {
- if (direction === 'a') {
- this.leftMove = false;
- } else {
- if (direction === 'd') {
- this.rightMove = false;
- }
- }
- }
- }
- }
- };
- Player.prototype.MoveUpdater = function () {
- if (this.upMove == true) {
- if (this.jumplock == false) {
- this.jumpSpeed = 23;
- //this.jumplock = true;
- }
- }
- this.y -= this.jumpSpeed;
- if (this.jumpSpeed > 0) {
- this.jumpSpeed -= 1;
- }
- if (this.downMove == true) {
- this.y += this.speed;
- }
- if (this.leftMove == true) {
- for (var i = 0; i < terrainTiles.length; i++) {
- terrainTiles[i].x += this.speed;
- }
- for (var i = 0; i < missileCount.length; i++) {
- missileCount[i].x += this.speed;
- }
- terrainEngine.x += this.speed;
- }
- if (this.rightMove == true) {
- for (var i = 0; i < terrainTiles.length; i++) {
- terrainTiles[i].x -= this.speed;
- }
- for (var i = 0; i < missileCount.length; i++) {
- missileCount[i].x -= this.speed;
- }
- terrainEngine.x -= this.speed;
- }
- };
- Player.prototype.Collisions = function() {
- for (var i = 0; i < terrainTiles.length; i++) {
- //collisions with top face of terrain
- if (this.y + this.height > terrainTiles[i].y && this.y + this.height < terrainTiles[i].y + terrainTiles[i].height / 2) {
- if (this.x < terrainTiles[i].x + terrainTiles[i].width && this.x + this.width > terrainTiles[i].x) {
- this.y = terrainTiles[i].y - this.height;
- this.jumplock = false;
- }
- }
- //collisiosn with left face of terrain
- if (this.x + this.width > terrainTiles[i].x && this.x + this.width < terrainTiles[i].x + terrainTiles[i].width / 2) {
- if (this.y < terrainTiles[i].y + terrainTiles[i].height && this.y + this.height > terrainTiles[i].y) {
- var overstep = this.x + this.width - terrainTiles[i].x;
- for (var value = 0; value < terrainTiles.length; value++) {
- terrainTiles[value].x += overstep;
- }
- for (var value = 0; value < missileCount.length; value++) {
- missileCount[value].x += overstep;
- }
- }
- }
- //collisions with right face of terrain
- if (this.x < terrainTiles[i].x + terrainTiles[i].width && this.x > terrainTiles[i].x + terrainTiles[i].width / 2) {
- if (this.y < terrainTiles[i].y + terrainTiles[i].height && this.y + this.height > terrainTiles[i].y) {
- var overstep = terrainTiles[i].x + terrainTiles[i].width - this.x;
- for (var value = 0; value < terrainTiles.length; value++) {
- terrainTiles[value].x -= overstep;
- }
- for (var value = 0; value < missileCount.length; value++) {
- missileCount[value].x -= overstep;
- }
- }
- }
- }
- for (var i = 0; i < missileCount.length; i++) {
- //top face collisions
- if (this.y + this.height > missileCount[i].y && this.y + this.height < missileCount[i].y + missileCount[i].height / 2) {
- if (this.x < missileCount[i].x + missileCount[i].width && this.x + this.width > missileCount[i].x) {
- this.jumpSpeed = 19;
- this.jumplock = true;
- missileCount[i].deathSequence = true;
- }
- }
- //left face collisions
- if (this.x + this.width > missileCount[i].x && this.x + this.width < missileCount[i].x + missileCount[i].width / 2) {
- if (this.y < missileCount[i].y + missileCount[i].height && this.y + this.height > missileCount[i].y) {
- console.log('GAME OVER!!!!!!');
- }
- }
- //right face collisions
- if (this.x < missileCount[i].x + missileCount[i].width && this.x > missileCount[i].x + missileCount[i].width / 2) {
- if (this.y < missileCount[i].y + missileCount[i].height && this.y + this.height > missileCount[i].y) {
- console.log('GAMEOVER!!!!!!!');
- }
- }
- }
- };
- terrainTiles = [];
- missileCount = [];
- players = [];
- players.push(new Player(200,200,40,40));
- terrainTiles.push(new Terrain(0,560,700,40));
- terrainTiles.push(new Terrain(100,520,700,40));
- terrainTiles.push(new Terrain(310,480,200,40));
- var keyActions = {
- 87: "w",
- 65: "a",
- 83: "s",
- 68: "d"
- };
- $("body").keydown(function (event) {
- var direction = keyActions[event.keyCode];
- var type = 'keydown';
- for (var i = 0; i < players.length; i++) {
- players[i].Move(direction,type);
- }
- });
- $("body").keyup(function (event) {
- var direction = keyActions[event.keyCode];
- var type = 'keyup';
- for (var i = 0; i < players.length; i++) {
- players[i].Move(direction,type);
- }
- });
- terrainEngine = new TerrainGenerator();
- var gravity = 8;
- setInterval(function() {
- ctx.clearRect(0,0,canvasWidth,canvasHeight);
- ctx.strokeRect(0,0,canvasWidth,canvasHeight);
- for (var i = 0; i < players.length; i++) {
- players[i].MoveUpdater();
- players[i].y += gravity;
- players[i].Collisions();
- players[i].Draw();
- }
- terrainEngine.Create();
- for (var i = 0; i < missileCount.length; i++) {
- missileCount[i].Move();
- missileCount[i].Draw();
- }
- for (var i = 0; i < terrainTiles.length; i++) {
- terrainTiles[i].Draw();
- if (terrainTiles[i].type == 'spawner') {
- terrainTiles[i].Spawn();
- }
- }
- }, 30);
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment