-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
100 lines (100 loc) · 3.05 KB
/
Copy pathgame.js
File metadata and controls
100 lines (100 loc) · 3.05 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
window.onload = function() {
var game = new Phaser.Game(500, 900, Phaser.CANVAS, "", {
preload: onPreload,
create: onCreate
});
var sumsArray = [];
var questionText;
var randomSum;
var timeTween;
var numberTimer;
var buttonMask;
var score=0;
var scoreText;
var isGameOver = false;
var topScore;
var numbersArray = [-3,-2,-1,1,2,3];
function buildThrees(initialNummber,currentIndex,limit,currentString){
for(var i=0;i<numbersArray.length;i++){
var sum = initialNummber+numbersArray[i];
var outputString = currentString+(numbersArray[i]<0?"":"+")+numbersArray[i];
if(sum>0 && sum<4 && currentIndex==limit){
sumsArray[limit][sum-1].push(outputString);
}
if(currentIndex<limit){
buildThrees(sum,currentIndex+1,limit,outputString);
}
}
}
function onPreload() {
game.load.image("timebar", "timebar.png");
game.load.image("buttonmask", "buttonmask.png");
game.load.spritesheet("buttons", "buttons.png",400,50);
}
function onCreate() {
topScore = localStorage.getItem("topScore")==null?0:localStorage.getItem("topScore");
game.stage.backgroundColor = "#cccccc";
game.stage.disableVisibilityChange = true;
for(var i=1;i<5;i++){
sumsArray[i]=[[],[],[]];
for(var j=1;j<=3;j++){
buildThrees(j,1,i,j);
}
}
questionText = game.add.text(250,160,"-",{
font:"bold 72px Arial"
});
questionText.anchor.set(0.5);
scoreText = game.add.text(10,10,"-",{
font:"bold 24px Arial"
});
for(var i=0;i<3;i++){
var numberButton = game.add.button(50,250+i*75,"buttons",checkAnswer,this).frame=i;
}
numberTimer = game.add.sprite(50,250,"timebar");
nextNumber();
}
function gameOver(gameOverString){
game.stage.backgroundColor = "#ff0000";
questionText.text = questionText.text+" = "+gameOverString;
isGameOver = true;
localStorage.setItem("topScore",Math.max(score,topScore));
}
function checkAnswer(button){
if(!isGameOver){
if(button.frame==randomSum){
score+=Math.floor((buttonMask.x+350)/4);
nextNumber();
}
else{
if(score>0) {
timeTween.stop();
}
gameOver(button.frame+1);
}
}
}
function nextNumber(){
scoreText.text = "Score: "+score.toString()+"\nBest Score: "+topScore.toString();
if(buttonMask){
buttonMask.destroy();
game.tweens.removeAll();
}
buttonMask = game.add.graphics(50, 250);
buttonMask.beginFill(0xffffff);
buttonMask.drawRect(0, 0, 400, 200);
buttonMask.endFill();
numberTimer.mask = buttonMask;
if(score>0){
timeTween=game.add.tween(buttonMask);
timeTween.to({
x: -350
}, 3000, "Linear",true);
timeTween.onComplete.addOnce(function(){
gameOver("?");
}, this);
}
randomSum = game.rnd.between(0,2);
questionText.text = sumsArray[Math.min(Math.round((score-100)/400)+1,4)][randomSum][game.rnd.between(0,sumsArray[Math.min(Math.round((score-100)/400)+1,4)][randomSum].length-1)];
}
}