123

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<html>
	<head>
		<meta charset="utf-8"/>
		<title>Juego prueba canvas 1</title>

		<style type="text/css">
			
			#titulo{

				color: blue;

			}

			#cuadroJuego{

				border: solid;
				border-color: blue;
				background-color: white;
			}


			#startGame{

				border: solid;

			}

			#canvasjuego{

					background-color: black;
				
			}

		</style>

	</head>
		<body onload="funcionBackground()">

			
				<h1 id="titulo">juego prueba v0.1</h1>
					<div id="cuadroJuego">
						
						<center>
						<canvas id='canvasjuego' width='800' height='400'></canvas>

						</center>

					</div>
						
					

						<center>
						<br>
						<br>
							<button id="startGame" onclick="botonJugar()">Jugar!!</button>
							<button id="restarVidas" onclick="pruebaVidas()">Restar vidas</button>
						</center>

					<script src='jsjuego.js'></script>
		
		</body>
</html>
            
          
!
            
              

//OBJETOS INDISPENSABLES PARA EL FUNCIONAMIENTO DE CANVAS
var canvas = document.getElementById('canvasjuego');
ctx = canvas.getContext("2d");


//VARIABLES
var background;
var backgroundAnchura;
var backgroundAltura;
var vidas = 3;
var lifes = 'VIDAS: 3';
var ramdonPosX1=500;
var ramdonPosY1=80;
var contador = 3;		
var disparoscontador=0;		

