circle arcanoid
// forked from h_sakurai's forked from: circle pong
// forked from shinh's circle pong
package {
import flash.display.*
import flash.geom.*
import flash.text.*
import flash.events.*
public class CircleArcanoid extends Sprite {
private var screen:BitmapData
private const W:int = 465, H:int = 465
private var ba:Number = 0, bv:Number = 0
private var cnt:int, noColCnt:int
private var running:Boolean
private var ball:Shape = new Shape()
private var wall:Shape = new Shape()
private var bricks:Vector.<Shape>;
private var score:TextField = new TextField()
private var text:TextField = new TextField()
private var dbg:TextField = new TextField()
private function makeTextField(t:TextField, x:int, y:int, s:int):TextField {
t.x = x
t.y = y
t.width = W
t.height = H
var fm:TextFormat = new TextFormat
fm.color = 0xffffff
fm.font = '_typewriter'
fm.size = s
fm.bold = true
t.defaultTextFormat = fm
return t
}
public function CircleArcanoid() {
addChild(new Bitmap(screen = new BitmapData(W, H, false, 0)))
ball.graphics.beginFill(0xffffff)
ball.graphics.drawCircle(0, 0, 7)
addChild(ball)
wall.graphics.beginFill(0xffffff)
wall.graphics.drawRect(-25, -2, 50, 5)
addChild(wall)
score.text = '0'
addChild(makeTextField(score, 10, 10, 20))
addChild(makeTextField(text, 100, 150, 30))
addChild(makeTextField(dbg, 0, 20, 10))
ball.x = -50
running = true
addEventListener(Event.ENTER_FRAME, update)
addEventListener(MouseEvent.CLICK, init)
}
public function init(event:MouseEvent): void {
if (running) return
running = true
bv = 2
ba = 0
ball.x = W / 2
ball.y = H / 1.5
score.text = '' + (cnt = 0)
text.text = ''
if (bricks) {
while (bricks.length > 0) {
removeChild (bricks.pop ());
}
}
var bcolor:Vector3D = new Vector3D (
Math.random (), Math.random (), Math.random ()
);
bcolor.normalize (); bcolor.scaleBy (2);
bricks = new Vector.<Shape>;
for (var i:int = 0; i < 64; i++) {
var brick:Shape = new Shape;
brick.x = W / 2 + 10 * (int (i % 8) - 4);
brick.y = H / 2 + 10 * (int (i / 8) - 4);
brick.graphics.beginFill (
uint (Math.min (1, bcolor.x) * 255 * 65536) +
uint (Math.min (1, bcolor.y) * 255 * 256) +
uint (Math.min (1, bcolor.z) * 255)
);
brick.graphics.drawRect ( -3, -3, 6, 6);
addChild (brick);
bricks.push (brick);
}
}
public function update(event:Event): void {
var a:Number = Math.atan2(stage.mouseX - W / 2,
stage.mouseY - H / 2)
var bx:Number = ball.x - W / 2
var by:Number = ball.y - H / 2
var da:Number = Math.abs(Math.atan2(bx, by) - a)
if ((noColCnt -= 1) < 0 &&
(da < 0.2 || da > Math.PI * 2 - 0.2) &&
Math.abs(Math.sqrt(bx*bx + by*by) - 200) < 10) {
ba = Math.PI - ba + a * 2
bv += 0.3
noColCnt = 10
}
wall.rotation = - a / 3.141592 * 180
if (bricks != null)
for (var i:int = 0; i < bricks.length; i++) {
var brick:Shape = bricks [i];
if (ball.hitTestObject (brick)) {
ba += Math.random () - Math.random ();
brick.parent.removeChild (brick);
score.text = '' + (cnt += 1);
bricks.splice (i, 1); i--;
}
}
ball.x += bv * Math.sin(ba)
ball.y += bv * Math.cos(ba)
wall.x = W / 2 + Math.sin(a) * 200
wall.y = H / 2 + Math.cos (a) * 200
var win:Boolean = (bricks != null) && (bricks.length == 0);
if ((ball.x < 0 || ball.x > W || win ||
ball.y < 0 || ball.y > H) && running) {
text.text = ' Arcanoid\n\n\n GAME OVER\n (YOU ' + (win ? 'WIN:' : 'LOSE') + ')\nCLICK TO START'
running = false
}
}
}
}