-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
228 lines (188 loc) · 4.89 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
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.lineWidth=1;
canvas.width=400
canvas.height=400
let dim = Math.min(window.innerWidth, window.innerHeight)
dim -= dim/4
canvas.style.width= dim + "px"
canvas.style.height= dim + "px"
// document.getElementById("brs").style.width = dim + "px"
const SHIFT = 2;
const L = 40;
const H = 40;
const CHAR_L = 10;
const CHAR_H = 10;
const A = -0.3
const STROKE_COLOR = "black"
const TILE_COLOR="green"
const STROKE_COMPARISON = [0, 0, 0, 255]
const TILE_COMPARISON = [0, 128, 0, 255]
const BACKGROUND_COLOR = "blue";
var Y;
var tile_list;
var char_y;
var char_x;
var vi;
var collide;
var stop;
var once;
var tiles_pos;
var tiles_heights;
var score;
var startTime;
var anim;
function init(){
char_y = 400/2
Y = 400 - H;
tile_list = [];
char_x = 60;
vi = 0
collide = true
stop = false
once = false;
tiles_pos = []
tiles_heights = []
score = 0
startTime = new Date();
console.log(startTime)
}
// init()
console.log(400/L)
function jump(){
if(is_collision()){
char_y -= 10
}
vi = 3
draw_character()
}
function compare_arrs(arr1, arr2){
for(let i = 0; i < arr1.length; i++){
if(arr1[i] !== arr2[i]){
return false
}
}
return true
}
function is_collision() {
ctx.beginPath()
ctx.fillStyle="orange"
let val = [char_x + (CHAR_H/2), char_y + (CHAR_L+1)]
let imgdata = ctx.getImageData(...val, 1,1)
return compare_arrs(imgdata.data, TILE_COMPARISON) || compare_arrs(imgdata.data, STROKE_COMPARISON)
}
function is_collision_x(){
ctx.beginPath()
ctx.fillStyle="orange"
let val = [char_x + (CHAR_H+1), char_y + (CHAR_L/2)]
let imgdata = ctx.getImageData(...val, 1,1)
return compare_arrs(imgdata.data, TILE_COMPARISON) || compare_arrs(imgdata.data, STROKE_COMPARISON)
}
function rect(x, y, w, h, fillStyle){
ctx.beginPath()
ctx.fillStyle=fillStyle
ctx.strokeStyle = STROKE_COLOR;
ctx.rect(x, y, w, h)
ctx.fill()
ctx.stroke()
ctx.closePath()
}
function draw_tiles_refined(){
if(tiles_pos.length === 0){
for(let i = 0; i <= 400; i += L){
tiles_pos.push(i)
i < 300 ? tiles_heights.push(-2) : tiles_heights.push(Math.random() * 4)
}
}
if(tiles_pos[0] < -L){
tiles_pos.shift()
tiles_heights.shift()
tiles_pos.push(400-2 * SHIFT)
tiles_heights.push(Math.random() * 4)
}
for(let i = 0; i<tiles_pos.length; i++){
let save_Y = Y
for(let j=0; j<tiles_heights[i]; j++){
rect(tiles_pos[i], Y, L, H, TILE_COLOR)
Y -= H
}
for(let j=0; j<tiles_heights[i]/2 + 2; j++){
Y -= H
}
rect(tiles_pos[i], Y, L, H, TILE_COLOR)
Y = save_Y
tiles_pos[i] -= SHIFT
}
}
function draw_character(){
collide = is_collision()
!collide ? vi += vi * 0.001 - Math.pow(A, 2) : vi=0
char_y -= vi
rect(char_x, char_y, CHAR_L, CHAR_H, "red")
}
function stopstart(){
tiles_pos = []
tiles_heights = []
// stop=false
// char_y = 400/2
// char_x = 60;
// vi = 0
init()
// document.getElementById("test").innerText = "-"
if (anim) {
window.cancelAnimationFrame(anim);
anim = undefined;
}
anim = window.requestAnimationFrame(game_loop)
}
function drawStroked(text, x, y, font=80) {
ctx.font = `${font}px Sans-serif`;
ctx.strokeStyle = 'black';
ctx.textAlign="center"
ctx.lineWidth = 8;
ctx.strokeText(text, x, y);
ctx.fillStyle = 'red';
ctx.fillText(text, x, y);
ctx.lineWidth=1
}
function mstop(){
stop = true
drawStroked("you lost", 200, 200, 70)
drawStroked("Happy mothers' day!", 200, 250, 30)
drawStroked("You're the best mom in the world!", 200, 300, 20)
window.cancelAnimationFrame(anim)
}
function drawElapsedTime() {
// console.log(startTime)
var elapsed = parseInt((new Date() - startTime) / 1000);
drawStroked(`Score: ${elapsed}`, 50, 50, 20)
}
function game_loop() {
if(stop){
return;
}
// console.log("SOMEHOW STOP IS TRUE")
setTimeout(() => {anim = window.requestAnimationFrame(game_loop)}, 1000/60)
ctx.clearRect(0, 0, canvas.width, canvas.height);
rect(0, 0, canvas.width, canvas.height, BACKGROUND_COLOR)
draw_tiles_refined()
draw_character()
while(is_collision()){
char_y -= 1
}
if(is_collision_x() || char_y < 0){
// document.getElementById('test').innerText = "You lost, good game!"
// console.log("LOST")
mstop()
// console.log(stop)
}
drawElapsedTime()
}
document.body.onkeydown = function(e){
if(e.keyCode == 32){
jump()
// console.log(vi)
}
}
canvas.addEventListener('click', function() { jump() }, false);
// var anim = window.requestAnimationFrame(game_loop)