-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (128 loc) · 5.33 KB
/
Copy pathscript.js
File metadata and controls
143 lines (128 loc) · 5.33 KB
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
class GameApp {
constructor() {
this.calcPoints();
this.gunShot();
this.restart();
this.hardModeStart(); // Call the function when the page is loaded
// this.cursorTracking();
}
calcPoints() {
let self = this;
let points = 0;
$('[class^=circle]').mousedown(function() {
const numberOfPoint = $(this).attr('data-label');
points += parseInt(numberOfPoint);
$('.score').text(points + ' pts');
});
$('.hardMode').click(function() {
$('.score').text('0 point');
points = 0;
self.hardModeStart();
});
}
gunShot() {
// Click on the target to leave bullet holes and bullet shell sounds
$('.target').mousedown(function(e) {
const bulletHole = $('<div class="bulletHole">');
const gunShotSound = new Audio('https://raw.githubusercontent.com/raychang2017/f2e-portfolio/master/07%20-%20JavaScript%20%E6%A8%99%E9%9D%B6%E9%81%8A%E6%88%B2%20%E2%80%93%20%E6%BB%91%E9%BC%A0%E4%BD%A0%E4%B8%80%E5%88%86%E9%90%98%E8%83%BD%E6%8C%89%E5%B9%BE%E4%B8%8B%EF%BC%9F/audio/5.56-AR15-Single-Close-GunShot-A.mp3');
const BulletShellFallingSound = new Audio(
'https://raw.githubusercontent.com/raychang2017/f2e-portfolio/master/07%20-%20JavaScript%20%E6%A8%99%E9%9D%B6%E9%81%8A%E6%88%B2%20%E2%80%93%20%E6%BB%91%E9%BC%A0%E4%BD%A0%E4%B8%80%E5%88%86%E9%90%98%E8%83%BD%E6%8C%89%E5%B9%BE%E4%B8%8B%EF%BC%9F/audio/empty-bullet-shell-fall-01.mp3'
);
// Because the upper left corner is positioned with .target as the center, the offset needs to be adjusted offset()
// After changing body to cursor: cell, it will be offset again, so adjust it back to 8 px
bulletHole.css({
// clientX、pageX、screenX (https://segmentfault.com/a/1190000002405897)
left: e.pageX - $(this).offset().left - 8 + 'px',
top: e.pageY - $(this).offset().top - 8 + 'px'
});
$(this).append(bulletHole);
gunShotSound.play();
setTimeout(() => BulletShellFallingSound.play(), 500);
});
}
restart() {
// 'enter', 'delete' does not need to use function(e) and if (e.key == 'enter')
$('.restart').click(function() {
// After the Hard mode timer returns to zero, the bullet holes and scores are unbind() and restored using this page refresh
window.location = window.location.href; // can refresh the previous page
});
}
hardModeStart() {
$('.hardMode').hide();
$('.bulletHole').remove();
$('.target').addClass('moving');
// Count down 60 seconds, and the remaining 10 seconds will turn into big red text
let sec = 1500;
// The recursive version of setTimeout can ensure that the delay between each execution is accurate, but setInterval cannot, because the execution time of func offsets part of the interval time. In extreme cases, if the execution time of the function exceeds the time set by delay, there will be no pause every time setTimeout is called.
(function count() {
$('.remainingTime').text(sec);
if (sec === 0) {
$('.target').removeClass('moving');
$('.remainingTime').text('0');
$('body').css('cursor', 'default');
// Stop the scoring event, stop the bullet hole event, stop the mode switching event
$('[class^=circle]').unbind();
$('.target').unbind();
$('.hardMode').unbind();
// Generally speaking, using JS to add or reduce classes is better than directly modifying CSS (the browser will re-render, and using mobile platforms may cause slower problems). Here, modifying CSS directly is for convenience.
} else if (sec <= 10) {
$('.remainingTime').css({
color: '#fe5b45',
'font-size': '50px'
});
} else {
$('.remainingTime').css({
color: 'black',
'font-size': '30px'
});
}
if (sec > 0) {
setTimeout(count, 1000);
}
sec--;
})();
// // Use setInterval
// let countDown = setInterval(function() {
// sec -= 1;
// $('.remainingTime').text(sec);
// // Other methods of reloading and canceling unbind() can be used with the following code
// // $('.restart').click(function(){
// // sec = 60
// // clearInterval(countDown)
// // $('.remainingTime').text('')
// // })
// if (sec <= 0) {
// clearInterval(countDown);
// $('.target').removeClass('moving');
// $('.remainingTime').text('0');
// $('body').css('cursor', 'default');
// //Score event stops, bullet hole event stops, mode switching event stops
// $('[class^=circle]').unbind();
// $('.target').unbind();
// $('.hardMode').unbind();
// // Generally speaking, using JS to add or reduce classes is better than directly modifying CSS (the browser will re-render, and using mobile platforms may cause slower problems). Here, modifying CSS directly is for convenience.
// } else if (sec <= 10) {
// $('.remainingTime').css({
// color: '#fe5b45',
// 'font-size': '50px'
// });
// } else {
// $('.remainingTime').css({
// color: 'black',
// 'font-size': '30px'
// });
// }
// }, 1000);
}
cursorTracking() {
// The cross mark moves with the mouse
$(window).mousemove(function(e) {
// console.log(e.pageX+','+e.pageY)
$('.crossSign').css({
left: e.pageX + 'px',
top: e.pageY + 'px'
});
});
}
}
var Game = new GameApp();