how to create a game using javascript
JavaScript
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
</style>
</head>
<body onload="startGame()">
<script>
var myGamePiece;
var myObstacles = [];
var myScore;
function startGame() {
myGamePiece = new component(30, 30, "red", 10, 120);
myGamePiece.gravity = 0.05;
myScore = new component("30px", "Consolas", "black", 280, 40, "text");
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function component(width, height, color, x, y, type) {
this.type = type;
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "text") {
ctx.font = this.width + " " + this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed += this.gravity;
this.x += this.speedX;
this.y += this.speedY + this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
for (i = 0; i < myObstacles.length; i += 1) {
if (myGamePiece.crashWith(myObstacles[i])) {
return;
}
}
myGameArea.clear();
myGameArea.frameNo += 1;
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
}
for (i = 0; i < myObstacles.length; i += 1) {
myObstacles[i].x += -1;
myObstacles[i].update();
}
myScore.text="SCORE: " + myGameArea.frameNo;
myScore.update();
myGamePiece.newPos();
myGamePiece.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
function accelerate(n) {
myGamePiece.gravity = n;
}
</script>
<br>
<button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
<p>Use the ACCELERATE button to stay in the air</p>
<p>How long can you stay alive?</p>
</body>
</html>//Javascript game template
//Move player with arrow keys
var canvas = document.createElement("canvas");
canvas.width = 500;
canvas.height = 500;
document.body.appendChild(canvas);
var ctx = canvas.getContext("2d");
var player = {x: canvas.width / 2, y: canvas.height / 2, speed: 10};
var keys = [];
function update() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.fillStyle = "red";
ctx.fillRect(player.x, player.y, 50, 50);
if (keys[37])
player.x -= player.speed;
if (keys[38])
player.y -= player.speed;
if (keys[39])
player.x += player.speed;
if (keys[40])
player.y += player.speed;
requestAnimationFrame(update);
}
update();
document.onkeydown = function(e) {
keys[e.keyCode] = true;
}
document.onkeyup = function(e) {
keys[e.keyCode] = false;
}
Also in JavaScript:
- Title
- get caret position javascript
- Category
- JavaScript
- Title
- global site tag (gtag.js) - google analytics gatsby
- Category
- JavaScript
- Title
- Cannot find module '.json'
- Category
- JavaScript
- Title
- fetch response json or text
- Category
- JavaScript
- Title
- Javascript compare two dates
- Category
- JavaScript
- Title
- insert into specific array index
- Category
- JavaScript
- Title
- how to make a dictionary javascript
- Category
- JavaScript
- Title
- how to import npm packages in node.js?
- Category
- JavaScript
- Title
- bootstrap javascript cdn
- Category
- JavaScript
- Title
- discord js bot leave voice channel
- Category
- JavaScript
- Title
- flexbox stretch height
- Category
- JavaScript
- Title
- geolocation speed
- Category
- JavaScript
- Title
- javascript array push object
- Category
- JavaScript
- Title
- javascript array flatten
- Category
- JavaScript
- Title
- javascript check empty property
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- cascading dropdown for forms react
- Category
- JavaScript
- Title
- change class javascript
- Category
- JavaScript
- Title
- empty textarea using jquery
- Category
- JavaScript
- Title
- if str contains jquery
- Category
- JavaScript
- Title
- how to add multiple comment in react
- Category
- JavaScript
- Title
- clear input field javascript
- Category
- JavaScript
- Title
- find class using jquery
- Category
- JavaScript
- Title
- how to set json type jquery ajax
- Category
- JavaScript
- Title
- array length javascript
- Category
- JavaScript
- Title
- javascript check if browser is ie
- Category
- JavaScript
- Title
- create array of objects javascript
- Category
- JavaScript
- Title
- javascript all type of data
- Category
- JavaScript
- Title
- boucle for of javascript
- Category
- JavaScript
- Title
- javascript are arrays equal
- Category
- JavaScript
- Title
- how to use the map method in javascript
- Category
- JavaScript
- Title
- flowjs attributes
- Category
- JavaScript
- Title
- fibonacci best performance javascript
- Category
- JavaScript
- Title
- create element javascript with id
- Category
- JavaScript
- Title
- angular router return back
- Category
- JavaScript
- Title
- how to code print in javascript
- Category
- JavaScript
- Title
- hoisting in javascript
- Category
- JavaScript
- Title
- javascript access pushed element
- Category
- JavaScript
- Title
- append element javascript
- Category
- JavaScript
- Title
- how to change node version
- Category
- JavaScript
- Title
- google sign in in firebase react
- Category
- JavaScript
- Title
- how to take create array using jquery
- Category
- JavaScript
- Title
- javascript circular evaluation
- Category
- JavaScript
- Title
- ${ js
- Category
- JavaScript
- Title
- adjust() js
- Category
- JavaScript
- Title
- javacript is checkbox checked
- Category
- JavaScript
- Title
- for of and for in javascript
- Category
- JavaScript
- Title
- angular sanitize vs validators
- Category
- JavaScript
- Title
- eval in javascript
- Category
- JavaScript
- Title
- how to get a toggle button to do different js functions
- Category
- JavaScript
- Title
- headroom js react
- Category
- JavaScript
- Title
- injected stylesheet remove
- Category
- JavaScript
- Title
- angular 7 folder structure best practices
- Category
- JavaScript
- Title
- javascript change background color
- Category
- JavaScript
- Title
- access index of array javascript
- Category
- JavaScript
- Title
- event import in angular
- Category
- JavaScript
- Title
- how to read json file in python
- Category
- JavaScript
- Title
- how to find remainder in javascript
- Category
- JavaScript
- Title
- check if number appears odd number of times in array javascript
- Category
- JavaScript
- Title
- javascript canvas
- Category
- JavaScript
- Title
- how to assign onEdit to specigfic tab
- Category
- JavaScript
- Title
- add two numbers javascript
- Category
- JavaScript
- Title
- click point invert zoom react simple map
- Category
- JavaScript
- Title
- check if variable is undefined or null jquery
- Category
- JavaScript
- Title
- buble sort in js
- Category
- JavaScript
- Title
- charat javascript
- Category
- JavaScript
- Title
- html get elements by class
- Category
- JavaScript
- Title
- how to print NODE_PATH
- Category
- JavaScript
- Title
- eslint ignore javascript
- Category
- JavaScript
- Title
- how to move div using jquery
- Category
- JavaScript
- Title
- how to access a database in express
- Category
- JavaScript
- Title
- how to remove item from array javascript
- Category
- JavaScript
- Title
- does a function inside a function need to be called?
- Category
- JavaScript
- Title
- javascript array contains
- Category
- JavaScript
- Title
- html hide till button click
- Category
- JavaScript
- Title
- deploy angular app on google app engine
- Category
- JavaScript
- Title
- forever loop in js
- Category
- JavaScript
- Title
- evaluation of postfix expression using stack in c
- Category
- JavaScript
- Title
- download jquery
- Category
- JavaScript
- Title
- get the current url javascript
- Category
- JavaScript
- Title
- how to do a classname variable and string react
- Category
- JavaScript
- Title
- how to check if local storage variable exists in javascript
- Category
- JavaScript
- Title
- add "hr" from javascript
- Category
- JavaScript
- Title
- javascript ajouter une donnée à une list
- Category
- JavaScript
- Title
- import jquery in angular
- Category
- JavaScript
- Title
- how to add set between two date in datatable
- Category
- JavaScript
- Title
- assign array to another array javascript
- Category
- JavaScript
- Title
- how to call javascript method using selectlist on change in vf page
- Category
- JavaScript
- Title
- column.footer jquery
- Category
- JavaScript
- Title
- add month date now javascript
- Category
- JavaScript
- Title
- how to access clipboard via js
- Category
- JavaScript
- Title
- chrome add a bookmark that appends to current url
- Category
- JavaScript
- Title
- fullcalendar react add event duration
- Category
- JavaScript
- Title
- get last char javascript
- Category
- JavaScript
- Title
- access angular app outside localhost
- Category
- JavaScript
- Title
- get text selected option jquery
- Category
- JavaScript
- Title
- add 2 class names react
- Category
- JavaScript
- Title
- javascript anagram check
- Category
- JavaScript
- Title
- difference between == and === in javascript
- Category
- JavaScript
- Title
- array chunk javascript
- Category
- JavaScript