-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
115 lines (91 loc) · 3.34 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
import { updateGround, setupGround } from "./ground.js"
import { updateRabbit, setUpRabbit, getRabbitRect, setRabbitLose } from './rabbit.js'
import { updateEasterEggs, setupEasterEggs, getEggsRects } from './easterEggs.js'
const CONTAINER_WIDTH = 100
const CONTAINER_HEIGHT = 30
const INCREASE_SPEED_BY = 0.00001
const container = document.querySelector('[data-container]')
const scoreElement = document.querySelector('[data-score]')
const startElement = document.querySelector('[data-start]')
resizeEverything()
window.addEventListener('resize', resizeEverything)
document.addEventListener('keydown', startGame, { once: true }) // making sure it only calls the function once
let lastUpdateTime
let speedScale
let score
let hasGameStarted = false
function update(timeNow) {
if (lastUpdateTime == null) {
lastUpdateTime = timeNow
window.requestAnimationFrame(update)
return
}
const diff = timeNow - lastUpdateTime
updateGround(diff, speedScale)
updateRabbit(diff, speedScale)
updateEasterEggs(diff, speedScale)
updateSpeedScale(diff)
updateScore(diff)
if(checkLose()) return handleLose() //stop updating
lastUpdateTime = timeNow
window.requestAnimationFrame(update)
}
function checkLose() {
const rabbitRect = getRabbitRect()
return getEggsRects().some(rect => isCollision(rect, rabbitRect))
}
function isCollision(rect1, rect2) {
return rect1.left < rect2.right &&
rect1.top < rect2.bottom &&
rect1.right > rect2.left &&
rect1.bottom > rect2.top
}
function updateSpeedScale(diff) {
speedScale += diff * INCREASE_SPEED_BY // to cause the ground to gradually move faster, like in the original game so it becomes more and more difficult
}
function updateScore(diff) {
if (!hasGameStarted) score = 0
score += diff * 0.01 // one point added to score for every 100ms that pass
scoreElement.textContent = Math.floor(score)
}
function startGame() {
hasGameStarted = true
lastUpdateTime = null
speedScale = 1
score = 0
setupGround()
setUpRabbit()
setupEasterEggs()
musicStart()
startElement.classList.add('hide')
window.requestAnimationFrame(update)
}
function handleLose() {
setRabbitLose()
audioStart.pause();
audioStart.currentTime = 0;
let loseAudio = new Audio();
loseAudio.src = "bunny-hop-sounds/game-over.mp3";
loseAudio.play();
setTimeout(() => {
document.addEventListener("keydown", startGame, {once: true})
startElement.classList.remove("hide")}, 100) //we added it so it wont automaticlly start the game
}
// this function makes sure everything inside the container is responsive (rabbit and ground, etc)
function resizeEverything() {
let containerToPixelScale
if (window.innerWidth / window.innerHeight < CONTAINER_WIDTH / CONTAINER_HEIGHT) {
containerToPixelScale = window.innerWidth / CONTAINER_WIDTH
} else {
z
containerToPixelScale = window.innerHeight / CONTAINER_HEIGHT
}
container.style.width = `${CONTAINER_WIDTH * containerToPixelScale}px`
container.style.height = `${CONTAINER_HEIGHT * containerToPixelScale}px`
}
const audioStart = new Audio()
function musicStart() {
audioStart.src="bunny-hop-sounds/bg-music.mp3";
audioStart.volume = 0.5; // adjust the volume to 0.5 (half of the max volume)
audioStart.play();
}