Pong (solo)
Pong っぽい何かを作ってみた。
/**
* Copyright yooyke ( http://wonderfl.net/user/yooyke )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/AhDY
*/
package {
import flash.geom.Matrix3D;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.geom.Point;
import flash.geom.Vector3D;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private const MARGIN:Number = 64;
private const UNIT_LENGTH:Number = 32;
private const BALL_RADIUS:Number = 8;
private const FIRST_SPEED:Number = 2;
private const ACCELERATION:Number = 1.1;
private const FONT_SIZE:Number = 48;
private const CPU_MAX_SPEED:Number = 4;
private var fieldW_:Number;
private var fieldH_:Number;
private var field_:Sprite;
private var p1_:Sprite;
private var p2_:Sprite;
private var ball_:Sprite;
private var ballVec_:Vector3D = new Vector3D();
private var tf1_:TextField;
private var tf2_:TextField;
private var score1_:int;
private var score2_:int;
public function FlashTest() {
// write as3 code here..
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
fieldW_ = 400;
fieldH_ = fieldW_ * (3 / 4);
setupObject(stage.stageWidth, stage.stageHeight, fieldW_, fieldH_);
addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function setupObject(stageW:Number, stageH:Number, w:Number, h:Number):void {
var cW:Number = w / 2;
var cH:Number = h / 2;
field_ = new Sprite();
field_.graphics.beginFill(0);
field_.graphics.drawRect(0, 0, w, h);
field_.x = stageW / 2 - field_.width / 2;
field_.y = stageH / 2 - field_.height / 2;
addChild(field_);
tf1_ = createScore(FONT_SIZE);
tf1_.x = cW - 64 - tf1_.width;
tf1_.y = 8;
field_.addChild(tf1_);
tf2_ = createScore(FONT_SIZE);
tf2_.x = cW + 64;
tf2_.y = 8;
field_.addChild(tf2_);
const grid:Number = 8;
var count:int = (stageH / grid) / 2;
var i:int;
var line:Sprite = new Sprite();
line.mouseEnabled = false;
line.graphics.lineStyle(1, 0xffffff);
for (i = 0; i < count; i++) {
line.graphics.moveTo(cW, i * 2 * grid);
line.graphics.lineTo(cW, ((i * 2) + 1) * grid);
}
field_.addChild(line);
p1_ = createUnit(MARGIN, cH, UNIT_LENGTH);
field_.addChild(p1_);
p2_ = createUnit(w - MARGIN, cH, UNIT_LENGTH);
field_.addChild(p2_);
ball_ = createBall(cW, cH, BALL_RADIUS);
field_.addChild(ball_);
reset(true, true);
}
private function reset(p1:Boolean, all:Boolean=false):void {
ball_.x = fieldW_ / 2 + (p1 ? -16 : 16);
ball_.y = fieldH_ / 2;
ballVec_.x = p1 ? -FIRST_SPEED : FIRST_SPEED;
ballVec_.y = 0;
p2_.y = fieldH_ / 2;
if (all) {
score1_ = 0;
score2_ = 0;
}
}
private function createUnit(x:Number, y:Number, length:Number):Sprite {
var sp:Sprite = new Sprite();
sp.mouseEnabled = false;
sp.graphics.lineStyle(1, 0xffffff);
sp.graphics.moveTo(0, -(length / 2));
sp.graphics.lineTo(0, +(length / 2));
sp.x = x;
sp.y = y;
return sp;
}
private function createScore(size:Number):TextField {
var format:TextFormat = new TextFormat();
format.size = size;
var tf:TextField = new TextField();
tf.selectable = false;
tf.mouseEnabled = false;
tf.defaultTextFormat = format;
tf.textColor = 0xffffff;
tf.text = "00";
tf.width = tf.textWidth + 8;
tf.height = tf.textHeight + 8;
return tf;
}
private function createBall(x:Number, y:Number, radius:Number):Sprite {
var i:int;
var sp:Sprite = new Sprite();
sp.mouseEnabled = false;
sp.graphics.lineStyle(1, 0xffffff);
for (i = 0; i < (radius / 2) + 1; i++) {
sp.graphics.moveTo(-(radius / 2), (i * 2));
sp.graphics.lineTo((radius / 2), (i * 2));
}
sp.x = x;
sp.y = y;
return sp;
}
private function moveP2():void {
var ld2:Number = UNIT_LENGTH / 2;
var act:Number = 1;//ld2;
if (p2_.y <= (ball_.y - act) || (ball_.y + act) <= p2_.y) {
var speed:Number = Math.abs(ball_.y - p2_.y);
if (speed > CPU_MAX_SPEED) {
speed = CPU_MAX_SPEED;
}
if (ball_.y < p2_.y) {
p2_.y -= speed;
}
else if (ball_.y > p2_.y) {
p2_.y += speed;
}
}
}
private function collison(x1:Number, y1:Number, w1:Number, h1:Number, x2:Number, y2:Number, w2:Number, h2:Number):Boolean {
return x1 <= x2 + w2 && x2 <= x1 + w1 && y1 <= y2 + h2 && y2 <= y1 + h1;
}
private function onMouseMove(e:MouseEvent):void {
p1_.y = e.localY;
}
private function onEnterFrame(e:Event):void {
moveP2();
var prex:Number = ball_.x + ballVec_.x;
var prey:Number = ball_.y + ballVec_.y;
var ld2:Number = UNIT_LENGTH / 2;
var rad2:Number = BALL_RADIUS / 2;
const theta:Number = 45;
var rate:Number;
var mat:Matrix3D = new Matrix3D();
if (collison(p1_.x, p1_.y - ld2, 1, UNIT_LENGTH, ball_.x - rad2, ball_.y - rad2, BALL_RADIUS, BALL_RADIUS)) {
rate = ((prey - p1_.y) / (UNIT_LENGTH / 2)) * theta;
mat.appendRotation(rate, Vector3D.Z_AXIS);
ballVec_ = mat.transformVector(new Vector3D(ballVec_.length, 0, 0));
ballVec_.scaleBy(ACCELERATION);
}
else if (collison(p2_.x, p2_.y - ld2, 1, UNIT_LENGTH, ball_.x - rad2, ball_.y - rad2, BALL_RADIUS, BALL_RADIUS)) {
rate = ((prey - p2_.y) / (UNIT_LENGTH / 2)) * theta;
mat.appendRotation(rate, Vector3D.Z_AXIS);
ballVec_ = mat.transformVector(new Vector3D(-ballVec_.length, 0, 0));
ballVec_.scaleBy(ACCELERATION);
}
else if (ballVec_.y < 0 && ball_.y < BALL_RADIUS) {
ballVec_.y = Math.abs(ballVec_.y);
}
else if (ballVec_.y > 0 && (fieldH_ - BALL_RADIUS) < ball_.y ) {
ballVec_.y = -Math.abs(ballVec_.y);
}
if (ballVec_.length > BALL_RADIUS) {
ballVec_.normalize();
ballVec_.scaleBy(BALL_RADIUS);
}
ball_.x += ballVec_.x;
ball_.y += ballVec_.y;
if (prex < -BALL_RADIUS) {
score2_++;
reset(true);
}
else if (prex > fieldW_ + BALL_RADIUS) {
score1_++;
reset(false);
}
tf1_.text = score1_.toString();
tf2_.text = score2_.toString();
}
}
}