-
Notifications
You must be signed in to change notification settings - Fork 277
/
Hacktoberfest2019_Gametrial.html
212 lines (172 loc) · 4.81 KB
/
Hacktoberfest2019_Gametrial.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
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
211
212
<style>
#platform{ border:1px solid white; background-color:black;}
body{background-color:palegreen;text-align:center;}
</style>
<html>
<body>
<canvas id="platform" width=300; height=550;></canvas>
<script>
var canvas =document.getElementById("platform");
var ctx = canvas.getContext("2d");
let midwidth=canvas.width/2;
let l=canvas.height;
let x=l-100;
let t=0;
let left,right;
var angle=0;
var m,n;
var obsHeight=20;
var score=0;
var lEdge=[0,120,120,200,30,50,250];
var obsWidth=[150,200,75,100,175,25,50];
var tEdge=[300,180,50,220,100,550,270];
var dc;
var ds;
var top;
var leftEdge;
var rightEdge;
var bottom;
// Listen for keydown events
window.addEventListener('keydown',keydown,false);
function keydown(event){
if (event.keyCode === 37) { // LEFT
left = true;
//console.log("down");
angle-=5;
dc=(80*Math.cos(angle*Math.PI/180));
ds=(80*Math.sin(angle*Math.PI/180));
setRotate(angle);
Hitout(dc,ds,lEdge,tEdge,obsWidth);
}
else if (event.keyCode === 39) { // RIGHT
right = true;
//console.log("down");
angle+=5;
dc=(80*Math.cos(angle*Math.PI/180));
ds=(80*Math.sin(angle*Math.PI/180));
setRotate(angle);
Hitout(dc,ds,lEdge,tEdge,obsWidth);
}
if(event.keyCode===32){
//console.log("down");
key();
}
}
// Listen for keyup events
window.addEventListener('keyup', keyup,false);
function keyup(event){
//console.log("up");
if (event.keyCode === 37) { // LEFT
left = false;
}
if (event.keyCode === 39) { // RIGHT
right = false;
}
}
function insideX(a, b, c, d, e, f) {
return (a+midwidth > b && a+midwidth < c )&&(d+x > e && d+x < f );
}
function insideY(a, b, c, d, e, f) {
return ( midwidth-a >b && midwidth-a < c)&&( x-d >e && x-d < f);
}
/*function checker(){
}*/
function ball(){
ctx.beginPath();
ctx.arc(midwidth+70,x, 10, 0, Math.PI*2);
ctx.fillStyle = "skyblue";
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.arc(midwidth,x,70,0,Math.PI*2);
ctx.strokeStyle="white";
ctx.lineWidth="0.1";
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.arc(midwidth-70,x, 10, 0, Math.PI*2);
ctx.fillStyle = "red";
ctx.fill();
ctx.closePath();
}
function obstacles(){
for(var o=0;o<3;o++){
for(var i=0;i<7;i++){
ctx.fillStyle="white";
ctx.fillRect(lEdge[i] ,tEdge[i],obsWidth[i],obsHeight);
Hitout(dc,ds,lEdge[i],tEdge[i],obsWidth[i]);
}
}
}
function drawScore() {
ctx.font = "16px Arial";
ctx.fillStyle = "white";
ctx.fillText("Score: "+ parseInt(score,10), 8, 20);
}
function move(){
//ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.fillStyle = 'rgba( 0, 0, 0, 0.2)';
ctx.fillRect(0, 0,2* canvas.width,2* canvas.height);
// getData();
obstacles();
setRotate(angle);
score=score+0.02;
drawScore();
Hitout(dc,ds,lEdge,tEdge,obsWidth);
//console.log(ds);
//console.log(dc);
for(var i=0;i<10;i++){
if(tEdge[i]<canvas.height){
t=t+.001;
tEdge[i]=tEdge[i]+t;
}}
}timer=setInterval(move,20);
function Hitout(dc,ds,lEdge,tEdge,obsWidth){
let top=tEdge;
let leftEdge=lEdge;
let rightEdge=lEdge+obsWidth;
let bottom=tEdge+obsHeight;
if( insideX(dc,leftEdge,rightEdge,ds, top, bottom)||insideY(dc,leftEdge,rightEdge,ds, top, bottom)){ // X
StopAnimation();
// alert("0");
}
}
function setRotate(angle){
if(left){
ctx.save();
ctx.translate(midwidth,x);
ctx.rotate(angle*Math.PI/180);
ctx.translate(-midwidth,-x);
ball();
ctx.restore();
}
else if(right){
ctx.save();
ctx.translate(midwidth,x);
ctx.rotate(angle*Math.PI/180);
ctx.translate(-midwidth,-x);
ball();
ctx.restore();
}
else if(!(left && right)){
ctx.save();
//ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.translate(midwidth,x);
ctx.rotate(angle*Math.PI/180);
ctx.translate(-midwidth,-x);
ball();
ctx.restore();
}
}//setInterval(setRotate,100);
function key(){
move();
setRotate(angle);
Hitout(dc,ds,lEdge,tEdge);
drawScore();
}
function StopAnimation(){
clearInterval(timer);
}
</script>
</body>
</html>