oc2019

改造のヒント

敵の動きを変える

class Enemy extends Actor {
	*main() {
		this.p=$pat_neko+44;
		this.cy=this.y;//追加:往復する中心の座標
		while(this.x>0) {
			this.x+=this.vx;
			this.y=this.cy+Math.sin($t)*10;//変更:三角関数で往復させる
			if (this.rnd(100)<1) {
			    new EnemyBullet({x:this.x, y:this.y});
			}
			yield;
		}
		this.die();
	}
} 

やってみよう

敵の形をランダムに変更する

class Enemy extends Actor {
	*main() {
		this.p=$pat_neko+44;//★この部分を変更
class Enemy extends Actor {
	*main() {
               this.n=this.rnd(2);// 0または1がthis.nに入る
               if (this.n==0) this.p=$pat_neko+44;// 0だったら
               if (this.n==1) this.p=$pat_neko+45;// 1だったら

やってみよう

弾の発射する向きを変える

プレイヤーの動作を変更

class Player extends Actor {
	*main() {
		this.p=$pat_neko+45;
		this.count=0;
		while (true) {
			this.x=$mouseX;
			this.y=$mouseY;
			if (this.count==30) {
				new PlayerBullet({x:this.x, y:this.y, vx:10,vy:3});//変更
				//★
				this.count=0;
			}
			this.count+=1;
			if (this.crashTo(Enemy) || this.crashTo(EnemyBullet)) {
				break;
			}
			yield;
		}
	}
}

プレイヤーの弾の動作も変更

class PlayerBullet extends Actor {
	*main() { 
		this.p=$pat_neko+48;
		this.vx=10;
		while (this.x<400) {            
			this.x+=this.vx;
			this.y+=this.vy;//追加
			var c=this.crashTo(Enemy)
			if (c) {
				$score++;
				c.die();
				break;
			}
			yield;
		}
		this.die();
	}
} 

やってみよう

プレイヤーの方向に発射する敵弾

class EnemyBullet extends Actor {
	*main() {
		this.p=$pat_neko+49;
		// -- ここから追加
		if (this.y>$player.y) {
		   this.vy=-1;
		} else {
		   this.vy=1;
		}
		// -- ここまで追加
		while(this.x>0) {
			this.x-=10;
			this.y+=this.vy;//追加
			yield;
		}
		this.die();
	}
}

やってみよう

ゲームオーバー

プレイヤーが敵に触れたとき「ゲームオーバー」というメッセージを 表示し,スペースキーが押されたか画面をタッチされたらもう一度ゲームを始める.

※「タイトル画面」を作っている人は,ゲームオーバー後にタイトル画面に移動する方法を考えてみよう

class Player extends Actor {
	*main() {
		this.p=$pat_neko+45;
		this.count=0;
		while (true) {
			this.x=$mouseX;
			this.y=$mouseY;
			if (this.count==30) {
				new PlayerBullet({x:this.x, y:this.y});
				this.count=0;
			}
			this.count+=1;
			if (this.crashTo(Enemy) || this.crashTo(EnemyBullet)) {
				break;//ゲームオーバーになると★に移動
			}
			yield;
		}
		//-----★ここを追加
		new Label({x:100,y:100,text:"ゲームオーバー"});
		this.p=-1;// キャラクタを消す
		while (true) {
		   if(this.getkey("space")==1 || $touches[0].touched==1){
		        this.loadPage(Start);
		   }
		   yield;
		}
		//--ここまで
		//this.die();
	}
}

タイトル画面

新しく Titleというクラスを作成し、次のように書く

class Title extends Actor {
	*main() {
		new Actor({x:$screenWidth/2,y:$screenHeight/2,text:"ゲームのタイトル"});
		while(true){
		    if(this.getkey("space")==1 || $touches[0].touched==1){
		        this.loadPage(Start);
		    }
		    yield;
		}
	}
}  

さらに、一番最後の行の

setMain(Start);

setMain(Title);

に変更する。

やってみよう


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS