-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
145 lines (124 loc) · 4.7 KB
/
app.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
// For Smartphone Users
screen.orientation.addEventListener("change", function () {
if (screen.orientation.type == "landscape-primary" || screen.orientation.type == "landscape-secondary") {
document.body.style.transform = `rotate(${screen.orientation.type == "landscape-primary" ? -90 : 90})`;
}
});
const handleEvent = (event) => {
if ((event.code === "Space" || event.touches.length >= 1) && !GAME.isGameOver) {
if (player.pushedUp) {
requestAnimationFrame(function () {
push("down");
});
} else {
requestAnimationFrame(function () {
push("up");
});
}
}
};
//DOM Variables
const titleDOM = document.getElementById("title");
const detailsDOM = document.getElementById("details");
const playBtnDOM = document.getElementById("play-btn");
const inputNameDOM = document.getElementById("input-name");
const canvas = document.getElementById("game");
const gameOverlayDOM = document.getElementById("game-overlay");
const gameOverlayTextDOM = document.getElementById("game-overlay-text");
const scoreDOM = document.getElementById("score");
const outerContainerDOM = document.getElementById("outer-container");
const viewScoresMenuDOM = document.getElementById("view-scores-menu");
const highScoresDOM = document.getElementById("high-scores");
const highScoresListDOM = document.getElementById("high-scores-list");
//Load Image for PowerUp Icon ⚡
const powerUpIcon = new Image();
powerUpIcon.src = "Assets/Images/flash.png";
function play() {
titleDOM.style.display = "none";
detailsDOM.style.display = "none";
inputNameDOM.style.display = "none";
playBtnDOM.style.display = "none";
outerContainerDOM.style.display = "flex";
canvas.style.display = "inline";
gameOverlayDOM.style.display = "flex";
scoreDOM.style.display = "inline";
// Load the gamescript
let gameScript = document.createElement("script");
gameScript.setAttribute("src", "./game.js");
document.body.appendChild(gameScript);
// Start after 5000ms
setTimeout(function () {
startGame();
document.addEventListener("keypress", handleEvent);
document.addEventListener("touchstart", handleEvent);
}, 5000);
}
// Renders the Player, Starts rendering the Game
function startGame() {
player.name = document.getElementById("name").value;
gameOverlayDOM.style.display = "none";
// Draw gap
context.fillStyle = grey;
context.fillRect(0, 125, spaceWidth, spaceHeight);
// Draw Player
contextObject.beginPath();
contextObject.moveTo(player.xCord, player.yCord);
contextObject.lineTo(player.xCord + player.side, player.yCord);
contextObject.lineTo(player.xCord + player.side / 2, player.yCord - player.height);
contextObject.closePath();
contextObject.fillStyle = blue;
contextObject.fill();
// Render Holes
window.requestAnimationFrame(renderHoles);
}
function showGameOver() {
gameOverMusic.play();
gameOverlayDOM.style.display = "flex";
scoreDOM.innerHTML = "";
gameOverlayTextDOM.textContent = `Game Over! Score: ${Math.floor(GAME.score / 10)}`;
viewScoresMenuDOM.style.display = "flex";
checkHighScore(Math.floor(GAME.score / 10));
}
function updateScore() {
scoreDOM.textContent = `Score: ${Math.floor(GAME.score / 10)}`;
}
// Check if score is Highscore
function checkHighScore(score) {
var highScores = [];
var lowestScore;
if (!JSON.parse(localStorage.getItem("HIGH_SCORES") === null)) {
highScores = JSON.parse(localStorage.getItem("HIGH_SCORES"));
}
if (highScores == null) {
lowestScore = 0;
} else {
lowestScore = highScores[9] ? highScores[9].playerScore : 0;
}
if (score > lowestScore) {
saveHighScore(highScores, score);
}
}
// Save score if it is among the Top 10 highscores
function saveHighScore(highScores, score) {
newEntry = {
playerScore: score,
playerName: player.name == "" ? "Unknown" : player.name,
};
if (highScores == null) {
highScores = [newEntry];
} else {
highScores.push(newEntry);
}
highScores.sort((a, b) => b.playerScore - a.playerScore);
highScores.splice(10);
localStorage.setItem("HIGH_SCORES", JSON.stringify(highScores));
}
// View Highscore
function viewHighScores() {
viewScoresMenuDOM.style.display = "none";
gameOverlayDOM.style.display = "none";
outerContainerDOM.style.display = "none";
highScoresDOM.style.display = "flex";
var highScores = JSON.parse(localStorage.getItem("HIGH_SCORES")) ?? [];
highScoresListDOM.innerHTML = highScores.map((entry) => `<li>${entry.playerScore} - ${entry.playerName}`).join("");
}