JavaScript/tmlib.js/sample/避けゲー(開始, 終了画面あり)
phi氏の作成されたシューティングゲームのサンプルアプリです。jsdo.itのコード画面は小さくて見にくいので引用させていただいてます。オリジナルはこちらです。
#pre{{
// forked from phi's "避けゲー - tmlib.js #techbuzz" http://jsdo.it/phi/gEhZ
/*
* 定数
*/
var SCREEN_WIDTH = 465;
var SCREEN_HEIGHT = 465;
var SCREEN_CENTER_X = SCREEN_WIDTH/2;
var SCREEN_CENTER_Y = SCREEN_HEIGHT/2;
var PLAYER_SIZE = 32;
/*
* グローバル変数
*/
var app = null;
var enemyList = null;
/*
* プレイヤー
*/
var Player = tm.createClass({
superClass: tm.app.TriangleShape,
// 初期化
init: function() {
this.superInit(PLAYER_SIZE, PLAYER_SIZE);
},
// 更新
update: function(app) {
var p = app.pointing;
// マウスダウン or タッチ判定
if (p.getPointing()) {
// 移動
this.x += app.pointing.deltaPosition.x*1.25;
// 画面内制御
if (this.x < 0) { this.x = 0; }
else if (this.x > SCREEN_WIDTH) { this.x = SCREEN_WIDTH; }
}
}
});
/*
* エネミー
*/
var Enemy = tm.createClass({
superClass: tm.app.StarShape,
// 初期化
init: function() {
this.superInit(PLAYER_SIZE, PLAYER_SIZE);
},
// 更新
update: function(app) {
this.y += 8; // 移動
this.rotation += 15; // 回転
// 削除判定
if (this.y > SCREEN_HEIGHT) {
this.remove();
enemyList.erase(this);
}
}
});
var TitleScene = tm.createClass({
superClass: tm.app.TitleScene,
init: function() {
this.superInit({
title: "避けゲー - tmlib.js",
width: SCREEN_WIDTH,
height: SCREEN_HEIGHT
});
},
onnextscene: function() {
var mainScene = new MainScene();
app.replaceScene(mainScene);
}
});
var ResultScene = tm.createClass({
superClass: tm.app.ResultScene,
init: function() {
this.superInit({
score: app.frame,
msg: "Game Over!",
width: SCREEN_WIDTH,
height: SCREEN_HEIGHT
});
},
onnextscene: function() {
app.replaceScene(TitleScene());
}
});
var MainScene = tm.createClass({
superClass: tm.app.Scene,
init: function() {
this.superInit();
enemyList = [];
// プレイヤー
this.player = Player();
this.addChild(this.player);
this.player.x = SCREEN_CENTER_X;
this.player.y = SCREEN_HEIGHT-100;
},
update: function() {
// エネミー生成
if (app.frame % 8 == 0) {
// エネミー
var enemy = Enemy();
enemy.x = tm.util.Random.randint(0, SCREEN_WIDTH);
enemy.y = -50;
this.addChild(enemy); // シーンに追加
enemyList.push(enemy); // リストに追加
}
// プレイヤー, エネミー衝突判定
for (var i=0,len=enemyList.length; i<len; ++i) {
var enemy = enemyList[i];
// 衝突判定
if (this.player.isHitElement(enemy)) {
// Game Over
app.replaceScene(ResultScene());
}
}
}
});
/*
* メイン処理(ページ読み込み後に実行される)
*/
tm.main(function() {
// アプリケーション作成
app = tm.app.CanvasApp("#world");
app.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
app.fitWindow(); // リサイズ対応
// 開始シーン
app.replaceScene(TitleScene());
// 実行
app.run();
});
}}