Código de JavaScript - Juego de la serpiente canvas(beta)

Requerimientos

Navegador de Internet que soporte canvas

Si es de tu ayuda y quieres aprender mas visita:
https://www.youtube.com/channel/UCWSKQyNDhvBVrddHIhzx5Zg?view_as=subscriber


https://www.hmlearning.com/

0.5

Publicado el 11 de Marzo del 2018gráfica de visualizaciones de la versión: 0.5
463 visualizaciones desde el 11 de Marzo del 2018

0.8

Actualizado el 22 de Marzo del 2018 (Publicado el 14 de Marzo del 2018)gráfica de visualizaciones de la versión: 0.8
1.867 visualizaciones desde el 14 de Marzo del 2018
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

se puede poner pause y ya come y tiene vidas :D

descarga
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<style>
canvas{
	border:1px solid #000;
	background:#09F;
}
</style>
<script>
class Cubo{
	constructor(x,y,width,height,color){
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.color = color;
	}
	cordenadas(){
		return this.x;
	}
}
//clase herede de otra Esto es una SubClase
// la clase personajes extends cubo
 
class Personaje extends Cubo{
	constructor(x,y,width,height,color,vidas,dano,puntos){
		super(x,y,width,height,color);//de la extension los qie necesites
		this.vidas = vidas;
		this.dano = dano;
	}
}
 
var c,ctx,direccion,pause,comida,tiempo,puntos,enemigo,personaje;
 
window.onload = function(){
	c = document.getElementById("micanvas");
	ctx = c.getContext("2d");
	puntos = 0;
 
	personaje = new Personaje(0,0,10,10,"#0000ff",3,2);
/*
	let enemigos.push({
		x: 0,
		y: 0,
		width: 0,
		height: 0,
		color: "#f4f4f4",
		vidas: 5
	});
	// Es como enemigos[0] etc aray de objetos
*/
	let comida =
	new Cubo(
		round(Math.round(Math.random()*c.width)),
		round(Math.round(Math.random()*c.height)),
		10,
		10,
		"#00ff00"
	);
 
	let enemigo = new Cubo(
		round(Math.round(Math.random()*c.width)),
		round(Math.round(Math.random()*c.height)),
		10,
		10,
		"#ff0000"
	);
 
	function round(value) {
	    return value - (value % 10);
	}
	function actualizar(){
		ctx.clearRect(0,0,c.width,c.height);
 
		//Comida de serpiente
		ctx.fillStyle=comida.color;
		ctx.lineWidth=2;
		ctx.strokeRect(comida.x,comida.y,comida.width,comida.height);
		ctx.fillRect(comida.x,comida.y,comida.width,comida.height);
		ctx.fill();
		ctx.stroke();
 
		//Enemigo
		ctx.fillStyle=enemigo.color;
		ctx.lineWidth=2;enemigo
		ctx.strokeRect(enemigo.x,enemigo.y,enemigo.width,enemigo.height);
		ctx.fillRect(enemigo.x,enemigo.y,enemigo.width,enemigo.height);
		ctx.fill();
		ctx.stroke();
 
		//Serpiente
		ctx.fillStyle=personaje.color;
		ctx.lineWidth=2;
		ctx.strokeRect(personaje.x,personaje.y,personaje.width,personaje.height);
		ctx.fillRect(personaje.x,personaje.y,personaje.width,personaje.height);
		ctx.fill();
		ctx.stroke();
 
		//Puntaje
		ctx.font="14px Arial";
		ctx.textAlign = 'right';
		ctx.fillStyle="#000000";
		ctx.fillText("Puntaje: " + puntos ,c.width,15);
		ctx.fill();
 
		//Puntaje
		ctx.font="14px Arial";
		ctx.textAlign = 'left';
		ctx.fillStyle="#000000";
		ctx.fillText("Vidas: " + personaje.vidas ,0,15);
		ctx.fill();
		//Texto Pause
		if(pause && personaje.vidas != 0){
			ctx.font="12px Arial";
			ctx.fillStyle="#000000";
			ctx.textAlign="center";
			ctx.fillText("Pause",c.width/2,c.height/2+14);
			ctx.fill();
		}
		//Comida roja -1 vida
		if (personaje.vidas <= 0) {
			ctx.font="14px Arial";
			ctx.textAlign = 'center';
			ctx.fillStyle="#000000";
			ctx.fillText("Fin del Juego",c.width/2,c.height/2);
			ctx.fill();
		}
	}
	window.onkeydown = function(event){
		switch (event.keyCode){
			case 37:
				direccion="izquierda";
			break;
			case 38:
				direccion = "arriba";
			break;
			case 39:
				direccion="derecha";
			break;
			case 40:
				direccion = "abajo";
			break;
			case 13:
			(pause)?pause=false:pause=true;
			break;
		}
	}
	function rango(){
		if(personaje.x + personaje.width > c.width){
			personaje.x = 0;
		}
		if(personaje.y + personaje.height > c.height){
			personaje.y = 0;
		}
		if (personaje.x <= -1) {
			personaje.x = c.width - personaje.width;
		}
		if (personaje.y < -1) {
			personaje.y = c.height - personaje.height;
		}
	}
	function colision(obj1,obj2,tipo){
		if (obj1.x + obj1.width > obj2.x &&
			obj1.x < obj2.x + obj2.width &&
			obj1.y < obj2.y + obj2.height &&
			obj1.y + obj1.height > obj2.y &&
			obj1.x==obj2.x &&
			obj1.y==obj2.y){
		switch (tipo) {
			case "bueno":
				puntos += 1;
				comida.x = round(Math.round(Math.random()*c.width));
		   		comida.y = round(Math.round(Math.random()*c.height));
			break;
			case "malo":
				enemigo.x = round(Math.round(Math.random()*c.width));
		   		enemigo.y = round(Math.round(Math.random()*c.height));
				personaje.vidas -= 1;
			break;
		}
	}
	}
	setInterval(tiempo,200);
	function tiempo(){
		actualizar();
		if(!pause && personaje.vidas >= 1){
			colision(personaje,comida,"bueno");
			colision(personaje,enemigo,"malo");
				switch (direccion){
					case "izquierda":
						personaje.x-=10;
					break;
					case "arriba":
						personaje.y-=10;
					break;
					case "derecha":
						personaje.x+=10;
					break;
					case "abajo":
						personaje.y+=10;
					break;
				}
			rango();
		}
		else if(pause) {
			clearInterval(tiempo);
		}
	}
	tiempo();
}
</script>
<canvas id="micanvas" width="300" height="300"></canvas>



Comentarios sobre la versión: 0.8 (0)


No hay comentarios
 

Comentar la versión: 0.8

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s4479