-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
155 lines (155 loc) · 5.42 KB
/
index.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!doctype HTML>
<html lang="pl">
<head>
<title>Breakout</title>
<meta charset="utf-8">
</head>
<body style="background-color:black;text-align:center;margin:0px">
<div id="pole" style="border-style:solid;width:400px;height:500px">
<div id="monit">
Press Spacebar to begin
</div>
<div id="wynik">
Score: 0
</div>
</div>
<script>
var xmax = 600;
var ymax = 700;
var blokix = 8;
var blokiy = 5;
var wynik = 0;
var color = "rgb(255,0,255)";
var colormeobjects = document.getElementsByClassName("colorme");
for(var i=0;i<colormeobjects.length;i++){
colormeobjects[0].style.backgroundColor=color;
colormeobjects[0].style.color=color;
}
document.getElementById("pole").style.borderColor=color;
document.getElementById("pole").style.borderBottomColor="red";
document.getElementById("monit").style.color=color;
document.getElementById("wynik").style.color=color;
blokitotal=blokix*blokiy;
var make_blok = function(xp,yp,xwp,ywp,idp,clp){
var blok = document.createElement("div");
blok.style.position="absolute";
blok.style.left=xp+"px";
blok.style.top=yp+"px";
blok.style.width=xwp+"px";
blok.style.height=ywp+"px";
blok.style.backgroundColor=color;
blok.id=idp;
blok.class=clp;
return(blok);
}
var pole = document.getElementById("pole");
pole.style.width=xmax-3+"px";
pole.style.height=ymax+"px"
var plansza = {player:pole.appendChild(make_blok(xmax/2,ymax*4/5,60,10,"player")),ball:pole.appendChild(make_blok(xmax/2,ymax*4/5-30,10,10))}
var wzormaker = function(w,h){
wzortu=[];
for(var i=0;i<h;i++){
for(var j=0;j<w;j++){
wzortu[i*w+j]=pole.appendChild(make_blok(xmax/w*j,xmax/w/3*2*(i+3),xmax/w,xmax/w/3*2,"blok_"+(i*w+j),"brick"))
}
}
plansza.wzor=wzortu;
}
wzormaker(blokix,blokiy);
var lpressed = false;
var rpressed = false;
var trackeron = function(event){
if(event.key=="ArrowLeft"){lpressed=true}
if(event.key=="ArrowRight"){rpressed=true}
}
var trackerup = function(event){
if(event.key=="ArrowLeft"){lpressed=false}
if(event.key=="ArrowRight"){rpressed=false}
}
document.addEventListener("keydown",trackeron)
document.addEventListener("keyup",trackerup)
var playermotion = function(){
if(lpressed&&plansza.player.style.left.slice(0,-2)>0){
plansza.player.style.left=(parseFloat(plansza.player.style.left.slice(0,-2))-2)+"px";
}
if(rpressed&&plansza.player.style.left.slice(0,-2)<xmax-60){
plansza.player.style.left=(parseFloat(plansza.player.style.left.slice(0,-2))+2)+"px";
}
}
var ballvx = 0;
var ballvy = 1;
var ballmotion = function(){
plansza.ball.style.left=(parseFloat(plansza.ball.style.left.slice(0,-2))+ballvx)+"px";
plansza.ball.style.top=(parseFloat(plansza.ball.style.top.slice(0,-2))+ballvy)+"px";
}
var calcmid = function(rect){
var x = parseFloat(rect.style.left.slice(0,-2))+parseFloat(rect.style.width.slice(0,-2))/2;
var y = parseFloat(rect.style.top.slice(0,-2))+parseFloat(rect.style.height.slice(0,-2))/2;
return([x,y]);
}
var ballcol = function(rect1,rect2){
if(parseFloat(rect1.style.left.slice(0,-2)) < parseFloat(rect2.style.left.slice(0,-2)) + parseFloat(rect2.style.width.slice(0,-2))&&
parseFloat(rect1.style.left.slice(0,-2)) + parseFloat(rect1.style.width.slice(0,-2)) > parseFloat(rect2.style.left.slice(0,-2))&&
parseFloat(rect1.style.top.slice(0,-2)) < parseFloat(rect2.style.top.slice(0,-2)) + parseFloat(rect2.style.height.slice(0,-2))&&
parseFloat(rect1.style.top.slice(0,-2)) + parseFloat(rect1.style.height.slice(0,-2)) > parseFloat(rect2.style.top.slice(0,-2))){
var xy1 = calcmid(rect1);
var xy2 = calcmid(rect2);
var x1 = xy1[0];
var y1 = xy1[1];
var x2 = xy2[0];
var y2 = xy2[1];
var a1 = x1-x2;
var b1 = y1-y2;
var c = Math.sqrt(a1*a1+b1*b1);
var a2 = a1/c;
var b2 = b1/c;
ballvx=a2;
ballvy=b2;
if(rect2.class=="brick"){
rect2.style.display="none";
plansza.wzor[rect2.id.slice(5)]=null;
blokitotal--;
wynik+=100;
document.getElementById("wynik").textContent="Score: "+wynik;
}
}
}
var ballcollide = function(){
if(plansza.ball.style.left.slice(0,-2)<=0||parseFloat(plansza.ball.style.left.slice(0,-2))+10>=xmax){
ballvx*=-1;
}
if(plansza.ball.style.top.slice(0,-2)<=0||parseFloat(plansza.ball.style.top.slice(0,-2))+10>=ymax){
ballvy*=-1;
}
ballcol(plansza.ball,plansza.player);
for (blo in plansza.wzor){
if(plansza.wzor[blo]!=null){
ballcol(plansza.ball,plansza.wzor[blo]);
}
}
}
var update = function(){
if(blokitotal==0){
clearInterval(tick);
document.getElementById("monit").textContent="You WON!";
document.getElementById("wynik").textContent="Your score: "+wynik+" Press F5 to restart"
}
if(plansza.ball.style.top.slice(0,-2)>ymax-30){
clearInterval(tick);
document.getElementById("monit").textContent="GAME OVER";
document.getElementById("wynik").textContent="Your score: "+wynik+" Press F5 to restart"
}
playermotion();
ballcollide();
ballmotion();
}
var tick;
var start = function(){
tick = setInterval(update,3);
document.removeEventListener("keypress",start);
document.getElementById("monit").textContent="";
}
document.addEventListener("keypress",start);
</script>
</body>
</html>