-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminigame.html
141 lines (140 loc) · 3.59 KB
/
minigame.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
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Space minigame</title>
</head>
<body>
</body>
<script>
var canvas=document.createElement("canvas");
canvas.height=window.innerHeight-40
canvas.width=window.outerWidth-20
var c = canvas.getContext("2d");
var w=canvas.width
var h=canvas.height
var perder=false
var left = false;
var right = false;
var timeStep = 50; // In milliseconds
var aceleration=0.5
var velocity=0
var asteroids=[];
var player;
class nave{
constructor(size,x,y) {
this.height = size;
this.width = size;
this.x=x;
this.y=y-size;
}
stroke(){
c.strokeRect(this.x, this.y, this.width, this.height);
c.beginPath();
c.moveTo(this.x,this.y);
c.lineTo(this.x+this.width/2,this.y-this.height/2);
c.lineTo(this.x+this.width,this.y);
c.closePath();
c.stroke();
}
move(x){
let sum=this.x+x
if(sum>0 && sum + this.width <w)this.x=sum
}
}
class asteroid{
constructor(r,x,y) {
this.r=r;
this.x=x;
this.y=y;
}
stroke(){
c.beginPath();
c.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
c.stroke();
}
move(y){
this.y+=y
return this.y>h
}
}
function getRandom(min, max) {
return Math.random() * (max - min) + min;
}
function colision(player, asteroid) {
var dx = player.x + player.width/2 - asteroid.x;
var dy = player.y + player.height/2 - asteroid.y;
var distance = Math.sqrt(dx * dx + dy * dy);
return (distance < asteroid.r + player.width/3)
}
function restart() {
player = new nave(40,canvas.width/2,canvas.height-50)
velocity=0
asteroids=[];
for(let i=0;i<10;i++){
var size=getRandom(20,60)
asteroids[i] = new asteroid(size,getRandom(0+size,w-size),getRandom(size-h,-size))
}
}
window.onkeydown = function(e) {
if (e.keyCode == 32 || e.keyCode == 37 || e.keyCode == 39) {
e.preventDefault();
switch(e.keyCode){
case 32: //espacio
restart()
perder=false
break;
case 37: //left
left=true
break;
case 39: //right
right=true
break;
}
}
};
window.onkeyup = function(e) {
if (e.keyCode == 37 || e.keyCode == 39) {
e.preventDefault();
switch(e.keyCode){
case 37: //left
left=false
break;
case 39: //right
right=false
break;
}
}
};
document.body.innerHTML = "";
document.body.append(canvas)
player = new nave(40,canvas.width/2,canvas.height-50)
for(let i=0;i<10;i++){
var size=getRandom(20,60)
asteroids[i] = new asteroid(size,getRandom(0+size,w-size),getRandom(size-h,-size))
}
function updateAll() {
if(!perder){
c.clearRect(0, 0, w, h);
c.font = "20px Arial";
c.fillText(""+velocity*2, 10, 20);
if(left)player.move(-20)
if(right)player.move(20)
player.stroke()
for(let i=0;i<10;i++){
if(asteroids[i].move(velocity)){
var size=getRandom(20,60)
asteroids[i] = new asteroid(size,getRandom(0+size,w-size),getRandom(-size,size))
}
if(colision(player,asteroids[i]))perder=true
asteroids[i].stroke();
}
if(perder){
c.fillText("Press Space To Restart", w/2-20, h/2);
}
velocity+=aceleration;
}
}
setInterval(updateAll, timeStep);
</script>
</html>