-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPracticaB10.html
99 lines (86 loc) · 3.33 KB
/
PracticaB10.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PracticaB10</title>
<style>
*{
margin: 0px;
padding: 0px;
}
</style>
<script>
window.onload=function(){
caja.style.width = "800px";
caja.style.height = "600px";
caja.style.backgroundColor = "rgb(0, 0, 0)";
var pelotas = new Array(10);
for( i =0; i < pelotas.length;i++){
pelotas[i]=new Pelota();
pelotas[i].crearBola();
}
}
function Pelota(){
this.width=10;
this.height = 10;
this.left = Math.floor(Math.random() * (parseInt(caja.style.width)-this.width) + 0);
this.top = Math.floor(Math.random() * (parseInt(caja.style.height)-this.height) + 0);
this.avanceTop=5;
this.avanceLeft=5;
this.random=Math.floor(Math.random()*(2))
if(this.random==0){
this.arriba=false;
this.izquierda=false;
}else if(this.random==1){
this.arriba=true;
this.izquierda=true;
}
this.intervalo=setInterval(this.moverBola.bind(this),20);
}
Pelota.prototype.crearBola=function(){
this.bola=document.createElement("div");//Lo que crearemos en pantalla
this.bola.style.width=this.width+"px";
this.bola.style.height=this.height+"px";
this.bola.style.backgroundColor="yellow";
this.bola.style.position="absolute";
this.bola.style.top=this.top+"px";
this.bola.style.left=this.left+"px";
this.bola.style.borderRadius="1em";
caja.appendChild(this.bola);
}
Pelota.prototype.moverBola=function(){
if(this.top>=parseInt(caja.style.height)-parseInt(this.bola.style.height)){
this.top=parseInt(caja.style.height)-parseInt(this.bola.style.height);
this.arriba=false;
}else if(this.top<=0){
this.top=0;
this.arriba=true;
}
if(!this.arriba){
this.top-=this.avanceTop;
this.bola.style.top=this.top+"px";
}else if(this.arriba){
this.top+=this.avanceTop;
this.bola.style.top=this.top+"px";
}
if(this.left>=parseInt(caja.style.width)-parseInt(this.width)){
this.left=parseInt(caja.style.width)-parseInt(this.width);
this.izquierda=false;
}else if(this.left<=0){
this.left=0;
this.izquierda=true;
}
if(!this.izquierda){
this.left-=this.avanceLeft;
this.bola.style.left=this.left+"px";
}else if(this.izquierda){
this.left+=this.avanceLeft;
this.bola.style.left=this.left+"px";
}
}
</script>
</head>
<body>
<div id="caja"></div>
</body>
</html>