-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (115 loc) · 3.81 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
canvas = document.getElementById('canvas');
context = canvas.getContext("2d");
canvas.width = window.innerWidth - 20;
ctx = canvas.width;
cty = canvas.height;
const up = document.getElementById('up');
const down = document.getElementById('down');
const left = document.getElementById('left');
const right = document.getElementById('right');
const music = document.getElementById('music');
const musicDesk = document.getElementById('music-desk');
// All vars
var x = 150;
var y = 150;
var t = Date.now();
var speed = 500;
var coinx = Math.random() * (400 - 50);
var coiny = Math.random() * (500 - 50);
//var cs = ['48','45','46','49','50'];
//var coinsize = Math.floor(Math.random() * cs);
var score = 0;
let dir;
const player = new Image();
player.src = './assets/img/Player2.webp';
const coin = new Image();
coin.src = './assets/img/Coin.webp';
const BGmusic = new Audio('./assets/sounds/sgt.mp3');
var playing = true;
const coinGrab = new Audio('./assets/sounds/coin.mp3');
//main
window.onload = function () {
function draw() {
//time and fps settings
var timePassed = (Date.now() - t) / 1000;
t = Date.now();
var fps = Math.round(1 / timePassed);
//clear()
context.clearRect(0, 0, ctx, cty);
//Coins Draw
context.beginPath();
context.drawImage(coin, coinx, coiny, 45, 45);
context.fillStyle = "red";
context.fill();
//Text
context.beginPath();
context.font = '1rem Arial';
context.fillStyle = 'blue';
context.fillText("SCORE: " + score, ctx - 100, 30);
context.fill();
context.fillStyle = "black";
context.fillText("FPS: " + fps, ctx - 380, 30);
context.fillText("Speed: " + speed, ctx - 320, 30);
context.fill();
//Player
context.beginPath();
context.drawImage(player, x, y, 60, 60);
context.stroke();
//Coins random draw
if (coinx + 45 >= x && coinx <= x + 60 &&
coiny + 45 >= y && coiny <= y + 60) {
score++; speed += 13;
coinx = Math.random() * (ctx - 50); coiny = Math.random() * (cty - 50);
coinGrab.play();
}
//Key Functions
canvas.focus();
down.addEventListener('touchstart', function () { dir = 0; });
up.addEventListener('touchstart', function () { dir = 1; });
right.addEventListener('touchstart', function () { dir = 2; });
left.addEventListener('touchstart', function () { dir = 3; });
document.addEventListener('keydown', function (event) {
if (event.key === 'ArrowDown') { dir = 0; }
else if (event.key === 'ArrowUp') { dir = 1; }
else if (event.key === 'ArrowRight') { dir = 2; }
else if (event.key === 'ArrowLeft') { dir = 3; }
});
if (dir == 0 && y + 80 < cty) {
y += (speed * timePassed);
}
else if (dir == 1 && y >= 10) {
y -= (speed * timePassed);
}
else if (dir == 2 && x + 70 < ctx) {
x += (speed * timePassed);
}
else if (dir == 3 && x >= 10) {
x -= (speed * timePassed);
}
window.requestAnimationFrame(draw);
}
draw();
music.onclick = function () {
if (playing) {
BGmusic.play();
playing = false;
}
else { BGmusic.pause(); playing = true; }
}
musicDesk.onclick = function () {
if (playing) {
BGmusic.play();
playing = false;
}
else { BGmusic.pause(); playing = true; }
}
//setInterval(draw,160);
}
$("#screen").hide();
$(window).on("load", function l() {
$(".container").fadeOut(400);
setTimeout(function () {
$("#screen").show();
}, 405)
window.requestAnimationFrame(l);
})