-
Notifications
You must be signed in to change notification settings - Fork 0
/
\
executable file
·101 lines (98 loc) · 2.63 KB
/
\
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
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
imgBall=new Image();
imgBall.src="images/ball.png";
imgBall.addEventListener("load",init,false);
var nBall=[];
var delArr=[];
var gameover=false;
var timer=setInterval(nBallAdder,3000);
function getRandomBallYValue(){
return Math.floor(Math.random()*310)+50;
}
function nBallAdder(){
var rndy=getRandomBallYValue()
nBall.push([700,rndy,false])
}
function ballMover(){
for(j=0;j<nBall.length;j++){
if(nBall[j][2]==false){
nBall[j][0]-=2;
nBall[j][1]+=2;
}
if(nBall[j][2]==true){
nBall[j][0]-=2;
nBall[j][1]-=2;
}
if(nBall[j][2]=="l"){
nBall[j][0]+=2;
nBall[j][1]+=2;
}
if(nBall[j][2]=="r"){
nBall[j][0]-=4;
nBall[j][1]+=2;
}
}
}
var requestAnimFrame=window.requestAnimationFrame||
window.webkitRequestAnimationFrame||
window.mozRequestAnimationFrame||
window.oRequestAnimationFrame||
window.msRequestAnimationFrame||
function(callback){
window.setTimeout(callback,1000/60);
};
var plateValues={
plateX:75,
plateY:500,
}
function init(){
requestAnimFrame(update);
}
context.fillRect(750,10,10,640,'#CEF19E');
function update(){
window.addEventListener("keydown",keypressed,false);
context.clearRect(0,0,750,650);
ballMover();
for(i=0;i<nBall.length;i++){
context.drawImage(imgBall,nBall[i][0],nBall[i][1],40,40);
if((plateValues.plateX+150)>nBall[i][0]&&(plateValues.plateX+140)<nBall[i][0]&&(nBall[i][1]+40)>plateValues.plateY&&nBall[i][1]<(plateValues.plateY+20)){
nBall[i][2]="l";
}
if(plateValues.plateX<(nBall[i][0]+40)&&(plateValues.plateX+10)>(nBall[i][0]+40)&&(nBall[i][1]+40)>plateValues.plateY&&nBall[i][1]<(plateValues.plateY+20)){
nBall[i][2]="r";
}
if((nBall[i][1]+40)>plateValues.plateY&&(nBall[i][1]+40)<(plateValues.plateY+20)){
if((nBall[i][0]+40)>plateValues.plateX&&nBall[i][0]<(plateValues.plateX+150)){
nBall[i][2]=true;
}
}
if(nBall[i][0]<=10||nBall[i][1]<=10){
delArr.push(i);
}
if((nBall[i][1]+40)>600||(nBall[i][2]==true&&(nBall[i][0]+40)>700)){
gameover=true;
}
}
for(k=0;k<delArr.length;k++){
nBall.shift(delArr[k]);
}
delArr=[];
if(gameover==true){
clearInterval(timer);
}
context.fillRect(plateValues.plateX,plateValues.plateY,150,20,"#000000");
context.fillRect(10,10,40,630,"#000000");
context.fillRect(10,10,730,40,"#000000");
context.fillRect(700,10,40,630,"#000000");
context.fillRect(10,600,730,40,"#000000");
requestAnimFrame(update);
}
function keypressed(e){
if(e.keyCode==37&&plateValues.plateX>75){
plateValues.plateX-=5;
}
if(e.keyCode==39&&plateValues.plateX<525){
plateValues.plateX+=5;
}
}