Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="pool">

	<canvas id="room" width="512px" height="288px"></canvas>

</div>
              
            
!

CSS

              
                body {
  background-color: #202020;
}

#pool {
  margin: auto;
  width: 512px;
  display: block;
  text-align: center;
}

canvas {
  border: 2px solid #FFFFFF;
  box-shadow: 0px 1px 1px #000000;
  width: 100%;
  height: 100%;
  max-width: 512px;
  max-height: 288px;
  display: block;
  position: relative;
}
              
            
!

JS

              
                window.addEventListener("keydown", function(e) {
    // space and arrow keys
    if([32, 37, 38, 39, 40].indexOf(e.keyCode) > -1) {
        e.preventDefault();
    }
}, false);

//Import some math functions
	//Such as sign...
	function sign(x) {
		return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
		};
	function radians(degrees) {
	  return degrees * Math.PI / 180;
		};
	function lengthdir_x(len, dir) {
		return (cos(radians(dir)) * len);
		};
	function lengthdir_y(len, dir) {
		return (-sin(radians(dir)) * len);
		};

	document.addEventListener('DOMContentLoaded', domloaded, false);

	function domloaded() {

	//---First we'll initialize our canvas---

		var canvas = document.getElementById('room');
		var ctx = canvas.getContext('2d');

		//Now we have a "room" to work with, so let's set an FPS
		var FPS = 60;

		//Get our room's width and height
		var room_width = document.getElementById('room').width;
		var room_height = document.getElementById('room').height;

		//Make some global variables
		var gravity = 0.5,
		velocity = 12,
		left = 0,
		up = 0,
		right = 0,
		canMove = 0,
		canJump = 0,
		tileSize = 32,
		cLength = Math.sqrt(2 * (Math.pow(tileSize, 2))),
		pixelRatio = (cLength / tileSize);

		var map = //16 * 9 Grid; 1 = wall, 0 = empty
		[[6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
		[6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
		[6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7],
		[6,0,0,0,0,0,2,5,5,3,0,0,0,0,0,7],
		[1,3,0,0,0,2,1,1,1,1,3,0,0,0,2,1],
		[1,6,0,0,0,7,1,1,1,1,6,0,0,0,7,1],
		[1,6,0,0,0,4,4,4,4,4,4,0,0,0,7,1],
		[1,6,0,0,0,0,0,0,0,0,0,0,0,0,7,1],
		[1,1,5,5,5,5,5,5,5,5,5,5,5,5,1,1]];

		function loadLevel() { 
			var x;
			if(navigator.appName.search('Microsoft') >- 1) {
				x = new ActiveXObject('MSXML2.XMLHTTP');
				} else {
				x = new XMLHttpRequest();
				};
			x.onreadystatechange = function() {
				map = x.responseText;
				};
			x.open("GET", "level_1.txt?t=" + Math.random(), true);
			x.send();
			}

		//loadLevel();


		var i, j, row, tileNumb,
		tileType = new Array(),
		tileX = new Array(),
		tileY = new Array();
		tileNumb = 0;
		row = 0;

		var buildMap = function() {

			for (i = 0; i < map.length; i++) { //Repeats per the length (amount of rows) of our map

		        row = map[i]; //Holds the current row in our map

		        for (j = 0; j < row.length; j++) { //Moves down the length of the row, seperates tiles

		            tileNumb += 1; //The number of the tile per row
		            tileX[tileNumb] = (j * tileSize);
		            tileY[tileNumb] = (i * tileSize);
		            tileType[tileNumb] = row[j];

		            };
		        };

		    };


		buildMap();

		//---Let's make our player object---
		var obj_player = {
			x: room_width / 2,
			y: 16,
			hspd: 0,
			vspd: 0,
			accel: 0.6,
			deccel: 0.3,
			smax: 5,
			jump: 12,
			size: 16,
			horKey: 0
			};

		//---Here goes our main gameplay code (Math!)---
		var update = function() {

			document.onkeydown = function(e) {
				e = e || window.event;
				switch(e.keyCode) {
					case 37:
						left = 1;
						break;
					case 38:
						up = 1;
						break;
					case 32:
					    up = 1;
					    break;
					case 39:
						right = 1;
						break;
					};
				};

			document.onkeyup = function(e) {
				e = e || window.event;
				switch(e.keyCode) {
					case 37:
						left = 0;
						break;
					case 38:
						up = 0;
						break;
					case 32:
					    up = 0;
					    break;
					case 39:
						right = 0;
						break;
					};
			    };

			obj_player.horKey = ((right) - (left)); //Check for horizontal movement


			//Accelerate Horizontally
			if (obj_player.horKey != 0) {
				obj_player.hspd += (obj_player.accel * obj_player.horKey);
				if (obj_player.hspd > obj_player.smax) {
					obj_player.hspd = obj_player.smax;
					} else 
				if (obj_player.hspd < (-1 * obj_player.smax)) {
					obj_player.hspd = -1 * obj_player.smax;
					};
				};

			//Deccelerate Horizontally
			if (Math.abs(obj_player.hspd) > obj_player.deccel) {
				obj_player.hspd -= (obj_player.deccel * sign(obj_player.hspd));
				} else {
				obj_player.hspd = 0;
				};

			canMove = 1;
			for(i = Math.abs(obj_player.hspd); i > 0; i--) {
				if (((obj_player.x + sign(obj_player.hspd)) > obj_player.size) && ((obj_player.x + sign(obj_player.hspd)) < room_width - obj_player.size)) {
					
					for (a = 0; a <= tileNumb; a++) {

						if ((tileType[a] == 1) || (tileType[a] == 5) || (tileType[a] == 6) || (tileType[a] == 7)) {

							if (((obj_player.y + obj_player.size) > tileY[a]) && ((obj_player.y - obj_player.size) < (tileY[a] + tileSize))) {

								if ((obj_player.x) < tileX[a]) {

									if ((obj_player.x + sign(obj_player.hspd)) < (tileX[a] - obj_player.size + 1)) {
										canMove = 1;
										} else {
										canMove = 0;
										break;
										};

										} else if ((obj_player.x + sign(obj_player.hspd)) > (tileX[a] + tileSize + obj_player.size - 1)) {
										canMove = 1;
										} else {
										canMove = 0;
										break;
										};
									};
								} else

							if (tileType[a] == 2) {

								if (((obj_player.y + obj_player.size) >= tileY[a]) && ((obj_player.y + obj_player.size) <= (tileY[a] + tileSize))) {

									if ((obj_player.x + obj_player.size) < tileX[a] + tileSize) {

										if ((obj_player.x + sign(obj_player.hspd)) < (tileX[a] - obj_player.size - ((obj_player.y + obj_player.size) - (tileY[a] + tileSize)))) {
											canMove = 1;
											} else {
											if ((obj_player.y + obj_player.size - 1) < tileY[a] + tileSize) {
												canMove = 1;
												obj_player.y -= 1;
												break;
												} else {
												canMove = 0;
												break;	
												}
											};
										};
									};
								} else

							if (tileType[a] == 3) {

								if (((obj_player.y + obj_player.size) >= tileY[a]) && ((obj_player.y + obj_player.size) <= (tileY[a] + tileSize))) {

									if ((obj_player.x - obj_player.size) > tileX[a]) {

										if ((obj_player.x + sign(obj_player.hspd)) > (tileX[a] + obj_player.size + ((obj_player.y + obj_player.size) - tileY[a]))) {
											canMove = 1;
											} else {
											if ((obj_player.y + obj_player.size - 1) < tileY[a] + tileSize) {
												canMove = 1;
												obj_player.y -= 1;
												break;
												} else {
												canMove = 0;
												break;	
												}
											};
										};
									};
								};
							};



					if (canMove == 1) {
						obj_player.x += sign(obj_player.hspd);
						} else {
						obj_player.hspd = 0;
						break;
						};


					} else {
					obj_player.hspd = 0;
					break;
					};
				};
			
			
			if (obj_player.y < room_height - obj_player.size) {
				obj_player.vspd += gravity;
				if (obj_player.vspd > velocity) {
					obj_player.vspd = velocity;
					} else 
				if (obj_player.vspd < (-1 * velocity)) {
					obj_player.vspd = -1 * velocity;
					};
				if ((obj_player.vspd < 0) && (up == 0)) {
					obj_player.vspd /= 1.1;
					};
				} else {
				obj_player.vspd = 0;
				if (up == 1) {
					obj_player.vspd -= obj_player.jump;
					};
				};
			
			canMove = 1;
			canJump = 0;
			for(i = Math.abs(obj_player.vspd); i > 0; i--) {
				if (((obj_player.y + sign(obj_player.vspd)) > obj_player.size) && ((obj_player.y + sign(obj_player.vspd)) < room_height - obj_player.size)) {

					for (a = 0; a <= tileNumb; a++) {

						if ((tileType[a] == 1) || (tileType[a] == 5) || (tileType[a] == 6) || (tileType[a] == 7)) {
							
							if (((obj_player.x + obj_player.size) > tileX[a]) && ((obj_player.x - obj_player.size) < (tileX[a] + tileSize))) {

								if ((obj_player.y) < tileY[a]) {

									if ((obj_player.y + sign(obj_player.vspd)) < (tileY[a] - obj_player.size + 1)) {
										canMove = 1;
										} else {
										canMove = 0;
										canJump = 1;
										break;
										};

										} else if ((obj_player.y + sign(obj_player.vspd)) > (tileY[a] + tileSize + obj_player.size - 1)) {
										canMove = 1;
										} else {
										canMove = 0;
										vspd = 0;
										break;
										};
									};
								} else 

								if (tileType[a] == 2) {

									if (((obj_player.x + obj_player.size) > tileX[a]) && ((obj_player.x + obj_player.size - 1) < (tileX[a] + tileSize))) {

										if ((obj_player.y + obj_player.size) < (tileY[a] + tileSize)) {

											if ((obj_player.y + obj_player.size + sign(obj_player.vspd)) < (tileY[a] - ((obj_player.x + obj_player.size) - (tileX[a] + tileSize)))) {
												canMove = 1;
												} else {
												canMove = 0;
												canJump = 1;
												break;
												};
											};

										} else {
										canMove = 1;
										};
									} else

								if (tileType[a] == 3) {

									if (((obj_player.x - obj_player.size + 1) > tileX[a]) && ((obj_player.x - obj_player.size) < (tileX[a] + tileSize))) {

										if ((obj_player.y + obj_player.size) < (tileY[a] + tileSize)) {

											if ((obj_player.y + obj_player.size + sign(obj_player.vspd)) < (tileY[a] + ((obj_player.x - obj_player.size) - tileX[a]))) {
												canMove = 1;
												} else {
												canMove = 0;
												canJump = 1;
												break;
												};
											};

										} else {
										canMove = 1;
										};
									};
								};


					if (canMove == 1) {
						obj_player.y += sign(obj_player.vspd);
						} else {
						obj_player.vspd = 0;
						break;
						};

					} else {
					obj_player.vspd = 0;
					break;
					};
				};
			
				if (canJump == 1) {
					if (up == 1) {
						obj_player.vspd -= obj_player.jump;
						canJump = 0;
						};
					};
			


			};

		//---Time to draw---
		var render = function() {

			var cTop = "#E4F797",
			cWall = "#505050",
			cBack = "#D43552",
			cPlayer = "#FFE32E";

			//Here we'll draw a background to the canvas
			ctx.beginPath();
			ctx.rect(0, 0, room_width, room_height);
			ctx.fillStyle = cBack;
			ctx.lineWidth = 0;
			ctx.fill();
			ctx.closePath();

			for (i = 0; i <= tileNumb; i++) {

				if (tileType[i] == 4) {

			        ctx.beginPath();
			        ctx.rect(tileX[i], tileY[i], tileSize, tileSize);
			        var shadowGrad = ctx.createLinearGradient(0, tileY[i], 0, tileY[i] + tileSize);
			    	shadowGrad.addColorStop(0, "rgb(200, 200, 200)");
					shadowGrad.addColorStop(1, "rgb(255, 255, 255)");
					ctx.globalCompositeOperation = "multiply";
			        ctx.fillStyle = shadowGrad;
			        ctx.fill();
			        ctx.closePath();
			        ctx.globalCompositeOperation = "source-over";

			    	};

				if (tileType[i] == 1) {

			        ctx.beginPath();
			        ctx.rect(tileX[i], tileY[i], tileSize, tileSize);
			        ctx.fillStyle = cWall;
			       	ctx.fill();
			        ctx.closePath();

			    	};

			    if (tileType[i] == 2) {

			        ctx.beginPath();
			        ctx.moveTo(tileX[i], tileY[i] + tileSize);
			        ctx.lineTo(tileX[i] + tileSize, tileY[i] + tileSize);
			        ctx.lineTo(tileX[i] + tileSize, tileY[i]);
			        ctx.fillStyle = cWall;
			        ctx.fill();
			        ctx.closePath();

			    	};

			    if (tileType[i] == 3) {

			    	ctx.beginPath();
			        ctx.moveTo(tileX[i], tileY[i]);
			        ctx.lineTo(tileX[i], tileY[i] + tileSize);
			        ctx.lineTo(tileX[i] + tileSize, tileY[i] + tileSize);
			        ctx.fillStyle = cWall;
			        ctx.fill();
			        ctx.closePath();

			    	};

			    if (tileType[i] == 5) {

			        ctx.beginPath();
			        ctx.rect(tileX[i], tileY[i], tileSize, tileSize);
			        ctx.fillStyle = cWall;
			       	ctx.fill();
			        ctx.closePath();

			    	};

			    if (tileType[i] == 6) {

			        ctx.beginPath();
			        ctx.rect(tileX[i], tileY[i], tileSize, tileSize);
			        ctx.fillStyle = cWall;
			       	ctx.fill();
			        ctx.closePath();

			    	};

			    if (tileType[i] == 7) {

			        ctx.beginPath();
			        ctx.rect(tileX[i], tileY[i], tileSize, tileSize);
			        ctx.fillStyle = cWall;
			       	ctx.fill();
			        ctx.closePath();

			    	};

		        };

		    //Now we can draw obj_player
			ctx.beginPath();
			ctx.rect(obj_player.x - obj_player.size, obj_player.y - obj_player.size, (obj_player.size * 2), (obj_player.size * 2));
			//ctx.arc(obj_player.x, obj_player.y, obj_player.size, 0, 2 * Math.PI, false);
			ctx.fillStyle = cPlayer;
			ctx.lineWidth = 0;
			ctx.fill();
			ctx.closePath();

			};

		//Finally we're going to start updating the canvas at our set interval of FPS
		setInterval(function() {
			update();
			render();
			}, 1000 / FPS);

		};
              
            
!
999px

Console