generated from irm1005-itec1005-fall-2023/template-assignment-05
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
225 lines (182 loc) · 5.65 KB
/
game.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
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
import Player from './player.js'
import Ground from './ground.js'
import asteroidcontroller from './asteroidcontroller.js'
import Score from './score.js'
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
//gamespeed
const gamespeedSTART = 1.0
const gamespeedINCREMENT = 0.00001;
const Game_width = 800;
const Game_height = 200;
const Player_width = 88/ 1.5; //height proportionate
const Player_height = 94/ 1.5
const Max_jump_height = Game_height
const Min_jump_height = 150;
//ground
const ground_width = 2400;
const ground_height = 24;
const ground_asteroid_speed = 0.5;
const asteroidconfig = [
{width:100 /1.5, height: 90 / 1.5, image: "images/Satellite.webp"},
{width:90 /1.5, height: 90/ 1.5, image: "images/comet.gif"},
{width:110 /1.5, height: 140/ 1.5, image: "images/rotating-asteroid-hand-drawn-in-photoshop-after-effects-v0-8fpmswc0avmb1.png"},
]
//game objects:
let player = null;
let ground = null;
let AsteroidController = null;
let score = null;
let scaleRatio = null;
let previousTime = null;
let gamespeed = gamespeedSTART;
let gameover = false;
let hasAddedEventListenersForRestart = false;
let waitingtostart = true;
function createSprites() {
const playerwidthingame = Player_width * scaleRatio;
const playerheightingame = Player_height * scaleRatio;
const minjumpheightingame = Min_jump_height * scaleRatio;
const maxjumpheightingame = Max_jump_height * scaleRatio;
const groundwidthingame = ground_width * scaleRatio;
const groundheightingame = ground_height * scaleRatio;
player = new Player(
ctx,
playerwidthingame,
playerheightingame,
minjumpheightingame,
maxjumpheightingame,
scaleRatio
);
ground = new Ground(
ctx,
groundwidthingame,
groundheightingame,
ground_asteroid_speed,
scaleRatio);
const asteroidImages = asteroidconfig.map(asteroid =>{
const image = new Image ();
image.src = asteroid .image;
return {
image: image,
width: asteroid.width * scaleRatio,
height: asteroid.height * scaleRatio,
};
})
AsteroidController = new asteroidcontroller(
ctx,
asteroidImages,
scaleRatio,
ground_asteroid_speed,
)
score = new Score(ctx, scaleRatio);
}
//screen sizing and compatibility
function setScreen(){
scaleRatio = getScaleRatio();
canvas.width = Game_width * scaleRatio;
canvas.height = Game_height * scaleRatio;
createSprites();
}
setScreen();
//set timeout to fix on safari browser
window.addEventListener("resize", () => setTimeout(setScreen, 500));
if (screen.oreintation) {
screen.orientation.addEventListener("change", setScreen);
}
function getScaleRatio(){
const screenHeight = Math.min(
window.innerHeight,
document.documentElement.clientHeight
);
const screenWidth = Math.min(
window.innerWidth,
document.documentElement.clientWidth
);
//window is wider than game width
if(screenWidth/ screenHeight < Game_width/ Game_height){
return screenWidth/ Game_width;
} else {
return screenWidth/ Game_height;
}
}
function showGameOver() {
const fontSize = 70 * scaleRatio;
ctx.font = `${fontSize}px MS Pゴシック`;
ctx.fillStyle = "#FF69B4";
const x = canvas.width / 4.5;
const y = canvas.height / 2;
ctx.fillText("GAME OVER", x, y);
}
function setupGameReset() {
if(!hasAddedEventListenersForRestart){
hasAddedEventListenersForRestart = true;
setTimeout(()=>{
window.addEventListener("keyup", reset, { once: true })
window.addEventListener("touchstart", reset, { once: true });
}, 100 );
}
}
function clearScreen() {
ctx.fillStyle = "#00000000";
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function reset(){
hasAddedEventListenersForRestart = false;
gameover = false;
waitingtostart = false;
ground.reset();
AsteroidController.reset();
score.reset();
gamespeed = gamespeedSTART;
}
function showStartGameText(){
const fontSize = 35 * scaleRatio;
ctx.font = `${fontSize}px MS Pゴシック`;
ctx.fillStyle = "#FF69B4";
const x = canvas.width / 14;
const y = canvas.height / 2;
ctx.fillText("TAP SCREEN OR PRESS SPACE TO START", x, y);
}
function updateGameSpeed(frameTimeDelta){
gamespeed += frameTimeDelta * gamespeedINCREMENT;
}
function gameLoop(currentTime){
console.log(gamespeed);
if(previousTime === null){
previousTime = currentTime;
requestAnimationFrame(gameLoop);
return;
}
const frameTimeDelta = currentTime - previousTime;
previousTime = currentTime;
clearScreen();
if (!gameover && !waitingtostart){
//update game objects
ground.update(gamespeed, frameTimeDelta);
AsteroidController.update(gamespeed, frameTimeDelta);
player.update(gamespeed, frameTimeDelta);
score.update(frameTimeDelta);
updateGameSpeed(frameTimeDelta);
}
if (!gameover && AsteroidController.collideWith(player)){
gameover = true;
setupGameReset();
score.setHighScore();
}
//draw game objects
ground.draw();
AsteroidController.draw();
player.draw();
score.draw()
if(gameover){
showGameOver();
}
if(waitingtostart){
showStartGameText();
}
requestAnimationFrame(gameLoop);
}
requestAnimationFrame(gameLoop);
window.addEventListener("keyup", reset, { once: true });
window.addEventListener("touchstart", reset, { once: true });