HTML5 game experiment with CreateJS

Developer
Size
4,135 Kb
Views
56,672

How do I make an html5 game experiment with createjs?

My first try with CreateJS, using EaselJS and TweenJS. Credits to Brad Manderscheid (https://twitter.com/bmanderscheid), who inspired this game with his book 'Beginning HTML5 Games with CreateJS' (http://www.apress.com/9781430263401).. What is a html5 game experiment with createjs? How do you make a html5 game experiment with createjs? This script and codes were developed by Michaela on 27 July 2022, Wednesday.

HTML5 game experiment with CreateJS Previews

HTML5 game experiment with CreateJS - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>HTML5 game experiment with CreateJS</title> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ @import url(http://fonts.googleapis.com/css?family=Open+Sans:300);
body,
button {	background-color: #E8E5E0; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 300; line-height: 1.5; letter-spacing: 0.05em;
}
button {	border: none;	outline: none;
}
#canvas {	background-color: #CD7794;
}
#restart {	background-color: transparent;	border: 1px solid #CD7794;	color: #CD7794;	cursor: pointer;	padding-top: 4px;	text-transform: uppercase;
}
.container {	display: block;	margin-top: -280px;	margin-left: -300px;	position: absolute;	top: 50%;	left: 50%;
}
.manual {	color: #F5AAC8;	font-size: 2em;	text-align: center;
}
.modal {	background-color: rgba(255, 255, 255, 0.8);	display: none;	margin-top: -16px;	margin-left: -300px;	position: absolute;	top: 50%;	left: 50%;	text-align: center;	width: 600px;	height: 100px;
}
.modal.is-visible {	display: block;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <body onload="init()"> <div class="container"> <p class="manual">Drag and drop the shapes</p> <canvas id="canvas" width=600 height=400></canvas> </div> <div id="restart-dialog" class="modal"> <p> Yeeha, you won! You must be a very smart guy! </p> <button id="restart" name="button">Restart</button> </div>
</body> <script src='http://code.createjs.com/createjs-2013.12.12.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

HTML5 game experiment with CreateJS - Script Codes CSS Codes

@import url(http://fonts.googleapis.com/css?family=Open+Sans:300);
body,
button {	background-color: #E8E5E0; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 300; line-height: 1.5; letter-spacing: 0.05em;
}
button {	border: none;	outline: none;
}
#canvas {	background-color: #CD7794;
}
#restart {	background-color: transparent;	border: 1px solid #CD7794;	color: #CD7794;	cursor: pointer;	padding-top: 4px;	text-transform: uppercase;
}
.container {	display: block;	margin-top: -280px;	margin-left: -300px;	position: absolute;	top: 50%;	left: 50%;
}
.manual {	color: #F5AAC8;	font-size: 2em;	text-align: center;
}
.modal {	background-color: rgba(255, 255, 255, 0.8);	display: none;	margin-top: -16px;	margin-left: -300px;	position: absolute;	top: 50%;	left: 50%;	text-align: center;	width: 600px;	height: 100px;
}
.modal.is-visible {	display: block;
}

HTML5 game experiment with CreateJS - Script Codes JS Codes

var stage,	shapes = [],	slots = [],	score = 0;
/** * Initializing the game */
function init () {	stage = new createjs.Stage('canvas');	buildShapes();	setBlocks();	startGame();
}
/** * Build the slots and draw them on canvas, * build the shapes and prepare to randomize them. * Every slot and shape gets a key to bind them together. * Both shapes and slots were pushed in their arrays. */
function buildShapes () {	var colors = ['rect', 'circle', 'star', 'roundrect'],	i,	shape,	slot;	for ( i = 0; i < colors.length; i++ ) {	slot = new createjs.Shape();	slot.graphics.beginStroke('#F2DEE7');	slot.graphics.beginFill('#CD7794');	switch (i) {	case i = 0:	slot.graphics.drawRect(0, 0, 100, 100);	slot.regX = slot.regY = 50;	break;	case i = 1:	slot.graphics.drawCircle(0, 0, 50);	slot.regX = slot.regY = 0;	break;	case i = 2:	slot.graphics.drawPolyStar(0, 0, 50, 6, 0.6, -90);	slot.regX = slot.regY = 0;	break;	case i = 3:	slot.graphics.drawRoundRect(0, 0, 100, 100, 25);	slot.regX = slot.regY = 50;	break;	}	slot.key = i;	slot.y = 80;	slot.x = (i * 130) + 100;	stage.addChild(slot);	slots.push(slot);	shape = new createjs.Shape();	shape.graphics.beginFill('#F2DEE7');	switch (i) {	case i = 0:	shape.graphics.drawRect(0, 0, 100, 100);	shape.regX = shape.regY = 50;	break;	case i = 1:	shape.graphics.drawCircle(0, 0, 50);	shape.regX = shape.regY = 0;	break;	case i = 2:	shape.graphics.drawPolyStar(0, 0, 50, 6, 0.6, -90);	shape.regX = shape.regY = 0;	break;	case i = 3:	shape.graphics.drawRoundRect(0, 0, 100, 100, 25);	shape.regX = shape.regY = 50;	break;	}	shape.key = i;	shapes.push(shape);	}
}
/** * Set the blocks on stage on random places */
function setBlocks () {	var i,	r,	shape,	l = shapes.length;	for ( i = 0; i < l; i++ ) {	r = Math.floor(Math.random() * shapes.length);	shape = shapes[r];	shape.homeY = 320;	shape.homeX = (i * 130) + 100;	shape.y = shape.homeY;	shape.x = shape.homeX;	shape.addEventListener('mousedown', startDrag);	stage.addChild(shape);	shapes.splice(r, 1);	}
}
/** * Game logic: * Drag and drop the shapes, * test if they are over a suitable slot, * if yes, place them in slot and count up, * if no, remove them to their home place. * * @param {event} e - mousedown event */
function startDrag (e) {	var shape = e.target,	slot = slots[shape.key];	stage.setChildIndex(shape, stage.getNumChildren() - 1);	stage.addEventListener('stagemousemove', function (e) {	shape.x = e.stageX;	shape.y = e.stageY;	});	stage.addEventListener('stagemouseup', function (e) {	var pt = slot.globalToLocal(stage.mouseX, stage.mouseY);	stage.removeAllEventListeners();	if ( shape.hitTest(pt.x, pt.y) ) {	shape.removeEventListener('mousedown', startDrag);	score++;	createjs.Tween.get(shape).to({x:slot.x, y:slot.y}, 200,	createjs.Ease.quadOut).call(checkGame);	}	else {	createjs.Tween.get(shape).to({x:shape.homeX, y:shape.homeY}, 200,	createjs.Ease.quadOut);	}	});
}
/** * Check if the shapes are all placed correctly. * If yes, give the user feedback * and the possibility to reload the game */
function checkGame () {	if ( score === 4 ) {	var modal = document.getElementById('restart-dialog'),	restartButton = document.getElementById('restart');	modal.className = modal.className + ' is-visible';	restartButton.onclick = function (e) {	e.preventDefault(); modal.className = 'modal'; score = 0;	init();	};	}
}
/** * Start and update the game */
function startGame () {	createjs.Ticker.setFPS(60);	createjs.Ticker.addEventListener('tick', function (e) {	stage.update();	});
}
HTML5 game experiment with CreateJS - Script Codes
HTML5 game experiment with CreateJS - Script Codes
Home Page Home
Developer Michaela
Username Fischaela
Uploaded July 27, 2022
Rating 3
Size 4,135 Kb
Views 56,672
Do you need developer help for HTML5 game experiment with CreateJS?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Michaela (Fischaela) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!