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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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" background-color="#000"></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 player = {
	x: 100,
	y: 100,
	width: 10,
	height: 20,
	color: "#e74c3c",
	speed:5,
	jSpeed: 2
};
var isLeft = false,
		isRight = false,
		isOnGround = false;

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

var FPS = 1000 / 30;

//----------- ONLOAD -------------
window.onload = function () {
	canvas = document.getElementById("canvas");
	ctx = canvas.getContext("2d");

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

	canvas.style.backgroundColor = "#262626";

	//Monta a plataforma
	platform.push({
		x: 0,
		y: h - pSize,
		width: w,
		height: pSize,
		color: pColor
	}); //piso

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


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

function keyUp(event) {
	switch (event.keyCode) {
		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;
	}
	
	player.y += player.jSpeed;

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

function draw() {
	ctx.clearRect(0, 0, w, h);
	for (var i = 0; i < platform.length; i++) {
									drawRectangle(platform[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 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
Loading ..................

Console