how to make a javascript game
JavaScript
//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;
}<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><!DOCTYPE html>
<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>
Also in JavaScript:
- Title
- defining props in react
- Category
- JavaScript
- Title
- javascript array stackoverflow
- Category
- JavaScript
- Title
- how to get last string in javascript
- Category
- JavaScript
- Title
- how to build a string javascript es6
- Category
- JavaScript
- Title
- get attribute value jquery
- Category
- JavaScript
- Title
- An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist.
- Category
- JavaScript
- Title
- compare two arrays javascript
- Category
- JavaScript
- Title
- javascript change attribute
- Category
- JavaScript
- Title
- add image in react native
- Category
- JavaScript
- Title
- how to remove first character from string in javascript
- Category
- JavaScript
- Title
- event.stoppropagation
- Category
- JavaScript
- Title
- array find method javascript
- Category
- JavaScript
- Title
- event handler attachment jquery
- Category
- JavaScript
- Title
- includes()
- Category
- JavaScript
- Title
- how to update jquery datatable row cell value
- Category
- JavaScript
- Title
- bash parse json
- Category
- JavaScript
- Title
- function to create an element javascript
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- how to find unique elements in array in javascript
- Category
- JavaScript
- Title
- adding element to array javascript
- Category
- JavaScript
- Title
- how to get value in array object value using for loop in javascript
- Category
- JavaScript
- Title
- javascript array remove duplicates
- Category
- JavaScript
- Title
- how to append object in array javascript
- Category
- JavaScript
- Title
- export function javascript
- Category
- JavaScript
- Title
- get client timezone offset JavaScript
- Category
- JavaScript
- Title
- how to attach javascript to html
- Category
- JavaScript
- Title
- how to deep copy object in javascript
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- document queryselector click event
- Category
- JavaScript
- Title
- delete previous line node
- Category
- JavaScript
- Title
- if clicked outside of div jquery
- Category
- JavaScript
- Title
- how to load the images from the website dynamically in react
- Category
- JavaScript
- Title
- hamster js
- Category
- JavaScript
- Title
- .shift js
- Category
- JavaScript
- Title
- express get query params from url
- Category
- JavaScript
- Title
- hover vanilla javascript
- Category
- JavaScript
- Title
- angular trigger input change
- Category
- JavaScript
- Title
- bootstrap js cdn
- Category
- JavaScript
- Title
- check window resize javascript
- Category
- JavaScript
- Title
- angular material table
- Category
- JavaScript
- Title
- bind in javascript
- Category
- JavaScript
- Title
- how to remove a property from an object in javascript
- Category
- JavaScript
- Title
- get last item in array js
- Category
- JavaScript
- Title
- get text of selected option jquery
- Category
- JavaScript
- Title
- anagram javascript example
- Category
- JavaScript
- Title
- ajax .put
- Category
- JavaScript
- Title
- append to array js
- Category
- JavaScript
- Title
- angular right click on link
- Category
- JavaScript
- Title
- how to fill false into array javascript
- Category
- JavaScript
- Title
- how to add property to object in javascript
- Category
- JavaScript
- Title
- 404 json laravel
- Category
- JavaScript
- Title
- convert object to json javascript
- Category
- JavaScript
- Title
- javascript ascending and descending
- Category
- JavaScript
- Title
- convert jquery to string
- Category
- JavaScript
- Title
- capitalize all letters jquery
- Category
- JavaScript
- Title
- handling event in jsx
- Category
- JavaScript
- Title
- add char in specific index stirng javascript
- Category
- JavaScript
- Title
- how to place text at custom position on canvas in javascript
- Category
- JavaScript
- Title
- how to get the index of an array in javascript
- Category
- JavaScript
- Title
- class component react
- Category
- JavaScript
- Title
- color js
- Category
- JavaScript
- Title
- how to remove attrivusre with jquery
- Category
- JavaScript
- Title
- chrome extension get current tab from popup
- Category
- JavaScript
- Title
- how to concatenate strings javascript
- Category
- JavaScript
- Title
- find last element with class jquery
- Category
- JavaScript
- Title
- element clicked js
- Category
- JavaScript
- Title
- Javascript compare two dates
- Category
- JavaScript
- Title
- get the value of a checkbox jquery
- Category
- JavaScript
- Title
- d3 not reading json
- Category
- JavaScript
- Title
- get day js
- Category
- JavaScript
- Title
- add month date now javascript
- Category
- JavaScript
- Title
- document queryselectorall and map javacript
- Category
- JavaScript
- Title
- add val to array jquery
- Category
- JavaScript
- Title
- functions in map javascript
- Category
- JavaScript
- Title
- how to get the end of an array javascript
- Category
- JavaScript
- Title
- javascript append to object
- Category
- JavaScript
- Title
- How do you call a functional component in React
- Category
- JavaScript
- Title
- Javascript clear canvas
- Category
- JavaScript
- Title
- get current directory vbscript
- Category
- JavaScript
- Title
- Angularjs datetime
- Category
- JavaScript
- Title
- get width of a dom element js
- Category
- JavaScript
- Title
- access session data from ejs view
- Category
- JavaScript
- Title
- how to run p5js in a particular section of page
- Category
- JavaScript
- Title
- create node project
- Category
- JavaScript
- Title
- anjular js
- Category
- JavaScript
- Title
- javascript add item to list
- Category
- JavaScript
- Title
- filtering json array in javascript
- Category
- JavaScript
- Title
- button click function in js
- Category
- JavaScript
- Title
- angular 6 reload current page
- Category
- JavaScript
- Title
- fonction fleche javascript
- Category
- JavaScript
- Title
- forever loop in js
- Category
- JavaScript
- Title
- how to filter object in javascript
- Category
- JavaScript
- Title
- fs create or edit file
- Category
- JavaScript
- Title
- facebook integration in node.js
- Category
- JavaScript
- Title
- empty div jquery
- Category
- JavaScript
- Title
- how to load localt ext file in js
- Category
- JavaScript
- Title
- default pool size in sequelize
- Category
- JavaScript
- Title
- clone an object javascript
- Category
- JavaScript
- Title
- discord.js how to go back a file
- Category
- JavaScript
- Title
- get react version
- Category
- JavaScript