TouchAndDie
TouchAndDie.as
Control a red box, touch green box and die.
<Operation>
Movement: Mouse
// forked from ABA's WallAll
// TouchAndDie.as
// Control a red box, touch green box and die.
// <Operation>
// Movement: Mouse
package {
import flash.display.Sprite;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="30")]
public class TouchAndDie extends Sprite { public function TouchAndDie() { main = this; initialize(); } }
}
import flash.display.*;
import flash.geom.*;
import flash.text.*;
import flash.events.*;
const SCREEN_WIDTH:int = 465, SCREEN_HEIGHT:int = 465, GAME_OVER_DURATION:int = 150, BLOCK_GAME_START_DURATION:int = 30;
var main:Sprite, screen:BitmapData = new BitmapData(SCREEN_WIDTH, SCREEN_HEIGHT, false, 0);
var scoreField:TextField = new TextField, messageField:TextField = new TextField;
var gameOverTicks:int, score:int, boxTicks:int, isMouseClicked:Boolean, rate:int;
// Initialize UIs.
function initialize():void {
main.addChild(new Bitmap(screen));
main.stage.addEventListener(MouseEvent.CLICK, function(e:Event):void { isMouseClicked = true; } );
scoreField = createTextField(SCREEN_WIDTH - 100, 0, 100, 24, 0xff6666, TextFormatAlign.RIGHT);
messageField = createTextField(SCREEN_WIDTH - 256, 0, 256, 36, 0xff6666);
main.addChild(scoreField); main.addChild(messageField);
startTitle(); main.addEventListener(Event.ENTER_FRAME, update);
}
// Update the game frame.
function update(event:Event):void {
screen.lock(); screen.fillRect(screen.rect, 0x000000);
boxTicks--; if (boxTicks <= 0 && (gameOverTicks < 0 || gameOverTicks >= GAME_OVER_DURATION)) addBox();
for (var i:int = 0; i < boxs.length; i++) boxs[i].update();
for (i = 0; i < bullets.length; i++)
if (!bullets[i].update()) { bullets.splice(i, 1); i--; score += rate; scoreField.text = String(score);}
if (gameOverTicks < 0) Player.update();
else if (gameOverTicks < GAME_OVER_DURATION) Player.draw();
screen.unlock();
if (gameOverTicks >= 0) {
gameOverTicks++; if (gameOverTicks == GAME_OVER_DURATION) startTitle();
if (isMouseClicked && gameOverTicks > BLOCK_GAME_START_DURATION) startGame();
if (boxs.length > score / 100 + 3) shootBullets();
}
}
// Boxs.
const WALL_WIDTH:Number = 20.0;
const WALL_HEIGHT:Number = 20.0;
const WALL_VEL:Number = 10.0;
var boxs:Vector.<Box>;
class Box {
public var rect:Rectangle = new Rectangle;
public var vx:Number, vy:Number;
public function update():void {
if (gameOverTicks < 0 || gameOverTicks >= GAME_OVER_DURATION) {
rect.x += vx; rect.y += vy;
}
screen.fillRect(rect, 0x66ff66);
if (rect.x < 0 || rect.x > SCREEN_WIDTH - WALL_WIDTH) vx = -vx;
if (rect.y < 0 || rect.y > SCREEN_HEIGHT - WALL_HEIGHT) vy = -vy;
}
}
function addBox():void {
var w:Box = new Box;
w.vx = rand() * WALL_VEL - WALL_VEL / 2;
w.vy = rand() * WALL_VEL / 2 + 1.0;
w.rect.width = WALL_WIDTH; w.rect.height = WALL_HEIGHT;
w.rect.x = 10 + rand() * (SCREEN_WIDTH - 20);
w.rect.y = 0;
boxs.push(w); boxTicks = (30 + rand() * 30) * 4000 / (4000 + score);
}
// Bullet.
const BULLET_WIDTH:Number = 10.0;
const BULLET_HEIGHT:Number = 10.0;
const BULLET_VEL:Number = 10.0;
const BULLET_VEL_S:Number = 7.1;
const BULLET_VX_LIST:Array = [ BULLET_VEL, BULLET_VEL_S, 0.0, -BULLET_VEL_S ];
const BULLET_VY_LIST:Array = [ 0.0, BULLET_VEL_S, BULLET_VEL, BULLET_VEL_S ];
var bullets:Vector.<Bullet>;
class Bullet {
public var rect:Rectangle = new Rectangle;
public var vx:Number, vy:Number;
public function update():Boolean {
if (gameOverTicks < 0 || gameOverTicks >= GAME_OVER_DURATION) {
rect.x += vx; rect.y += vy;
}
screen.fillRect(rect, 0xff66ff);
return screen.rect.contains(rect.x, rect.y);
}
}
function shootBullets():void {
var vx:Number, vy:Number;
for (var i:int = 0; i < boxs.length; i++) {
for (var j:int = 0; j < 4; j++){
vx = BULLET_VX_LIST[j];
vy = BULLET_VY_LIST[j];
addBullet(boxs[i].rect.x, boxs[i].rect.y, vx, vy);
addBullet(boxs[i].rect.x, boxs[i].rect.y, -vx, -vy);
}
}
boxs = null; boxs = new Vector.<Box>;
}
function addBullet(x:int, y:int, vx:Number, vy:Number):void {
var b:Bullet = new Bullet;
b.rect.x = x; b.rect.y = y;
b.rect.width = BULLET_WIDTH; b.rect.height = BULLET_HEIGHT;
b.vx = vx * 16 / (16 + boxs.length); b.vy = vy * 16 / (16 + boxs.length);
bullets.push(b);
}
// Player.
class Player {
public static var pos:Vector3D = new Vector3D, prevPos:Vector3D = new Vector3D;
public static function update():void {
prevPos.x = pos.x; prevPos.y = pos.y;
pos.x = main.stage.mouseX; pos.y = main.stage.mouseY;
draw();
var cx:Number = pos.x, cy:Number = pos.y, ox:Number = prevPos.x - pos.x, oy:Number = prevPos.y - pos.y;
ox /= 9; oy /= 9;
for (var i:int = 0; i < 10; i++, cx += ox, cy += oy)
for (var j:int = 0; j < boxs.length; j++) {
if (boxs[j].rect.contains(cx, cy)) {
boxs.splice(j, 1); j--;
rate = boxs.length;
shootBullets(); break;
}
}
for each (var b:Bullet in bullets) if (b.rect.contains(cx, cy)) {
startGameOver(); pos.x = cx; pos.y = cy; return;
}
}
public static function draw():void {
rect.x = pos.x - 5; rect.y = pos.y - 5; rect.width = rect.height = 11;
screen.fillRect(rect, 0xff4444);
}
}
// Handle the game lifecycle.
function startTitle():void {
clearActors(); gameOverTicks = GAME_OVER_DURATION; isMouseClicked = false;
messageField.y = SCREEN_HEIGHT / 3; messageField.text = "TouchAndDie";
}
function startGame():void {
clearActors(); gameOverTicks = -1; messageField.text = ""; score = 0; scoreField.text = "0";
boxTicks = 0; Player.pos.x = main.stage.mouseX; Player.pos.y = main.stage.mouseY;
}
function startGameOver():void {
gameOverTicks = 0; isMouseClicked = false; rate = 0;
messageField.y = SCREEN_HEIGHT / 2; messageField.text = "GAME OVER";
}
function clearActors():void {
boxs = null; boxs = new Vector.<Box>;
bullets = null; bullets = new Vector.<Bullet>;
}
// Utility functions and variables.
var rect:Rectangle = new Rectangle, rand:Function = Math.random;
function createTextField(x:int, y:int, width:int, size:int, color:int, align:String = TextFormatAlign.LEFT):TextField {
var fm:TextFormat = new TextFormat, fi:TextField = new TextField;
fm.font = "_typewriter"; fm.bold = true; fm.size = size; fm.color = color; fm.align = align;
fi.defaultTextFormat = fm; fi.x = x; fi.y = y; fi.width = width; fi.selectable = false;
return fi;
}