/
game.js
200 lines (181 loc) · 8.25 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
enchant();
var game;
window.onload = function() {
game = new Game(640, 640);
game.preload('./background.jpg');
game.preload('./background.jpg');
game.preload('./enchant.js-builds-0.8.0/images/monster/bigmonster1.gif');
game.preload('./enchant.js-builds-0.8.0/images/end.png');
game.preload('./enchant.js-builds-0.8.0/images/icon0.png');
game.preload('./enchant.js-builds-0.8.0/images/space0.png');
game.preload('./enchant.js-builds-0.8.0/images/effect0.png');
var DropFrequency = 15;
var DropVelosity = 8;
game.onload = function() {
var mainScene = new Scene();
var background = new Sprite(mainScene.width, mainScene.height);
background.image = game.assets['./background.jpg'];
mainScene.addChild(background);
// スコアを入れる変数を用意する
game.score = 1;
// スコアを表示するラベルを作成
var scoreLabel = new Label("SCORE : 0");
scoreLabel.font = "16px Tahoma";
scoreLabel.color = "white";
scoreLabel.x = 10; // X座標
scoreLabel.y = 5; // Y座標
mainScene.addChild(scoreLabel);
var dragon = new Sprite(80, 80);
dragon.image = game.assets['./enchant.js-builds-0.8.0/images/monster/bigmonster1.gif'];
dragon.x = mainScene.width / 2;
dragon.y = mainScene.height - dragon.height - 10;
dragon.frame = 3;
mainScene.addChild(dragon);
dragon.frameIndex = 0;
var frameList = [2, 3, 4, 3];
var gameFreezeTime = 0;
dragon.onenterframe = function() {
gameFreezeTime = Math.max(gameFreezeTime - 1, 0);
if (gameFreezeTime > 0) {
if (gameFreezeTime == 1) {
this.frame = frameList[this.frameIndex];
}
return;
}
//ドラゴンをアニメーションさせる
if (game.frame % 10 === 0) {
this.frameIndex++;
this.frameIndex %= frameList.length;
this.frame = frameList[this.frameIndex];
}
if (game.input.right) {
dragon.x += 10;
dragon.x = Math.min(mainScene.width - dragon.width, dragon.x);
dragon.scaleX = -1;
}
if (game.input.left) {
dragon.x -= 10;
dragon.x = Math.max(0, dragon.x);
dragon.scaleX = 1;
}
};
game.onenterframe = function() {
if (gameFreezeTime > 0) {
return;
}
if (game.frame % DropFrequency === 0) {
var dropItem;
if (game.frame % (DropFrequency * 10) === 0) {
dropItem = new Sprite(32, 64);
dropItem.image = game.assets['./enchant.js-builds-0.8.0/images/space0.png'];
dropItem.scaleX = 1;
dropItem.scaleY = -1;
dropItem.isMissile = true;
} else {
dropItem = new Sprite(16, 16);
dropItem.image = game.assets['./enchant.js-builds-0.8.0/images/icon0.png'];
dropItem.scaleX = 2;
dropItem.scaleY = 2;
dropItem.isMissile = false;
}
dropItem.x = Math.random() * game.width;
dropItem.y = 0;
var rand = Math.floor(Math.random() * 10);
dropItem.frame = rand;
dropItem.onenterframe = function() {
if (gameFreezeTime > 0) {
return;
}
this.y += DropVelosity;
if (this.within(dragon, 40)) {
this.remove();
if (this.frame !== 0) {
// 1以外をとった時
dragon.frame = 0;
gameFreezeTime = 30;
// ミサイルがぶつかった時
if (this.isMissile) {
game.score = 0;
scoreLabel.text = "SCORE : " + game.score;
var effect = new Sprite(16, 16);
effect.image = game.assets['./enchant.js-builds-0.8.0/images/effect0.png'];
effect.x = this.x;
effect.y = this.y;
effect.scaleX = 3;
effect.scaleY = 3;
effect.frameIndex = 0;
var frameList = [0, 1, 2, 3, 4];
mainScene.addChild(effect);
effect.onenterframe = function() {
if (game.frame % 3 === 0) {
this.frameIndex++;
if (this.frameIndex == 5) {
this.remove();
return;
}
this.frame = frameList[this.frameIndex];
}
};
} else {
game.score = Math.max(0, --game.score);
scoreLabel.text = "SCORE : " + game.score;
var effect = new Sprite(16, 16);
effect.image = game.assets['./enchant.js-builds-0.8.0/images/icon0.png']
effect.x = this.x;
effect.y = this.y;
effect.scaleX = 3;
effect.scaleY = 3;
effect.frame = 11;
effect.lifeTime = 0;
mainScene.addChild(effect);
effect.onenterframe = function() {
if (this.lifeTime % 3) {
this.y -= 3;
}
if (this.lifeTime == 18) {
if(game.score <= 0) {
game.pushScene(setupEndingScene());
}
this.remove();
}
this.lifeTime++;
}
}
} else {
// 1をとった時
game.score = Math.max(0, ++game.score);
scoreLabel.text = "SCORE : " + game.score;
var effect = new Sprite(16, 16);
effect.image = game.assets['./enchant.js-builds-0.8.0/images/icon0.png']
effect.x = this.x;
effect.y = this.y;
effect.scaleX = 3;
effect.scaleY = 3;
effect.frame = 10;
effect.lifeTime = 0;
mainScene.addChild(effect);
effect.onenterframe = function() {
if (this.lifeTime % 3) {
this.y -= 3;
}
if (this.lifeTime == 18) {
this.remove();
if( game.score === 0) {
game.Scene.pushScene(endingScene);
}
}
this.lifeTime++;
};
}
}
if (this.y > mainScene.height) {
this.remove();
}
}
mainScene.insertBefore(dropItem, dragon);
}
}
game.pushScene(mainScene);
}
game.start();
}