cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <canvas id="canvas" width="500px" height="300px"></canvas>
            
          
!
            
              html, body{
    width: 100%;
    height: 100%;
    overflow:hidden;
}

body{
    background-color: #333333;
    display: flex;
    justify-content: center;
    align-items: center;
}

canvas{
    background-color: #fff;
}
            
          
!
            
              var canvas, ctx, w, h;

var pColor = "#95a5a6";
var pSize = 20;
var platform = [];

var iColor = "#ffdb49";
var items = [];
var iSize = 10;

var maxJump = 15;
var player = {
	x: 100,
	y: 100,
	width: 10,
	height: 20,
	color: "#e74c3c",
	speed:5,
	jSpeed: maxJump
};
var isLeft = false,
		isRight = false,
		isSpace = false,
		isOnGround = false,
		isJumping = false;

var FPS = 1000 / 30;

//----------- ONLOAD -------------
window.onload = function () {
	loadFullGame();
}

function loadFullGame() {
	canvas = document.getElementById("canvas");
	ctx = canvas.getContext("2d");

	w = canvas.offsetWidth;
	h = canvas.offsetHeight;

	canvas.style.backgroundColor = "#262626";

	player.y = h - pSize - player.height;
	player.x = 10;

	//Monta a plataforma
	platform.push({
		x: 0,
		y: h - pSize,
		width: w,
		height: pSize,
		color: pColor
	}); //piso
	platform.push({
		x: w - 3 * w / 10,
		y: h - 3 * pSize,
		width: w / 10,
		height: pSize,
		color: pColor
	});
	platform.push({
		x: w - w / 10,
		y: h - 6 * pSize,
		width: w / 10,
		height: pSize,
		color: pColor
	});
	platform.push({
		x: w / 10,
		y: h - 8 * pSize,
		width: 7 * w / 10,
		height: pSize,
		color: pColor
	});
	platform.push({
		x: 0,
		y: 4 * pSize,
		width: w / 10,
		height: pSize,
		color: pColor
	});

	//Itens coletáveis
	var yy = h - 10 * pSize;
	var xx = 2 * w / 10;
	for (i = 0; i < 8; i++) {
		items.push({
			x: xx,
			y: yy,
			width: iSize,
			height: iSize,
			color: iColor
		});
		xx += 3 * iSize;
	}

	setInterval(gameloop, FPS);
	window.addEventListener("keydown", keyDown, true);
	window.addEventListener("keyup", keyUp, true);
}

//----------- TECLADO -------------
function keyDown(event) {
	switch (event.keyCode) {
		case 32:
		case 16:
			isSpace = true;
			break;
		case 37:
			isLeft = true;
			break;
		case 39:
			isRight = true;
			break;
	}
}

function keyUp(event) {
	switch (event.keyCode) {
		case 32:
		case 16:
			isSpace = false;
			break;
		case 37:
			isLeft = false;
			break;
		case 39:
			isRight = false;
			break;
	}
}

//----------- GAMELOOP -------------
function gameloop() {
	update();
	draw();
}


function update() {
	//Movimento horizontal
	if (isRight) {
		if (player.x + player.speed < w - player.width) player.x += player.speed;
		else player.x = w - player.width;
	}
	if (isLeft) {
		if (player.x - player.speed > 0) player.x -= player.speed;
		else player.x = 0;
	}
	if (isSpace || isJumping) {
		jump();
	}

	for (var i = 0; i < platform.length; i++) {

		isOnGround = false;
		if (detectBaseCollision(player, platform[i])) {
			isOnGround = true;
			break;
		}
	}

	if (!isOnGround) {
		isJumping = true;
	}

	//Coleta um item
	for (i = 0; i < items.length; i++) {
		if (detectCollision(player, items[i])) items.splice(i, 1);
	}


}

function jump() {
	if (!isJumping) {
		isJumping = true;
		player.jSpeed = maxJump * -1;
		player.y += player.jSpeed;
	} else {
		if (player.jSpeed < 0) {
			player.jSpeed *= 1 - maxJump / 75;
			if (player.jSpeed > -maxJump / 5) {
				player.jSpeed *= -1;
			}
		}
		if (player.jSpeed > 0 && player.jSpeed <= maxJump) {
			player.jSpeed *= 1 + maxJump / 50;
		}
		player.y += player.jSpeed;

		for (var i = 0; i < platform.length; i++) {
			if (detectBaseCollision(player, platform[i])) {
				isOnGround = false;
				isJumping = false;
				player.y = platform[i].y - player.height;
				break;
			} else {
				isOnGround = true;
			}
		}
	}
}

function draw() {
	ctx.clearRect(0, 0, w, h);

	for (var i = 0; i < platform.length; i++) {
		drawRectangle(platform[i]);
	}

	for (i = 0; i < items.length; i++) {
		drawRectangle(items[i]);
	}

	drawRectangle(player);
}

//----------- Desenha quadrado -------------
function drawRectangle(obj) {
	ctx.beginPath();
	ctx.rect(obj.x, obj.y, obj.width, obj.height);
	ctx.fillStyle = obj.color;
	ctx.fill();
}


//----------- Detecta Colisão -------------
function detectCollision(obj1, obj2) {
	var vertical = false;
	var horizontal = false;

	if ((obj1.y >= obj2.y && obj1.y <= obj2.y + obj2.height) ||
			(obj1.y + obj1.height >= obj2.y && obj1.y + obj1.height <= obj2.y + obj2.height))
		vertical = true;

	if ((obj1.x >= obj2.x && obj1.x <= obj2.x + obj2.width) ||
			(obj1.x + obj1.width >= obj2.x && obj1.x + obj1.width <= obj2.x + obj2.width))
		horizontal = true;

	return vertical && horizontal;
}

//----------- Detecta Colisão apenas da Base -------------
function detectBaseCollision(obj1, obj2) {
	var vertical = false;
	var horizontal = false;

	if (obj1.y + obj1.height >= obj2.y && obj1.y + obj1.height <= obj2.y + obj2.height)
		vertical = true;

	if ((obj1.x >= obj2.x && obj1.x <= obj2.x + obj2.width) ||
			(obj1.x + obj1.width >= obj2.x && obj1.x + obj1.width <= obj2.x + obj2.width))
		horizontal = true;

	return vertical && horizontal;
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console