Pen Settings

HTML

CSS

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

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

              
                		<canvas id="myCanvas" width="480" height="320"></canvas>
		<div id="mensajes"> <p>Pulsa cualquier tecla para empezar o<p><button onclick="keyPressed()"> Click aqui </button></div>
		

<script src="script.js"></script>


              
            
!

CSS

              
                *{
	padding: 0;
	margin: 0;
	font-family: 'Press Start 2P', cursive;
}
#myCanvas{
	background-color: #eee;
	display: block;
	margin: 0 auto;
}
#mensajes{

	text-align: center;
	padding: 10px;
}
#mensajes p{
	color: black;
	font-size: 12px;
}
#myCanvas{
  width: 480px;
  margin: 0 auto;
  display: block;
  border: 1px solid #d9d9d9;
}
@media  (max-width: 480px) {
#myCanvas{
  width: 100%;
}
}

              
            
!

JS

              
                function empezar(pista,vidas,puntos){
clearInterval(res);
document.removeEventListener("keydown", keyPressed)
var mensaje = document.getElementById('mensajes');
mensaje.innerHTML="";
var canvas= document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var x = canvas.width/2;
var y = canvas.height - 30;
//bola
console.log(pista);
var dx=2+pista;
var dy=-2-pista;
var ballRadius = 6;
//raqueta
var paddleHeight = 8;
var paddleWidth = 60;
var paddleX = (canvas.width- paddleWidth)/2;
var rightPressed = false;
var leftPressed = false;
var vel=7;
//ladrillos
var brickRowCount = 4;
var brickColumnCount = 8;
var brickWidth = 45;
var brickHeight = 15;
var brickPadding = 8;
var brickOffsetTop = 30;
var brickOffsetLeft = 32;
var totalPoints =0;
var bricks =[];
for (c=0; c<brickColumnCount; c++){
	bricks[c] =[];
	var brickX = (c*(brickWidth+brickPadding)+brickOffsetLeft);
	for (r=0; r<brickRowCount; r++){
		var hard = Math.floor(Math.random()*3)+1;
		var brickY = (r*(brickHeight+brickPadding)+brickOffsetTop);
		bricks[c][r] ={x: brickX, y: brickY, status: hard, locked: 0};
		totalPoints += hard*10;
	}
}
//puntuacion
var score = puntos;
var lives = vidas;
//logica raqueta
document.addEventListener("keydown", keyDownHandler);
document.addEventListener("keyup", keyUpHandler);
function keyDownHandler(e){
	if(e.keyCode == 39){
		rightPressed = true;
	}else if(e.keyCode == 37){
		leftPressed=  true;
	}
}
function keyUpHandler(e){
	if(e.keyCode == 39){
		rightPressed = false;
	}else if(e.keyCode == 37){
		leftPressed=  false;
	}
}
function drawBricks(){
	var bColor="#aa95dd";
	for(c=0; c<brickColumnCount; c++){
		for (r=0; r<brickRowCount; r++){
			if(bricks[c][r].status > 0){
				ctx.beginPath();
				ctx.rect(bricks[c][r].x, bricks[c][r].y, brickWidth, brickHeight);
				switch (bricks[c][r].status) {
					case 3:
						bColor="#222222";
						break;
					case 2:
						bColor="#AA0033";
						break;
					default:
						bColor="#bb95dd";
						break;
				}
				ctx.fillStyle = bColor;
				ctx.fill();
				ctx.closePath();
			}
			
		}
	}
}
function drawBall(){
	ctx.beginPath();
	ctx.arc(x,y,ballRadius,0, Math.PI*2);
	ctx.fillStyle = "#ff0033";
	ctx.fill();
	ctx.closePath();
}
function drawPaddle(){
	ctx.beginPath();
	ctx.rect(paddleX, canvas.height-paddleHeight, paddleWidth, paddleHeight);
	ctx.fillStyle = "#0095dd";
	ctx.fill();
	ctx.closePath();
}
function recargar(){
	document.location.reload();
}
function unlock(){
	for(c=0; c<brickColumnCount; c++){
		for (r=0; r<brickRowCount; r++){
			var b = bricks[c][r];
			b.locked=0;
		}
	}
}
function collision(){
	for(c=0; c<brickColumnCount; c++){
		for (r=0; r<brickRowCount; r++){
			var b = bricks[c][r];
			if (b.status > 0 && x>b.x && x<b.x+brickWidth && y>b.y && y<b.y+brickHeight) {
				
					dy = -dy;
					
				if(y <= b.y+brickHeight-4 && y> b.y+4) dx = -dx;
				//console.log("ladrillo"+(b.y+brickHeight)+", bola"+y + "ladrilloarriba" + b.y);
				if(b.locked==0){
					b.status--;
					unlock();
					b.locked=1;
					score += 10;
				}
				//alert (b.status);
				
				if (score == totalPoints+puntos){
					ctx.font = '26px "Press Start 2P"';
					ctx.fillStyle = "#0095dd";
					ctx.fillText("YOU WIN",canvas.width/2-90,canvas.height/2);
					document.removeEventListener("keydown", keyDownHandler);
					document.removeEventListener("keyup", keyUpHandler);
					dy=0;
					dx=0;
					setTimeout(function(){
							empezar(pista+1, lives, score);
						}
						, 5000);
				}
			}
		}
	}
}
function drawScore(){
	ctx.font = '13px "Press Start 2P"';
	ctx.fillStyle = "#0095dd";
	ctx.fillText("Puntos: "+ score, 8, 20);
}
function drawFase(){
	ctx.font = '14px "Press Start 2P"';
	ctx.fillStyle = "#0095dd";
	ctx.fillText("FASE: "+ pista, canvas.width/2-50, 20);
}
function drawLives(){
	ctx.font = '13px "Press Start 2P"';
	ctx.fillStyle = "#0095dd";
	ctx.fillText("Vidas: "+ lives, canvas.width-125, 20);
}
function draw(){
	if(dy!=0){
		ctx.clearRect(0,0,canvas.width,canvas.height);
	}
	drawBricks();
	drawBall();
	drawPaddle();
	collision();
	drawScore();
	drawFase();
	drawLives();
	if(y+dy<0+ballRadius){
		dy=-dy;
		unlock();
	}
	if(y+dy>canvas.height-ballRadius){
		unlock();
		if(x>(paddleX + paddleWidth)/2-3 && x<(paddleX + paddleWidth)/2+3 && dy!=1){
			dy--;
			dy=-dy;
		}
		else if(x > paddleX+5 && x< paddleX + paddleWidth-5){
			dy=-dy;
		}else if(x > paddleX-5 && x < paddleX+5 && dx != 1){
			dx--;
			dy=-dy;
		}else if(x > paddleX + paddleWidth-5 && x < paddleX + paddleWidth+5 && dx != -1){
			dx++;
			dy=-dy;
		}
		else{
			lives--;
			if(lives==0){
				ctx.font = '26px "Press Start 2P"';
				ctx.fillText("GAME OVER",canvas.width/2-90,canvas.height/2);
				document.removeEventListener("keydown", keyDownHandler);
				document.removeEventListener("keyup", keyUpHandler);
				dy=0;
				dx=0;
				setTimeout(recargar, 5000);
			}else{
				x= canvas.width/2;
				y= canvas.height-30;
				dx = 2+pista;
				dy = -2-pista;
				paddleX = (canvas.width-paddleWidth)/2;
			}
			
		}
	}
	if(x+dx<0+ballRadius || x+dx>canvas.width-ballRadius){
		dx=-dx;
		unlock();
	}

	if (rightPressed && paddleX<canvas.width-paddleWidth){
		paddleX += 7;
	}else if(leftPressed && paddleX>0){
		paddleX -= 7;
	}

	x += dx;
	y +=dy;
	requestAnimationFrame(draw);
}
document.addEventListener("touchmove", touchMoveHandler);
function touchMoveHandler(e){
	e.preventDefault();
	vel=7;
	var relativeX = e.touches[0].clientX - canvas.offsetLeft;
	if(relativeX > paddleX+paddleWidth){
		var touchMove = setInterval(function(){
			if(paddleX+paddleWidth<canvas.width){paddleX += vel;}
		}, 20);
		
	}
	if(relativeX <paddleX){
		var touchMove = setInterval(function(){
			if(paddleX > 0+paddleWidth/2){paddleX -= vel;}
		}, 20);
	}
}
document.addEventListener("touchend", touchStopHandler);
function touchStopHandler(e){
  vel=0;
//	clearInterval(touchMove);
}
draw();
}
function keyPressed(){
	empezar(1,3,0);
}
function size(){
	var mensaje = document.getElementById("mensajes");
	if(screen.width<480){
		mensaje.innerHTML = 
		"<p>Anchura de pantalla " + screen.width+ "<br> Rota el dispositivo para poder jugar</p>";
	}else{
		mensaje.innerHTML = '<p>Pulsa cualquier tecla para empezar o<p><button onclick="keyPressed()"> Click aqui </button>';
		document.addEventListener("keydown", keyPressed);
	}
}
var res = setInterval(size, 1000);


              
            
!
999px

Console