-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpomodoroGame.html
315 lines (304 loc) · 8.94 KB
/
pomodoroGame.html
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<!doctype html>
<html lang="en">
<head>
<!--automatically redirects to timer page after 5 mins-->
<meta http-equiv="refresh" content="300;url=timer.html" charset="UTF-8" />
<title>Pomodoro Game</title>
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/phaser.js"></script>
<style type="text/css">
body {
margin: 0;
}
.wrapper{
text-align:center;
}
#studyBtn, #restartBtn{
font-size: 150%;
font-family: "Courier New";
border-radius:12px;
}
#studyBtn{
background-color:#cceeff;
}
#restartBtn{
background-color:#ff6666;
}
</style>
<div class="wrapper">
<button id="studyBtn">Study</button>
<button id="restartBtn">Restart</button>
</div>
</head>
<body>
<script type="text/javascript">
//music
let audio = new Audio();
let src1 = document.createElement("source");
src1.type = "audio/mpeg";
src1.src = "music.mp3";
audio.appendChild(src1);
audio.play();
//phaser set up
let config = {
type: Phaser.AUTO,
width: 800,
height: 600,
scene: {
preload: preload,
create: create,
update: update
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false
}
}
};
let game = new Phaser.Game(config);
let gameOver = false;
let score = 0;
let scoreText;
/**
* Goes back to study page if study btn clicked
*/
studyBtn.onclick = function(){
//stop audio
audio.pause();
audio.currentTime=0;
location.href = "timer.html";
};
/**
* Restarts game when restart btn clicked
*/
restartBtn.onclick = function(){
startNewGame();
};
/**
* Load necessary images for the game
*/
function preload ()
{
this.load.image('sky', 'images/newSky.png');
this.load.image('ground', 'images/ground.png');
this.load.image('tomato', 'images/tomato.png');
this.load.image('tile', 'images/tilegrass.png');
this.load.spritesheet('character',
'images/rabbit.png',
{ frameWidth: 64, frameHeight: 64 }
);
this.load.spritesheet('coin',
'images/coin.png',
{ frameWidth: 16, frameHeight: 16 })
}
/**
* Create all objects for game
*/
function create ()
{
//add sky
skyBg = this.add.tileSprite(0,250, 1600, 1080, 'sky');
skyBg.setScale(1.5);
//add platforms
platforms = this.physics.add.staticGroup();
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
//add character
player = this.physics.add.sprite(300, 450, 'character');
<<<<<<< HEAD
player.setBounce(0.05);
player.setCollideWorldBounds(true);
player.body.setGravityY(600);
this.anims.create({
key: 'spin',
frames: this.anims.generateFrameNumbers('coin', { start: 0, end: 6 }),
frameRate: 15,
repeat: -1
});
//player's body position when moving
=======
player.setBounce(0.005);
player.setCollideWorldBounds(true);
player.body.setGravityY(400);
//set up character animations when moving
>>>>>>> 5b2c20433e398b348b3154cd36e23996e748f885
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('character', { start: 0, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: [ { key: 'character', frame: 4 } ],
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('character', { start: 5, end: 8 }),
frameRate: 10,
repeat: -1
});
//check for collisions between player and platforms
this.physics.add.collider(player, platforms);
//setup tomatoes
tomatoes = this.physics.add.group();
this.physics.add.collider(tomatoes, platforms);
this.physics.add.collider(player, tomatoes, hitTomato, null, this);
addTomatoes();
//setup coins
this.anims.create({
key: 'spin',
frames: this.anims.generateFrameNumbers('coin', { start: 0, end: 6 }),
frameRate: 15,
repeat: -1
});
coins = this.physics.add.group();
this.physics.add.collider(coins, platforms);
this.physics.add.collider(player, coins, getCoins, null, this);
addCoins();
//score board
scoreText = this.add.text(16,16,'score: 0', {fontSize: '32px', fill:'#201'});
}
/**
* Event for when player collides with coin
* @param {sprite} player
* @param {coin} coin
*/
function getCoins(player, coin)
{
//make coin disppear when player touches it
coin.disableBody(true, true);
//update score
score += 10;
scoreText.setText('score: '+score);
}
/**
* Add coins to screen
*/
function addCoins(){
if(!gameOver){
//random coin y position
let coinY = Math.random() * (550 - 450) + 450;
let coin = coins.create(810, coinY, 'coin')
//delete coin if it goes off screen
if(coin.x < 10){
coin.disableBody(true, true);
}
let timeInterval = Math.floor(Math.random()*7)+4;
setTimeout(addCoins, timeInterval*1000);
}
}
/**
* Event for when player and tomato collide
* @param {sprite} player
*/
function hitTomato (player)
{
gameOver = true;
endGame(player);
}
/**
* Event for when player dies
* @param {sprite} player
*/
function endGame(player){
//get rid of objects on screen
tomatoes.children.iterate(function (child) {
child.disableBody(true, true);
});
coins.children.iterate(function (child) {
child.disableBody(true, true);
});
//make player go off screen
player.setCollideWorldBounds(false);
player.setTint(0xff0000);
}
/**
* Add tomatoes to screen
*/
function addTomatoes(){
if(!gameOver){
//setup tomatoes
let tomato = tomatoes.create(750, 350, 'tomato');
let bounce = Math.random()*1.1;
tomato.setBounce(bounce);
tomato.setGravityY(300);
//delete tomato when it goes off screen
if(tomato.x < 10){
tomato.disableBody(true, true);
}
let timeInterval = Math.floor(Math.random()*8)+5;
setTimeout(addTomatoes, timeInterval*1000);
}
}
/**
* Starts a new game
*/
function startNewGame(){
gameOver = false;
//reset score
score = 0;
scoreText.setText('score: '+score);
//put player back to start position
player.x = 300;
player.y = 450;
player.setTint(0xFFFFFF );
player.setCollideWorldBounds(true);
addTomatoes();
addCoins();
}
/**
* Puts game in action
*/
function update ()
{
//move player with keyboard
cursors = this.input.keyboard.createCursorKeys();
if(!gameOver){
//loop platform
platforms.children.iterate(function (platform) {
if(platform.x == 399){
platforms.create(800, 568, 'ground').setScale(2).refreshBody();
}
})
//move sky background when player goes past midpoint
if(player.x >= 400){
//move background towards player
skyBg.tilePositionX += 1;
platforms.children.iterate(function (platform) {
platform.x += -1;
})
player.x = 399;
}
//move tomato towards player
tomatoes.setVelocityX(-100);
//move star towards player
coins.setVelocityX(-50);
coins.children.iterate(function (coin) {
coin.anims.play('spin', true);
})
//keyboard controls
if (cursors.space.isDown && player.y>500)
{
player.setVelocityY(-300);
}
else if (cursors.right.isDown)
{
player.anims.play('right', true)
player.setVelocityX(100);
}
else if (cursors.left.isDown)
{
player.anims.play('left', true)
player.setVelocityX(-100);
}
else{
player.anims.play('turn', true)
player.setVelocityX(0);
}
}
}
</script>
</body>
</html>