//CLASES
	var canonJson={
		//atributos del canon
		posX: 	400,
		posY: 	40,
		width: 	50,
		height: 50
	}
	var enemigoJson={
		//atributos del enemigo
		posX: 	40,
		posY: 	0,
		width: 	15,
		height: 15,
		velocidad: 1
	}
	var enemigoJson1={
		//atributos del enemigo
		posX: 	58, 
		posY: 	0,
		width: 	15,
		height: 15,
		velocidad: 2
	}
	var enemigoJson2={
		//atributos del enemigo
		posX: 	65,
		posY: 	0,
		width: 	15,
		height: 15,
		velocidad: 3
	}
	var enemigoJson3={
		//atributos del enemigo
		posX: 	75,
		posY: 	0,
		width: 	15,
		height: 15,
		velocidad: 3

	}
	var enemigos= new Array(4);
	var numeroEnemigos=4;
	var i;
		for(i=0;i<enemigos.length;i++){
			if(i==0){
				enemigos[i] = enemigoJson;
			} else if (i==1) {
				enemigos[i] = enemigoJson1;
			} else if (i==2){
				enemigos[i] = enemigoJson2;
			} else if (i==3){
				enemigos[i] = enemigoJson3;
			}
		}

	var arrayDisparos=[10];
	var disparosJson={
		//atributos de la bala en movimiento
		posX: canonJson.posX+15,
		posY: 320,
		width: 15,
		height: 15,
		velocidad: 2
	}
	var evento={}

		//********FUNCIONES QUE SE EJECUTARAN EN EL SCRIPT DIRECTAMENTE*********//



				////////////FUNCION PARA AGREGAR BACKGROUND Y TEXT DE VIDAS//////////
		function funcionBackground(){

			ctx.fillStyle='black';
			ctx.fillRect(0,0,800,400);

				/*The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).
				The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.*/
				intervaloCanon = window.setInterval(rutinaJuego,1000/55);
		}

					//////////EVENTOS DE TECLADO///////////
		function keyboardEvents(){

			agregarEventos(document,"keydown", function(e){
				evento[e.keyCode] = true; //tecla presionada en TRUE

				//alert(e.keyCode);

			});

			agregarEventos(document,"keyup", function(e){
				evento[e.keyCode] = false; //tecla que dejo de ser presionada en false
			});
		}
		function agregarEventos(elementoDelEvento, nombreEvento, funcionAEjecutar){
			if(elementoDelEvento.addEventListener){
				elementoDelEvento.addEventListener(nombreEvento, funcionAEjecutar, false);
			}
		}


				///////FUNCION DE MOVIMIENTO DEL CANON EN EL EJE X////////
		function canon(){
				
			if(evento[37]){
				
				canonJson.posX-=4;
				
				if(canonJson.posX<0){
					canonJson.posX=10; //para que no se salga del canvas
				}
			}
			if(evento[39]){ //movimiento a la derecha	
				canonJson.posX+=4;
				if(canonJson.posX>750){
					canonJson.posX=740;
				}
			}
			if(evento[32]){
				dibujarDisparos();
			}

					ctx.clearRect(canonJson.width-50,canvas.height-50,canvas.width,canvas.height);
					//clear rect de arriba, borra el tamano exacto del rectangulo canonJson, el fondo se rellena
					//con CSS a canvas
					ctx.fillStyle='red';
					ctx.fillRect(canonJson.posX,350,canonJson.width,canonJson.height);			
		}					////////////FIN FUNCION CANON////////

		function dibujandoEnemigos(numeroEnemigos){
			for(var i=0;i<numeroEnemigos;i++){
				ctx.fillStyle='yellow';
				ctx.fillRect(enemigos[i].posX,enemigos[i].posY,enemigos[i].width,enemigos[i].height);
			}	
		}

		function moverEnemigos(){

			for(var i=0;i<numeroEnemigos;i++){
				ctx.clearRect(enemigos[i].posX,enemigos[i].posY,enemigos[i].width,enemigos[i].height);
				enemigos[i].posX+=enemigos[i].velocidad; //va a moverse con la operacion: + n veces sea declarada la variable velocidad
			}

			for(var j=0;j<numeroEnemigos;j++){
				if(enemigos[j].posX>canvas.width){
					enemigos[j].posX=0;
					enemigos[j].posY+=5;
				}
				if(enemigos[j].posY>canvas.height){s
					enemigos[j].posX=0;
					enemigos[j].posY=10;
				}
			}
		}
		//API FIREFOX
		function detecciondeColisiones(a,b){
			var colision = false;

			if(b.x + b.width >= a.x && b.x <a.x+a.width){
				if(b.y+b.height>=a.y && b.y<a.y+a.height){
					colision=true; //
				}
			}
			if(b.x <= a.x && b.x+b.width>=a.x+a.width){
				if(b.y <= a.y && b.y+b.height>= a.y+a.height){
					colision=true;
				}

			}
			if(a.x <= b.x && a.x+a.width>=b.x+b.width){
				if(bay <= b.y && a.y+a.height>= b.y+b.height){
					colision=true;
				}

			}
		}
		
		function dibujarDisparos(){
			ctx.fillStyle='white';
			for(var i=0;i<arrayDisparos.length;i++){
				ctx.fillRect(canonJson.posX+16,disparosJson.posY,disparosJson.width,disparosJson.height);
			}
		}

		function moverDisparos(){
			for(var i=0;i<arrayDisparos.length;i++){
				ctx.clearRect(disparosJson.posX,canonJson.posY,disparosJson.width,disparosJson.height);
				disparosJson.posY-=2;
			}

		}
		function disparar(){
			
			moverDisparos();
			dibujarDisparos();

			
		}
		function rutinaJuego(){

			moverEnemigos();
			canon();
			dibujandoEnemigos(numeroEnemigos);

			
		}

		
		function botonJugar(){
			ctx.fillStyle='black';
			ctx.fillText(lifes,20,20);
			ctx.fillStyle='white';
			ctx.font="20px Arial";
			ctx.fillText("VIDAS: "+vidas,20,20);
		}
		function pruebaVidas(){


			vidas--;

			//prueba tambien de avion enemigo\
			ctx.clearRect(ramdonPosX1,ramdonPosY1,10,10);
			ramdonPosX1=ramdonPosX1-20;
			ramdonPosY1=ramdonPosY1+60;
			
			//fin prueba avion enemigo
			if(vidas==2){
				ctx.fillStyle='black';
				ctx.fillText(lifes,20,20);
				ctx.fillStyle='white';
				ctx.font="20px Arial";
				ctx.fillText("VIDAS: "+vidas,20,20);
			} else if (vidas==1){
				ctx.fillStyle='black';
				ctx.fillText(lifes,20,20);
				ctx.fillStyle='white';
				ctx.font="20px Arial";
				ctx.fillText("VIDAS: "+vidas,20,20);

			} else if (vidas==0){
				ctx.fillStyle='black';
				ctx.fillText(lifes,20,20);
				ctx.fillStyle='white';
				ctx.font="20px Arial";
				ctx.fillText("HAZ PERDIDO D':",20,20);
			}
	}



//FUNCIONES QUE SE EJECUTAN CONTINUAMENTE (ya que tienen aplicado el window listener y la funcion onLoad)

		keyboardEvents();
		funcionBackground();
		//aviones();
		//canon();

            
          
!
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.

Console