This repository has been archived by the owner on Aug 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
169 lines (131 loc) · 4.03 KB
/
script.js
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
var gunAngle = 6, gunAngleRad, gunEnd = [];
var bombX, bombY; //bomb coordinates
var dy, dy; //change in bomb coordinates
var gravity = 0.01;
var points = 0, triesLeft =10;
function prepareBattleField(){
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath(); // hill triangle
ctx.moveTo(450, 450);
ctx.lineTo(350, 150);
ctx.lineTo(250, 450);
ctx.fillRect(22,430, 55, 20); // my tank body
ctx.closePath();
ctx.fill();
ctx.fillRect(722,430, 55, 20); // enemy tank body
ctx.closePath();
ctx.fill();
ctx.moveTo(769, 430); // enemy tank gun
ctx.lineTo(720 , 420);
ctx.stroke();
drawTankGun(gunAngle);
}
function drawTankGun(gunAngle){
var ctx = canvas.getContext('2d');
ctx.lineWidth = 4;
gunAngleRad = -gunAngle * (Math.PI / 180);
gunEnd[0] = 25 + 75 * Math.cos(gunAngleRad);
gunEnd[1] = 430 + 75 * Math.sin(gunAngleRad);
ctx.moveTo(25, 430);
ctx.lineTo(gunEnd[0], gunEnd[1]);
ctx.stroke();
}
function checkCollisions(){
if(bombX>250 && bombX <350)
{
// alert(bombX + " " + bombY) ;
if(bombY > (-(3*bombX) + 1200))
{
$("#messages").text("That bomb got wasted :(. Try agin");
window.cancelAnimationFrame(bombAnimaton);
}
}
else if(bombY > canvas.height)
{
$("#messages").text("The bomb was grounded :(")
window.cancelAnimationFrame(bombAnimaton);
}
else if(bombX > canvas.width)
{
$("#messages").text("That was overshot :( Try again")
window.cancelAnimationFrame(bombAnimaton);
}
else if((bombX > 722 && bombX<772) && (bombY>430 && bombY<450))
{
points += 1;
$("#points").text(points);
$("#messages").text("You've scored ^_^")
gunAngle = 7;
window.cancelAnimationFrame(bombAnimaton);
}
}
function drawBomb(){
//alert("ii");
var ctx = canvas.getContext('2d');
prepareBattleField();
ctx.beginPath();
ctx.arc(bombX, bombY, 10, 0, Math.PI*2);
ctx.fillStyle = "#000000";
ctx.fill();
ctx.closePath();
bombX += dx;
bombY += dy;
checkCollisions();
dy += gravity;
var bombAnimaton = window.requestAnimationFrame(drawBomb);
}
function fire(){
var ctx = canvas.getContext('2d');
dx = (Math.cos(gunAngleRad))*3;
dy = (Math.sin(gunAngleRad))*3;
/*ctx.beginPath();
ctx.arc(gunEnd[0], gunEnd[1], 10, 0, Math.PI*2);
ctx.fillStyle = "#000000";
ctx.fill();
ctx.closePath();*/
bombX = gunEnd[0];
bombY = gunEnd[1];
triesLeft -= 1;
$("#tries-left").html(triesLeft);
$("#messages").text("");
if(triesLeft == 0)
{
$("#controls").text("The game is over.");
$("#controls").append("<br> You scored " + points + " points <br>.");
$("#controls").append("<button onClick='location.reload()'> Play again </button>")
}
window.requestAnimationFrame(drawBomb);
//drawBomb();
}
function reload(){
}
function all(){
prepareBattleField();
$("#plus-angle").click(function(){
if(gunAngle<90)
{
gunAngle += 2;
prepareBattleField();
}
else{
$("#messages").text("That's tooo far");
}
//drawTankGun(gunAngle);
});
$("#minus-angle").click(function(){
if(gunAngle>2)
{
gunAngle -= 2;
prepareBattleField();
}
else{
$("#messages").text("That's too far");
}
});
$(".angle-change-button").click(function(){
window.cancelAnimationFrame(bombAnimaton);
});
$("#fire").click(fire);
} // end of all function
$(document).ready(all);