From 593c1ff51ba290891ff768393a3fbda51192f743 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 16 Jul 2018 11:18:23 +0100 Subject: [PATCH] Dino Game --- JavaScript/dino-game/cacti.js | 39 ++++++++++++++++ JavaScript/dino-game/clouds.js | 0 JavaScript/dino-game/colision.js | 5 ++ JavaScript/dino-game/enemys.js | 38 +++++++++++++++ JavaScript/dino-game/flyer.js | 0 JavaScript/dino-game/game.js | 68 +++++++++++++++++++++++++++ JavaScript/dino-game/ground.js | 0 JavaScript/dino-game/index.html | 24 ++++++++++ JavaScript/dino-game/package.json | 11 +++++ JavaScript/dino-game/player.js | 71 +++++++++++++++++++++++++++++ JavaScript/dino-game/style.css | 10 ++++ JavaScript/dino-game/world.js | 22 +++++++++ JavaScript/search-tree/index.js | 47 +++++++++++++++++++ JavaScript/search-tree/package.json | 11 +++++ 14 files changed, 346 insertions(+) create mode 100644 JavaScript/dino-game/cacti.js create mode 100644 JavaScript/dino-game/clouds.js create mode 100644 JavaScript/dino-game/colision.js create mode 100644 JavaScript/dino-game/enemys.js create mode 100644 JavaScript/dino-game/flyer.js create mode 100644 JavaScript/dino-game/game.js create mode 100644 JavaScript/dino-game/ground.js create mode 100644 JavaScript/dino-game/index.html create mode 100644 JavaScript/dino-game/package.json create mode 100644 JavaScript/dino-game/player.js create mode 100644 JavaScript/dino-game/style.css create mode 100644 JavaScript/dino-game/world.js create mode 100644 JavaScript/search-tree/index.js create mode 100644 JavaScript/search-tree/package.json diff --git a/JavaScript/dino-game/cacti.js b/JavaScript/dino-game/cacti.js new file mode 100644 index 0000000..96346e6 --- /dev/null +++ b/JavaScript/dino-game/cacti.js @@ -0,0 +1,39 @@ +class Cacti { + constructor() { + this.y = world.ground + 10; + this.x = 900; + this.width = 15; + this.height = 30; + } + + update() { + this.x -= player.velocityX; + } + + draw() { + ctx.fillStyle="#FFFFFF"; + ctx.fillRect(this.x - 2, this.y, this.width + 4, this.height); + ctx.fillStyle="#000000"; + ctx.fillRect(this.x, this.y, this.width, this.height); + } +} + +class ThiccCacti { + constructor() { + this.y = world.ground ; + this.x = 900; + this.width = 30; + this.height = 40; + } + + update() { + this.x -= player.velocityX; + } + + draw() { + ctx.fillStyle="#FFFFFF"; + ctx.fillRect(this.x - 2, this.y, this.width + 4, this.height); + ctx.fillStyle="#000000"; + ctx.fillRect(this.x, this.y, this.width, this.height); + } +} diff --git a/JavaScript/dino-game/clouds.js b/JavaScript/dino-game/clouds.js new file mode 100644 index 0000000..e69de29 diff --git a/JavaScript/dino-game/colision.js b/JavaScript/dino-game/colision.js new file mode 100644 index 0000000..74ff76a --- /dev/null +++ b/JavaScript/dino-game/colision.js @@ -0,0 +1,5 @@ +function colide() { + for (let i = 0; i < cacti.length; i++) { + + } +} diff --git a/JavaScript/dino-game/enemys.js b/JavaScript/dino-game/enemys.js new file mode 100644 index 0000000..9ee2a8b --- /dev/null +++ b/JavaScript/dino-game/enemys.js @@ -0,0 +1,38 @@ +function spawnEnemys() { + let r = Math.floor(Math.random() * 100); + + if (2 > r) { + for (let i = 0; i < cacti.length; i++) { + if (cacti[i].x > 700) { + console.log('Spawn failed') + return; + } + } + + if (r == 1) { + cacti.push(new Cacti()); + } else { + cacti.push(new ThiccCacti); + } + } +} + +function updateEnemys() { + for (let i = 0; i < cacti.length; i++) { + cacti[i].update(); + } +} + +function despawnEnemys() { + for (let i = 0; i < cacti.length; i++) { + if (cacti[i].x < -cacti[i].width) { + cacti.splice(1, i) + } + } +} + +function drawEnemys() { + for (let i = 0; i < cacti.length; i++) { + cacti[i].draw(); + } +} diff --git a/JavaScript/dino-game/flyer.js b/JavaScript/dino-game/flyer.js new file mode 100644 index 0000000..e69de29 diff --git a/JavaScript/dino-game/game.js b/JavaScript/dino-game/game.js new file mode 100644 index 0000000..94baca5 --- /dev/null +++ b/JavaScript/dino-game/game.js @@ -0,0 +1,68 @@ + /* + Author(s): Ben (plane000)#8618 + Created: 11/07/2018 + Last Updated: 16/07/2018 + Last Updated by: Ben (plane000)#8618 +*/ + +let c = document.getElementById("canv"); +let ctx = c.getContext("2d"); + +let player; +let world; +let cacti; + +function initialize() { + world = new World(); + world.reset(); + player = new Player(); + player.reset(); + cacti = []; +} + +function gameLoop() { + cls(); + + world.update(); + + spawnEnemys(); + despawnEnemys(); + updateEnemys(); + + player.update(); + + colide(); + + drawEnemys(); + + world.draw(); + player.draw(); + + player.velocityX += 0.001; + player.score++; +} + +function cls() { + ctx.fillStyle="#FFFFFF" // white + ctx.fillRect(0, 0, 800, 400); +} + +window.addEventListener('keydown', (event) => { + if (event.code == 'Space' || event.code == 'ArrowUp' || event.code == 'KeyW') { + console.log('jump'); + player.jump(); + } + if (event.code == 'ShiftLeft' || event.code == 'ArrowDown' || event.code == 'KeyS') { + console.log('duck'); + player.duck(); + } +}); + +window.addEventListener('keyup', (event) => { + if (event.code == 'ShiftLeft' || event.code == 'ArrowDown' || event.code == 'KeyS') { + player.stand(); + } +}); + +initialize(); +setInterval(gameLoop, 1000 / 60) diff --git a/JavaScript/dino-game/ground.js b/JavaScript/dino-game/ground.js new file mode 100644 index 0000000..e69de29 diff --git a/JavaScript/dino-game/index.html b/JavaScript/dino-game/index.html new file mode 100644 index 0000000..bf7c63c --- /dev/null +++ b/JavaScript/dino-game/index.html @@ -0,0 +1,24 @@ + + + + + + Dino Game + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/JavaScript/dino-game/package.json b/JavaScript/dino-game/package.json new file mode 100644 index 0000000..79ebde6 --- /dev/null +++ b/JavaScript/dino-game/package.json @@ -0,0 +1,11 @@ +{ + "name": "dino-game", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Ben (plane000)", + "license": "MIT" +} diff --git a/JavaScript/dino-game/player.js b/JavaScript/dino-game/player.js new file mode 100644 index 0000000..a0394f1 --- /dev/null +++ b/JavaScript/dino-game/player.js @@ -0,0 +1,71 @@ +class Player { + constructor() { + this.x = 50; + this.y = world.ground; + this.width = 20; + this.height = 40; + this.velocityX = 5; + this.velocityY = 0; + this.onGround = true; + this.ground = world.ground; + this.ducking = false; + this.score = 0; + } + + jump() { + if (this.onGround) { + this.velocityY = -17; + this.onGround = false; + } + } + + duck() { + if (!this.ducking) { + this.ducking = true; + this.height = 20; + this.y += 20; + this.ground += 20; + } + } + + stand() { + if (this.ducking) { + this.ducking = false; + this.height = 40; + this.y = world.ground; + this.ground = world.ground; + } + } + + update() { + if (!this.onGround) { + this.velocityY += world.gravity; + this.y += this.velocityY; + // console.log(this.y) + if (this.y >= this.ground) { + this.onGround = true; + this.y = this.ground; + // console.log('landed') + return; + } + } + } + + draw() { + ctx.fillStyle="#FFFFFF"; + ctx.fillRect(this.x - 2, this.y, this.width + 4, this.height); + ctx.fillStyle="#000000"; + ctx.fillRect(this.x, this.y, this.width, this.height); + } + + reset() { + this.x = 50; + this.y = 300; + this.width = 20; + this.height = 40; + this.velocityX = 5; + this.velocityY = 0; + this.onGround = true; + this.score = 0; + } +} diff --git a/JavaScript/dino-game/style.css b/JavaScript/dino-game/style.css new file mode 100644 index 0000000..6bed620 --- /dev/null +++ b/JavaScript/dino-game/style.css @@ -0,0 +1,10 @@ +canvas { + padding: 0; + margin: auto; + display: block; + position: absolute; + top: 0; + bottom: 0; + left: 0; + right: 0; +} diff --git a/JavaScript/dino-game/world.js b/JavaScript/dino-game/world.js new file mode 100644 index 0000000..bd8828d --- /dev/null +++ b/JavaScript/dino-game/world.js @@ -0,0 +1,22 @@ +class World { + constructor() { + this.gravity = 1; + this.ground = 300; + } + + update() { + + } + + draw() { + ctx.beginPath(); + ctx.moveTo(0, this.ground + 40 - 7); + ctx.lineTo(800, this.ground + 40 - 7); + ctx.stroke(); + } + + reset() { + this.gravity = 1.3; + this.ground = 300; + } +} diff --git a/JavaScript/search-tree/index.js b/JavaScript/search-tree/index.js new file mode 100644 index 0000000..3741913 --- /dev/null +++ b/JavaScript/search-tree/index.js @@ -0,0 +1,47 @@ +class Node { + constructor(value, parent) { + this.Value = value; + this.Parent = parent; + this.Left = null; + this.Right = null; + } + + addNode(n) { + if(n.Value < this.Value) { + if (this.Left == null) { + this.Left = n; + } else { + this.Left.addNode(n); + } + } else if (n.Value > this.Value) { + if (this.Right == null) { + this.Right = n; + } else { + this.Right.addNode(n); + } + } + } +} + +class Tree { + constructor(values) { + this.Values = values; + this.Root = null; + + for (let value of this.Values) { + this.addVal(value); + } + } + + addVal(val) { + let n = new Node(val); + if (this.Root == null) { + this.Root = n; + } else { + this.Root.addNode(n); + } + } +} + +let tree = new Tree([4,2,5,3,8,1]); +console.log(JSON.stringify(tree, 4, 2)); \ No newline at end of file diff --git a/JavaScript/search-tree/package.json b/JavaScript/search-tree/package.json new file mode 100644 index 0000000..1d016c9 --- /dev/null +++ b/JavaScript/search-tree/package.json @@ -0,0 +1,11 @@ +{ + "name": "search-tree", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Ben (plane000)", + "license": "MIT" +}