ActionScriptPong

今までの練習があまりにもPongを連想させる物だったので書いてみた。

キーイベント取得のコードも書けて良かったかと。

CPU対戦無し、ゲームオーバー無しの無限ループ。

2P対戦でキー同時押しとかすると効かなくなったりするのでそれはそれで面白いが、ゲーム的には如何な物かと。

http://good-morning-world.com/swf/pong/

pong.as

package {
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	
	[SWF(width = 400, height = 300)]
	public class pong extends Sprite {
		private var flag:Boolean = false;
		private var dx:int = 4;				// 球移動量
		private var dy:int = 4;
		private var bary:int = 5;			// バー移動量
		private var lscore:int = 0;			// スコア
		private var rscore:int = 0;
		private var wait:int = 30;			// ウェイト
		
		private var base:Sprite = new Sprite();
		private var ball:Sprite = new Sprite();
		private var left:Sprite = new Sprite();
		private var right:Sprite = new Sprite();
		private var tfield:TextField = new TextField();
		private var format:TextFormat = new TextFormat();
		private var timer:Timer = new Timer(wait, 0);
		
		public function pong() {
			base.graphics.beginFill(0x000000);
			base.graphics.drawRect(0, 0, 400, 300);
			base.graphics.endFill();
			addChild(base);
			
			var str:String = "Click to Start!\n\n";
				str += "1P : [Q],[A]キー\n2P : [P],[;]キー"
			tfield.text = str;
			tfield.x = 160;
			tfield.y = 10;
			format.color = 0xFFFFFF;
			tfield.setTextFormat(format);
			addChild(tfield);
			
			ball.graphics.beginFill(0xFFFFFF);
			ball.graphics.drawCircle(0, 0, 5);
			ball.graphics.endFill();
			
			left.graphics.beginFill(0xFFFFFF);
			left.graphics.drawRect(0, 0, 10, 80);
			left.graphics.endFill();
			left.x = 10;
			left.y = 110;
			addChild(left);
			
			right.graphics.beginFill(0xFFFFFF);
			right.graphics.drawRect(0, 0, 10, 80);
			right.graphics.endFill();
			right.x = 380;
			right.y = 110;
			addChild(right);
			
			base.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			base.addEventListener(MouseEvent.CLICK, onMouseClick);
			
			timer.addEventListener(TimerEvent.TIMER, onTick);
			timer.start();
		}
		
		private function onTick(event:TimerEvent):void {
			// 球移動
			if(flag) {
				ball.x += dx;
				ball.y += dy;
			}
			
			// バー当たり判定
			if (ball.x > 376 && ball.x <= 380) {
				if (right.y + 85 > ball.y && right.y < ball.y + 5) {
					dx = -dx;
				}
			} else if (ball.x < 24 && ball.x >= 20) {
				if (left.y + 85 > ball.y && left.y < ball.y+ 5) {
					dx = -dx;
				}
			}
			
			// 上下当たり判定
			if (ball.y > 296 && ball.y <= 300) {
				dy = -dy;
			} else if (ball.y >= 0 && ball.y < 4) {
				dy = -dy;
			}
			
			// 点が入った時
			if (ball.x > 420) {
				lscore++;
				ball.x = 100;
				ball.y = 140;
				tfield.text = lscore + " - " + rscore;
				tfield.setTextFormat(format);
			} else if (ball.x < -25) {
				rscore++;
				ball.x = 300;
				ball.y = 140;
				tfield.text = lscore + " - " + rscore;
				tfield.setTextFormat(format);
			}
		}
		
		private function onKeyDown(event:KeyboardEvent):void {
			// バー移動
			if (event.keyCode == 81) {
				left.y -= bary;
				if (left.y < 0) { left.y = 0; }
			}
			if (event.keyCode == 65) {
				left.y += bary;
				if (left.y > 220) { left.y = 220; }
			}
			if (event.keyCode == 80) {
				right.y -= bary;
				if (right.y < 0) { right.y = 0; }
			}
			if (event.keyCode == 187) {
				right.y += bary;
				if (right.y > 220) { right.y = 220; }
			}
		}
		
		private function onMouseClick(event:MouseEvent):void {
			flag = true;
			
			// フォーカス移動
			stage.focus = base;
			
			// 球セット
			ball.x = 100;
			ball.y = 140;
			addChild(ball);
			
			// 得点セット
			tfield.x = 190;
			tfield.y = 10;
			tfield.text = lscore + " - " + rscore;
			tfield.setTextFormat(format);
		}
	}
}