-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
253 lines (233 loc) · 7.53 KB
/
index.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var cvs = document.getElementById("canvas");
var ctx = cvs.getContext("2d");
const WIDTH = 1000;
const HEIGHT = 700;
var wrappage = true;
var gamepage = false;
var restpage = false;
function wrapper() {
start = {
width: 550-137.5,
height: 300,
visible: true,
}
start.img = new Image();
start.img.src = "img/start.png";
start.img.onload = draw;
var func = function(e) {
x = e.clientX;
y = e.clientY;
if ((x >= 659 && x <= 1018) && (y >= 500 && y <= 550))
{
window.removeEventListener('mousedown', func, false);
wrappage = false;
game();
}
}
window.addEventListener('mousedown', func);
function draw() {
ctx.drawImage(start.img, WIDTH/2- start.width + 210, HEIGHT/2 - start.height + 100, start.width, start.height);
if (wrappage)
requestAnimationFrame(draw);
}
}
function restart(score) {
restpage = true;
ctx.fillStyle = "#cddac8";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.fillStyle = "#000";
ctx.strokeStyle = "#F00";
ctx.font = "bold 28pt Courier New";
var str = "Your score: " + score.toString()
;
ctx.fillText(str, 340, 250);
var func = function(e) {
x = e.clientX;
y = e.clientY;
console.log(x, " ", y);
if ((x >= 654 && x <= 1042) && (y >= 467 && y <= 506))
{
window.removeEventListener('mousedown', func, false);
restpage = false;
game();
}
}
window.addEventListener('mousedown', func);
var rest = new Image();
rest.src = "img/restart.png";
rest.onload = draw;
function draw() {
if (restpage) {
ctx.drawImage(rest, 300, 300);
requestAnimationFrame(draw);
}
}
}
function game() {
gamepage = true;
var bg = new Image(); bg.src = "img/bg.png";
var grass = new Image(); grass.src = "img/grass.png";
var player_right = {
visible: 0,
height: 75,
width: 50
};
player_right.img = new Image(); player_right.img.src = "img/player_right.png";
var player_left = { visible: 1 };
player_left.img = new Image(); player_left.img.src = "img/player_left.png";
var xPos = WIDTH / 2 - 75;
var yPos = 575;
var player_speed = 8;
var A_Pressed = false;
var D_Pressed = false;
var jumpPressed = false;
var jumpCount = 0;
var jumpLength = 50;
var jumpHeight = 0;
var helths_count = 7;
var helths = [];
var helth = {
xPos: 10,
yPos: 50,
width: 50,
height: 50
}
helth.img = new Image();
helth.img.src = "img/helth.png";
for (var i = 1; i <= helths_count; i++)
{
helths[i] = Object.assign({}, helth);
}
function spawn_helths() {
for (var i = 1; i <= helths_count; i++)
{
helths[i].yPos = 10 + (helth.height + 5) * i ;
ctx.drawImage(helths[i].img, helths[i].xPos, helths[i].yPos, helth.width, helth.height);
}
}
var score = 0;
var moneys_count = 7;
var moneys_visible = true;
var moneys = [];
var money = {
xPos: 100,
yPos: -30,
width: 50,
height: 50,
speed: 1,
};
moneys_change_speed = 0;
money.img = new Image();
money.img.src = "img/money.png";
let timer = setInterval(() => moneys_change_speed += 0.1, 5000 );
function write_score(score) {
ctx.fillStyle = "#000";
ctx.strokeStyle = "#F00";
ctx.font = "bold 27pt Courier New";
var str = "Score: " + score.toString();
ctx.fillText(str, 10, 35);
}
function spawnMoneys() {
for (var i = 0; i <= moneys_count; i++)
{
moneys[i] = Object.assign({}, money);
moneys[i].xPos = Math.floor(Math.random() * (WIDTH - 100 + 1)) + 50;
moneys[i].yPos = - Math.floor(Math.random() * (1000 - 30)) + 30;
moneys[i].speed = Math.random() + 0.1;
}
}
var keyDown = function(e) {
switch(e.keyCode) {
case 65:
A_Pressed = true;
player_left.visible = 1;
player_right.visible = 0;
break;
case 68:
D_Pressed = true;
player_left.visible = 0;
player_right.visible = 1;
break;
case 32:
jumpPressed = true;
break;
}
}
window.addEventListener('keydown', keyDown);
var keyUp = function(e) {
switch(e.keyCode) {
case 65:
A_Pressed = false;
break;
case 68:
D_Pressed = false;
break;
}
}
window.addEventListener('keyup', keyUp);
function draw()
{
if (moneys_count) {
ctx.drawImage(bg, 0, -100);
ctx.drawImage(grass, 0, 650);
spawn_helths();
yPos = canvas.height-125-jumpHeight;
if (player_left.visible == 1) {
ctx.drawImage(player_left.img, xPos, yPos, player_right.width, player_right.height);
if ((A_Pressed == 1) && (xPos > 75)){
xPos -= player_speed;
}
}
if (player_right.visible == 1) {
ctx.drawImage(player_right.img, xPos, yPos, player_right.width, player_right.height);
if ((D_Pressed == 1) && (xPos < WIDTH - 60)){
xPos += player_speed;
}
}
if(jumpPressed) {
jumpCount++;
jumpHeight = 3*jumpLength*Math.sin(Math.PI*jumpCount/jumpLength);
}
if(jumpCount>jumpLength) {
jumpCount=0;
jumpPressed=false;
jumpHeight=0;
}
for (var i = 0; i < moneys_count; i++)
{
moneys[i].yPos += moneys[i].speed + moneys_change_speed;
if (moneys_visible) {ctx.drawImage(moneys[i].img, moneys[i].xPos, moneys[i].yPos, 30, 30);}
if (moneys[i].yPos > 624)
{
moneys[i].yPos = - Math.floor(Math.random() * (400 - 30)) + 30;
moneys[i].xPos = Math.floor(Math.random() * (WIDTH - 100 + 1)) + 70;
helths_count -= 1;
if (helths_count == 0) {
removeEventListener('keydown', keyDown, false);
removeEventListener('keyup', keyUp, false);
player_left.visible = 0;
player_right.visible = 0;
delete timer;
moneys_visible = false;
gamepage = false;
restart(score);
}
}
else
if (((xPos + player_right.width >= moneys[i].xPos) && (xPos <= moneys[i].xPos + moneys[i].width-25)) && (yPos + player_right.height>= moneys[i].yPos) && (yPos <= moneys[i].yPos + moneys[i].height-25))
{
moneys[i].yPos = - Math.floor(Math.random() * (400 - 30)) + 30;;
moneys[i].xPos = Math.floor(Math.random() * (WIDTH - 100 + 1)) + 70;
score += 1;
}
}
if (gamepage){
requestAnimationFrame(draw);
write_score(score);
}
}
}
spawnMoneys();
moneys[moneys_count].img.onload = draw;
}
wrapper();