-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainMenu.html
227 lines (208 loc) · 6.52 KB
/
mainMenu.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<!DOCTYPE html>
<!-- Свали последната версия от: https://github.com/StanislavNikolov/gamedev_pishtov -->
<!-- Download the latest verion from: https://github.com/StanislavNikolov/gamedev_pishtov -->
<html>
<head>
<style>
body, canvas {
position:absolute;
left:0px;
top:0px;
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<meta charset="UTF-8"></meta> <!-- allows for cyrillic and other exotic text in console.logs -->
</head>
<body onload="init()">
<canvas id="canvas-id" width="800" height="600"></canvas>
<script>
function areColliding(Ax, Ay, Awidth, Aheight, Bx, By, Bwidth, Bheight) {
if (Bx <= Ax + Awidth) {
if (Ax <= Bx + Bwidth) {
if (By <= Ay + Aheight) {
if (Ay <= By + Bheight) {
return 1;
}
}
}
}
return 0;
};
function randomInteger(upTo){
return Math.floor(Math.random()*upTo);
}
function drawLine(startX, startY, endX, endY) {
// For better performance bunch calls to lineTo WITHOUT beginPath() and stroke() inbetween.
context.beginPath(); // resets the current path
context.moveTo(startX, startY);
context.lineTo(endX, endY);
context.stroke();
}
function tryToLoad(imageNameWithoutDotPng, backupColor) {
return tryToLoadWithFullPath(imageNameWithoutDotPng + '.png', backupColor);
}
function tryToLoadWithFullPath(imageNameAndPath, backupColor) {
return {
img : new Image(),
src : "images/" + imageNameAndPath,
color : backupColor,
needed: false // flag letting know drawImage to request the actual image if ever used
};
}
function drawImage(img, x, y, xs, ys) {
if(img == null) {
// invalid image
console.error('Tried to draw invalid image');
return;
}
if(img.needed !== true) {
img.needed = true;
img.img.src = img.src;
}
try {
if (xs != null) context.drawImage(img.img, x, y, xs, ys);
else context.drawImage(img.img, x, y);
} catch(e) {
context.fillStyle = img.color;
if(xs == null) {
xs = 100;
ys = 100;
}
context.fillRect(x, y, xs, ys);
}
}
let endlessCanvas = true ;
let updateTime = 10; // in ms
let canvases=[]
const canvas = document.getElementById("canvas-id");
// const canvas2 = document.getElementById("canvas2-id");
const context = canvas.getContext("2d");
// const context2 = canvas2.getContext("2d");
context.fillStyle = "#0000ff";
// global variables with mouse coordinates
let mouseX = 0;
let mouseY = 0;
// some keycodes
let key_left = 37;
let key_up = 38;
let key_right = 39;
let key_down = 40;
let key_a = 65;
let key_z = 90;
let isKeyPressed = [];
for (i = 0; i < 256; ++i) {
isKeyPressed.push(0);
}
// gridSize = 50; // uncomment or add to game.js if you want a grid
const reqAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
setTimeout(callback, 1000 / 30);
};
function redraw() {
context.clearRect(0, 0, canvas.width, canvas.height);
draw(); // call progammer's draw() function
context.globalAlpha = 1;
// draw grid
//context.fillStyle = "#FF0000";
context.font = "10px Arial";
if (typeof gridSize != "undefined" && gridSize >= 25) {
context.fillText(0, 4, 10);
context.beginPath();
for(i = gridSize;i < canvas.width;i += gridSize) {
context.moveTo(i, 0);
context.lineTo(i, canvas.height);
context.fillText(i, i + 4, 10);
}
for(i = gridSize;i < canvas.height;i += gridSize) {
context.moveTo(0, i);
context.lineTo(canvas.width, i);
context.fillText(i, 4, i + 10);
}
context.stroke();
}
reqAnimationFrame(redraw);
};
function init() {
if (endlessCanvas) {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.onresize = function () {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
} else {
canvas.width = 800;
canvas.height = 600;
}
if ('ontouchstart' in window || navigator.maxTouchPoints) {
isMobile = true;
window.addEventListener("touchstart", function (e) {
let touchobj = e.changedTouches[0];
mouseX = parseInt(touchobj.pageX - canvas.offsetLeft);
mouseY = parseInt(touchobj.pageY - canvas.offsetTop);
mousedown();
});
window.addEventListener("touchend", function (e) {
let touchobj = e.changedTouches[0];
mouseX = parseInt(touchobj.pageX - canvas.offsetLeft);
mouseY = parseInt(touchobj.pageY - canvas.offsetTop);
mouseup();
});
window.addEventListener("touchmove", function (e) {
let touchobj = e.changedTouches[0];
mouseX = parseInt(touchobj.pageX - canvas.offsetLeft);
mouseY = parseInt(touchobj.pageY - canvas.offsetTop);
});
}
window.addEventListener("mousemove", function (e) {
mouseX = e.pageX - canvas.offsetLeft;
mouseY = e.pageY - canvas.offsetTop;
});
if(typeof mousemove != "undefined") {
window.addEventListener("mousemove", mousemove);
}
if(typeof mouseup != "undefined") {
window.addEventListener("mouseup", mouseup);
}
if(typeof mousedown != "undefined") {
window.addEventListener("mousedown", mousedown);
}
if(typeof keydown != "undefined") {
window.addEventListener("keydown", function (e) {
isKeyPressed[e.keyCode] = 1;
keydown(e.keyCode);
});
} else {
window.addEventListener("keydown", function (e) {
isKeyPressed[e.keyCode] = 1;
});
}
if(typeof keyup != "undefined") {
window.addEventListener("keyup", function (e) {
isKeyPressed[e.keyCode] = 0;
keyup(e.keyCode);
});
} else {
window.addEventListener("keyup", function (e) {
isKeyPressed[e.keyCode] = 0;
});
}
if(typeof draw == "undefined") {
redraw = function () {
context.clearRect(0, 0, canvas.width, canvas.height);
context.globalAlpha = 1;
context.fillStyle = "#FF0000";
context.font = "20px Arial";
context.fillText("Press <F12> for error info!", 40, 40);
};
}
redraw();
setInterval(update, updateTime);
}
</script>
<!-- user's game file -->
<script src="./scripts/mainMenu.js"></script>
</body>
</html>