showing results for - "how to make a javascript game"
Anna
06 Aug 2016
1<html>
2<head>
3<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
4<style>
5canvas {
6    border:1px solid #d3d3d3;
7    background-color: #f1f1f1;
8}
9</style>
10</head>
11<body onload="startGame()">
12<script>
13
14var myGamePiece;
15var myObstacles = [];
16var myScore;
17
18function startGame() {
19    myGamePiece = new component(30, 30, "red", 10, 120);
20    myGamePiece.gravity = 0.05;
21    myScore = new component("30px", "Consolas", "black", 280, 40, "text");
22    myGameArea.start();
23}
24
25var myGameArea = {
26    canvas : document.createElement("canvas"),
27    start : function() {
28        this.canvas.width = 480;
29        this.canvas.height = 270;
30        this.context = this.canvas.getContext("2d");
31        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
32        this.frameNo = 0;
33        this.interval = setInterval(updateGameArea, 20);
34        },
35    clear : function() {
36        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
37    }
38}
39
40function component(width, height, color, x, y, type) {
41    this.type = type;
42    this.score = 0;
43    this.width = width;
44    this.height = height;
45    this.speedX = 0;
46    this.speedY = 0;    
47    this.x = x;
48    this.y = y;
49    this.gravity = 0;
50    this.gravitySpeed = 0;
51    this.update = function() {
52        ctx = myGameArea.context;
53        if (this.type == "text") {
54            ctx.font = this.width + " " + this.height;
55            ctx.fillStyle = color;
56            ctx.fillText(this.text, this.x, this.y);
57        } else {
58            ctx.fillStyle = color;
59            ctx.fillRect(this.x, this.y, this.width, this.height);
60        }
61    }
62    this.newPos = function() {
63        this.gravitySpeed += this.gravity;
64        this.x += this.speedX;
65        this.y += this.speedY + this.gravitySpeed;
66        this.hitBottom();
67    }
68    this.hitBottom = function() {
69        var rockbottom = myGameArea.canvas.height - this.height;
70        if (this.y > rockbottom) {
71            this.y = rockbottom;
72            this.gravitySpeed = 0;
73        }
74    }
75    this.crashWith = function(otherobj) {
76        var myleft = this.x;
77        var myright = this.x + (this.width);
78        var mytop = this.y;
79        var mybottom = this.y + (this.height);
80        var otherleft = otherobj.x;
81        var otherright = otherobj.x + (otherobj.width);
82        var othertop = otherobj.y;
83        var otherbottom = otherobj.y + (otherobj.height);
84        var crash = true;
85        if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
86            crash = false;
87        }
88        return crash;
89    }
90}
91
92function updateGameArea() {
93    var x, height, gap, minHeight, maxHeight, minGap, maxGap;
94    for (i = 0; i < myObstacles.length; i += 1) {
95        if (myGamePiece.crashWith(myObstacles[i])) {
96            return;
97        } 
98    }
99    myGameArea.clear();
100    myGameArea.frameNo += 1;
101    if (myGameArea.frameNo == 1 || everyinterval(150)) {
102        x = myGameArea.canvas.width;
103        minHeight = 20;
104        maxHeight = 200;
105        height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
106        minGap = 50;
107        maxGap = 200;
108        gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
109        myObstacles.push(new component(10, height, "green", x, 0));
110        myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
111    }
112    for (i = 0; i < myObstacles.length; i += 1) {
113        myObstacles[i].x += -1;
114        myObstacles[i].update();
115    }
116    myScore.text="SCORE: " + myGameArea.frameNo;
117    myScore.update();
118    myGamePiece.newPos();
119    myGamePiece.update();
120}
121
122function everyinterval(n) {
123    if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
124    return false;
125}
126
127function accelerate(n) {
128    myGamePiece.gravity = n;
129}
130</script>
131<br>
132<button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
133<p>Use the ACCELERATE button to stay in the air</p>
134<p>How long can you stay alive?</p>
135</body>
136</html>
Gabriela
04 Jan 2017
1//Javascript game template
2//Move player with arrow keys
3
4var canvas = document.createElement("canvas");
5canvas.width = 500;
6canvas.height = 500;
7document.body.appendChild(canvas);
8var ctx = canvas.getContext("2d");
9
10var player = {x: canvas.width / 2, y: canvas.height / 2, speed: 10};
11var keys = [];
12
13function update() {
14  ctx.clearRect(0, 0, canvas.width, canvas.height);
15  
16  ctx.beginPath();
17  ctx.fillStyle = "red";
18  ctx.fillRect(player.x, player.y, 50, 50);
19  
20  if (keys[37])
21    player.x -= player.speed;
22  if (keys[38])
23    player.y -= player.speed;
24  if (keys[39])
25    player.x += player.speed;
26  if (keys[40])
27    player.y += player.speed;
28  
29  requestAnimationFrame(update);
30}
31update();
32
33document.onkeydown = function(e) {
34  keys[e.keyCode] = true;
35}
36document.onkeyup = function(e) {
37  keys[e.keyCode] = false;
38}
Anna
21 Nov 2020
1window.onload = function() {
2     canvas = document.createElement("canvas");
3     canvas.width = 400;
4     canvas.height = 400;
5     canvasContext = canvas.getContext("2d");
6     document.body.appendChild(canvas);
7     setInterval(gameCanvas, 1000/10);
8}
9function gameCanvas() {
10     canvasContext.fillStyle = "black";
11     canvasContext.fillRect(0, 0, canvas.width, canvas.height);
12     canvasContext.fillStyle = "cyan";
13     canvasContext.fillRect(0, 330, 20, 70);
14     canvasContext.fillStyle = "orange";
15     canvasContext.fillRect(380, 330, 20, 70);
16     canvas.addEventListener("click", function() {
17          canvasContext.fillStyle = "yellow";
18          canvasContext.fillRect(25, 360, 15, 7);
19     });
20     canvasContext.fillStyle = "darkblue";
21     canvasContext.fillRect(0, 0, canvas.width, 100);
22     canvasContext.fillStyle = "red";
23     canvasContext.fillRect(0, 100, canvas.width, 100);
24     canvasContext.fillStyle = "gray";
25     canvasContext.fillRect(30, 80, 80, 30);
26     canvasContext.fillStyle = "gray";
27     canvasContext.fillRect(200, 150, 80, 30);
28     canvasContext.fillStyle = "gray";
29     canvasContext.fillRect(250, 50, 80, 30);
30}
queries leading to this page
simple game code in javascripthow to make a vr game in unitybuild game in javascripthtml game programminghow to make a game using htmlhow to make a game in javascript codehow to make a n onlin html gamecreate a game with javascript quicklymake html gamesgames in javascriptjavascript code for a gamegames javascript codemake a game with javascript and html5really simple game html and csscodes for html to make gamejavascript html gammeshtml js how make gamegames in jscoding a game in javascriptlingo game create with javascriptbuilding a game with javascriptcode javascript gamebuild games with javascripthow to create a game using javascripthow to make a simple game in htmlhow to make a game with jsjs gamescreating a game using javascriptmaking a game in javascriptmaking game in javascriptmaking games with jsbuilding game in javascriptmake game in jshow to make a javascript gamecreate game with htmlhow make a game with javascripthow to put a game in htmlcreate html gamehtml game code funsample games javascriptcreate game by javascripthow to create a game with javascriptbuild games with jscreate a game with javascriptcreate a game in javascriptcreate a game using javascript 5ccreate js gamecreate a game html and jsmaking a game in cssjavascript code of a gamecreate games with javascriptbuild a simple javascript gamehow to create game javascripthow to make a game with htmlhow to create a javascript gamesimple javascript gamehow to make games in html 5simple game with javascriptjavascript game development tutorialshow to make a simple js gamejavascript make a gamemake games in javascriptsimple javascript game tutorialhow to create a game in javascripthow to put a javascript game into my html websitebuild a game using htmllearn how to make a game with htmlhtml games code to addjavacript gameseasy javascript games to makejavascript simple game codemak a game using html 2c css and jshow to create games in html js and cssmake a js browser gamemaking a game wid htmlhow to make a javascript game with codebuild a game using javascriptcan you make games with javascriptmake a game using javascriptmaking a game with javascriptjavascript game engineeasy to make javascript gamsbuild a basic js gamehow to make games on javascriptonline game devolpment javascript htmlsimple web game htmleasy html gamehtml5 canvas gamegame on javascriptcode game in javascriptjava script how to make a gamemaking the easiest game in javascripthow to make a simple game in javascriptmake javascript gameshtml5 game tutorialhow to create a js and html gamehow to create a simple game using jsmaking games using javascriptcan you make a game in javascripthtml css and javascript basic gamecreate game in jsmake javascript game tutorialhtml games projectbuilding a javascript gamehow to make a online game usinfg htmljavascript game development tutorialsimple game in javascript source codehow to make game using javascripthow javascript gamecreating a game in jsjava script gamedsmake a javscript gamemake a simple game using javascriptbuild a game javascriptmake a game in jshow to develop a game with jsweb html gamegame javascript wcoding a game with javascripthow to create games using javascriptbest way to make a game in javascriptjavascript html5 gamegave development with html game with javascripthow to build a javascript game without htmlhow to create games in javascriptjavascript game creationmaking a game wid html only htmlcoding with javascript gamehow to make a gmae using htmlhow to make a game in html and javascripthow to code a game in javascri 5bthow to make games on jsbuild game with javascripthow to make game with javascripthow to code a js gamesweb games in javascriptbuilding games in javascriptsimple javascript game codecreating game using javascripthow to create a game from javascripthow to make a game using javascriptwrite javascript gamesmaking a game using javascript for beginnershow to make a js gamecreate a new game jsgame development in javascripthow to create a game in javasc ript create a game in html languagehow do you create a video game i js 3fgame javascript codewhat is the best way to make a game in javascripthow do you create a video game injs 3fcreate game using java scriptmake a game in js websitescratch game js librarysimple game using javascriptlearn make game in javascriptmaking a game using jsjs game codemaking game with javascriptmaking a game in htmlhtml game basicsnew game javascriptmaking game in jsgame jsmake javascript 2c html and css gamejavascript games tutorialgame making javascripthtml5 javascript gamemake js game for browserhow to make a game using jshow to make a game in html css and javascripthow to create game in javascripthow to code a game on javascripthtml gamegame javascript tutorialhow javascript games makemake a js gamemake a game with javascriptbuild game using javascripthtml5 or javascript for gameshow to make a game in jsjavascript game codehow to make games with javascripthow to make a make game in jswrite a bsic html gamehow to make an html gamegame in jshow to create game with javascriptwriting a game in javascripthow to code a game with jshow to code a simple game jsjavascript game makingjava scripts command gamecreate game javascriptcan you make games using javascriptputting a python game in javascriptjs game designhow to create a simple game in javascriptcreating game in jsjavascript coding minigamefirst simple game coding in htmlhow to make game with jshow to create bakamon game in javascripttrist javascript gameshow to create a game with jshow to create games with javascriptmaking game javascripthow do you create a game in javascript 27how to do javascript gamemaking a js gamesimple game using java scriptwrite games in javascripthow to make a game with javascriptadd game phpjavascript simple gamesgame in javascriptjavascript gamesa simple game to create with javascripthow to make html gamesjavascript game developinghow to make a small game in javascriptjs simple gamehtml code to make a gamegame using html css javascriptcreate game html5 javascriptcan u make a game with jswrite a game in javascriptgames for javascriptcodes for java script to make an gamemake a game in javascripthow make a game with java scriptcreating a game with javascriptbuilding a game using javascript and htmlhow to make games in jshow to build a game in javascriptcreate a javascript gamejavascript game development code referencecreate a simple game javascriptcreate a game with javascript and htmljavascript make gamejs code gamewho to program html canvas gameshow to make a game jseasy html game to createhow make game javascripthow to make games using js3game programming javascripthtml gamesmake javascript gamejavascript html css game examplehow to make a game in html5 and javascriptjavascript game 3fjavascript tutorial game how to make agame in javascripthow do i make a game in html csscan i make a game with javascriptdevelop game in javascriptcan you make game with javascriptgame html jssimple game javascriptsimple javascript game examplemaking a game with javsacriptcreate ganes with javascripthtml game windowmake game by jsbuild a game in javascriptis making a javascript game easymaking a javascript gamehow to make game in jsusing javascript to make a gamesimple game in javascripthow to program a game in javascriptlingo game create with javascript tutorialdesigning game with javascriptgames in html5how to make a 3d game in htmlhow to create html gamescode a game in javascriptcoding a javascript gamebuild a javascript gamehow to make a web game with javascriptsimple game in jsgame tutorial in javascripthow to make a game htmljavascript game htmlgame made with javascriptmaking a game in jsjavascript games tutorialsw3schools html gamemaking a game with java scripthow to codea js gamehow to create a game in jshow can i build game by jshow to make a game in javascript for beginnersjavascript game development with demohtml building game codebuild a simple game in javascriptbasic javascript gamehow to make games on htmlinteractive game javascriptgame using html and csshow to code a game in javascript video game javascriptmaking games in jscreate game canvas html5js game examplemaking javascript gameshow can i make a game with javascriptmake game with javascripthow to make a html gsme 5dcreate game javascript html5how to creat a game in html onlyjavascript game in jscan we make games using javascripthow to make javascript gamesgame using javascriptjavascript how to make gamejavascript game tutorial for beginnersmake html gamesimple javascript gameshow to create a game in htmlgame javascriptbasic javascript game codegame in html5how to make a asimple js gamemake a game with jsmake games with csssimple games in jshow to create a javascript game in websitehtml basic gamecreating a game in jscriptjavascript web game tutorialhow to code a game in jshow to make a basic game in javascripthow to make game app on javascriptgame makig using jsusing js to make a gamegames with javascriptcreate simple game with javascriptgame like tutorial for jsmake game with jscreate a video game app w3 schoolshow to make javascript gamewhat we need to make a game with javascriptmake g html gamehow to make a simple game with javascriptmake games using javascripthow to make a game in with htmlchreate a game with jshtml game css jshtml 5 game tutorialcreate game javascriptfirst javascript gamehow to build a javascript gamehow to code a game with javascriptjavascript game program exemplehow to make a 2d game unityhow to make a simple game with html and csslearn html gamehow to make a javascript gfamehow to code games in javascriptmaking games with javascriptgame development in javascript 3fcreate a gamews in javascriptmaking js gamesmaking a game with jscodes for java script to make games simple games to make in javascriptmaking game in html 27java script game examplesjava script making a gamehow to code a game in html js csshow to make a game in javascript tutorialhow to code a game javascriptgame with jshow to make a game using html5easiest javascript gamescreate game with javascriptjava script gamejavascript create web gamesimple diy javascript gamelearn how to make games with javascriptjs game examplesmake js gamecreate a game using javascriptbasic game javascriptsimple game using html and cssgame creator html css javascripthow to make a game in javascriptdevelop a game in htmlmake a simple game with javascripthow to make a basic game in jscreating a game in javascriptmake games using html css and jswhat do i need to build a game with javascriptcreate js game examplegames in htmlhow to make games using javascriptjavascript making a game tutorialjavascript game tutoriallearning to create game with jscreate simple web gamesimple html game codehow to make a game only using jsjavascript basic gamehow to code a javascript gamejavascript game tutorialsgame in javascript tutorialthings to add to your js gamehow to make a game with javascript and html5 cssgames created with javascriptjavascript as a gamehow to make a game in javascript for beginners with only codeshow to create games with htmljavasript gamehow to make games with htmlcreate games beginner javascriptmaking a html game tutorial 1building game javascripthow to make js browser gamegames create javascripthow to make a game javascriptminigame javascriptcan you make a game with javascriptjavascript webpage gamehow to add html gamesjavascript gamehtml5 game codemaking a simple game on jscreate interactive game in javascriptgame jsgame using html and javsscripthow to make a game on htmllearn how to make games with html css and javascriptcodes for javascript to make an gamejavascript building gamegames with jsgame navigation html5html css js gamesbasic game in jsjs simple game examplehow to make a html gamehtml game developmentjavascript basic game tutorialhow to make a browser game with javascripthtml code for gamesjavascript game code tutorialjavascript simple gamehow to make a game from javascriptmake game javascriptrunner game with html css and javascript tutorialjavascript gmehow to make a video game in html anhow to create a easy game in javascriptjavascript game with htmlhtml 5 game projecthow to code a simple javascript gamegame code in jsbuilding a game using javascripthow to make js gamesgames using html css javascriptgame using javascript codehow to make a game with javascript and html5make game javascript tutorialbuilding games with javascriptmini game htmljs script gamehow to make a 2d game in htmlgame in javascript codegame code in javascriptcreating a javascript gamesimple html gamehow to make a new game javascripthtml css game detailgame using htmlmake your own game with jsjavascript build a web gamejavascript game examplejavascript videogame projectsmakeing a javscript gamehtml and js gamesmaking a game using javascriptjavascript how to make a gamecan i use javascript for making game 3fhow to make game in javascriptjavascript html gamesmaking html gameshow to make games in javascriptmake a game javascriptbasic games javascriptbuild game javascripthtml gamge simplehow to creat a game in htmlhow made javascript gamejavascript game basicsjavascript code a gamehow to create game using javascriptbuild simple game jshow do you make a game with javascriptsimple javascript games for beginnersmaking a game with javascript codejavascript making games tutorialcreating javascript gamescreating a javascript game in the browsercreate game with jsgame in htmlhow to create a game in javascript with graphicsmake a javascript gamejs gamecan you make game in javascriptjavascript game programminghow to make a game with html and jscan i built a game with jsdifferent ways to go about making a javascript gamejavascript making a gamemake game in javascriptjava for html code game onlinehow to code a js gamejavascript videogamehow to make a game in htmlcreate javascript gamehow to make game on javascriptmake games with javascripthow to make a javascript game in htmljs html and css gamemake a game jsgame program in javascriptcan i create games in javascriptjavascript game developmenthow to make a good game by javascriptcreate game in javascripthowb to make a game in jshow to add javascrpt game in htmlgame with javascriptbuilding a simple game with javascriptjavascript making a tag gamegames for pc in html csscould you make a simple game with javascriptjavascript coding gamemaking a simple game in javascriptcreate a game with jshow to develop game in jshow to make agame in javascrigames made on canvas tag in htmlbasic js gamemaking games in javascriptmaking game with only javascriptcode a javascript gamesimple js gamesjavascript make gamesmini html js gamesimple js gamevideo game javascrip beginnercreate simple games with javascriptgame java scripthow to make games with only jshow to build a video game in javascripthow to make a javascript game