Particle Crush
パーティクルの勉強ついでにゲームっぽいものを。
少しリファクタリングしました
package
{
import flash.display.Bitmap;
import flash.display.SimpleButton;
import flash.events.*;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.*;
/**
* ...
* @author inu
*/
/*
パーティクルの勉強ついでにゲームっぽいものを。
少しリファクタリングしました
*/
public class Main extends Sprite
{
public static var score:int;
public static var combo:int;
private var player:Player;
private var enemyV:Vector.<Enemy>;
private var numberV:Vector.<NParticle>;
private var canvas:Bitmap;
private var cnt:int;
private var blankBmd:BitmapData;
public static var tf:TextField;
public var startBtn:Sprite;
[SWF(width = "465", height = "465", frameRate = "30")]
public function Main():void
{
var i:int;
blankBmd = new BitmapData(465, 465, true, 0xffffffff);
// entry point
enemyV = new Vector.<Enemy>();
numberV = new Vector.<NParticle>();
canvas = new Bitmap();
addChild(canvas);
player = new Player(230, 300);
tf = new TextField();
tf.selectable = false;
tf.y = 180;
tf.width = 465;
tf.autoSize = TextFieldAutoSize.CENTER;
addChild(tf);
startBtn = new Sprite();
startBtn.y = 220;
var startTf:TextField = new TextField;
startTf.selectable = false;
startTf.text = "start";
startTf.background = true;
startTf.border = true;
startTf.width = 465;
startTf.autoSize = TextFieldAutoSize.CENTER;
startTf.addEventListener(MouseEvent.CLICK, start);
startBtn.addChild(startTf);
addChild(startBtn);
stage.addEventListener(MouseEvent.CLICK, click);
addEventListener(Event.ENTER_FRAME, ent);
}
private function start(e:MouseEvent):void {
enemyCnt = 24;
score = 0;
}
private function click(e:MouseEvent):void {
combo = 0;
player.isMoving = true;
player.dx = e.stageX;
player.dy = e.stageY;
}
private var enemyCnt:int = 0;
private function ent(e:Event):void {
var i:int,j:int;
cnt++;
tf.text = ""+score;
var enemy:Enemy;
if (enemyCnt > 0 && startBtn.y >= -50) {
startBtn.y -= (startBtn.y + 50) / 4;
}
if (enemyCnt < 1) {
if(enemyV.length == 0 && startBtn.y<=220) startBtn.y += (220-startBtn.y ) / 4;
}
else {
if (cnt % 300 == 0) enemyCnt-=1+enemyCnt/20+enemyCnt/10
if (cnt % enemyCnt == 0) {
var r1:int = int(Math.random() * 10 + 4) * 8;
var r2:int = int(Math.random() * 10 + 4) * 8;
enemy = new Enemy(Math.random() * (455 - r1)+5, -r2, r1, r2);
enemyV.push(enemy);
}
}
//移動
for (i = 0; i < enemyV.length; i++ ) {
if (enemyV[i].parts.length == 0) {
enemyV[i] = null;
enemyV.splice(i, 1);
}
else {
if (enemyV[i].alive) enemyV[i].move();
else enemyV[i].drop();
}
}
for (i = 0; i < numberV.length; i++ ) {
numberV[i].move();
if (numberV[i].y > 465) {
score += 20;
numberV[i] = null;
numberV.splice(i, 1);
}
}
player.move();
//描写
var bmd:BitmapData = blankBmd.clone();
bmd.lock();
for (i = 0; i < enemyV.length; i++ ) {
if (enemyV[i].alive) {
bmd.copyPixels(enemyV[i].parts[0].bmd, new Rectangle(0, 0, enemyV[i].width, enemyV[i].height), new Point( enemyV[i].x, enemyV[i].y));
}
else {
for (j = 0; j < enemyV[i].parts.length; j++ ) {
bmd.fillRect(enemyV[i].parts[j], enemyV[i].parts[j].color);
}
}
}
for (i = 0; i < numberV.length; i++ ) {
bmd.copyPixels(numberV[i].bmd, new Rectangle(0, 0, 12, 24), new Point(numberV[i].x, numberV[i].y), null, null, true);
}
for (i = player.parts.length-1; i >=0 ; i-- ) {
bmd.fillRect(player.parts[i], player.parts[i].color);
}
bmd.unlock();
canvas.bitmapData = bmd;
for (i = 0; i < player.hitRect.length; i++ ) for (j = 0; j < enemyV.length; j++ ) {
if (!enemyV[j].alive) continue;
if (player.hitRect[i].intersects(enemyV[j])) {
enemyV[j].dead();
if (combo < 9) combo++;
if (!player.isMoving) combo = 0;
for (var k:int = 0; k < combo;k++ ){
numberV.push(new NParticle(player.parts[0].x, player.parts[0].y, combo));
}
}
}
}
}
}
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Rectangle;
class Particle extends Rectangle{
public var vx:Number;
public var vy:Number;
public var color:uint;
public var bmd:BitmapData;
public function Particle(x:Number, y:Number, w:int,h:int,color:uint=0xFF000000,isParts:Boolean=false) {
this.x = x;
this.y = y;
this.color = color;
if (w < 1) w = 1;
if (h < 1) h = 1;
this.width = w;
this.height = h;
if (!isParts) bmd = new BitmapData(w, h, true, color);
else {
vx = Math.random() * 20 - 5;
vy = -Math.random() * 15;
bmd = null;
}
}
}
class Player{
private var w:Number = 0;
private var h:Number = 0;
public var dx:Number = 0;
public var dy:Number = 0;
public var parts:Vector.<Particle>;
public var hitRect:Vector.<Rectangle>;
public var isMoving:Boolean = false;
public function Player(x:Number=0,y:Number=0,w:Number=5,h:Number=5,r:Number=0){
dx = x;
dy = y;
this.w = w;
this.h = h;
parts = new Vector.<Particle>();
for (var i:int = 0; i < 40; i++ ) {
var p:Particle = new Particle(x + i / 40, y + i / 40, w - i / 20, h - i / 20, (255-i*5)*0x1000000);
parts.push(p);
}
}
public function move():void {
var i:int, j:int;
hitRect = null;
hitRect = new Vector.<Rectangle>();
var pastX:Number = parts[parts.length-1].x;
var pastY:Number = parts[parts.length - 1].y;
if (Math.abs((dx - parts[0].x) / 3) < 0.05) isMoving = false;
for (i = 0; i < parts.length; i++ ) {
var vx:Number = (dx + (w - parts[i].width) / 2 - parts[i].x) / (3 + i / 10);
var vy:Number = (dy + (h- parts[i].height) / 2 - parts[i].y) / (3 + i / 10);
parts[i].x += vx;
parts[i].y += vy;
}
var absX:int = Math.abs(pastX - parts[0].x) / 5 + 1;
var absY:int = Math.abs(pastY - parts[0].y) / 5 + 1;
var sX:Number = (pastX - parts[0].x) / absY;
var sY:Number = (pastY - parts[0].y) / absX;
if (Math.abs(pastX - parts[0].x) > Math.abs(pastY - parts[0].y)) {
for (i = 0; i < absX; i++ ) {
hitRect.push(new Rectangle(pastX + i * ((sX<0)?5:-5), pastY - i * sY, 5, 5));
}
}else {
for (i = 0; i < absY; i++ ) {
hitRect.push(new Rectangle(pastX - i * sX, pastY + i * ((sY<0)?5:-5), 5, 5));
}
}
}
}
class Enemy extends Rectangle{
public var vx:Number = 0;
public var vy:Number = 0;
public var number:int;
public var alive:Boolean = true;
public var parts:Vector.<Particle>;
public function Enemy(x:Number,y:Number,w:Number=10,h:Number=10) {
var i:int, j:int;
this.x = x;
this.y = y;
this.width = w;
this.height = h;
parts = new Vector.<Particle>();
parts.push(new Particle(x, y, w, h));
var borderColor:uint = 0x22000000//Math.random() * 0xffffffff;
for (i = 0; i < w / 8; i++ ) for (j = 0; j < h / 8;j++ ) {
parts[0].bmd.fillRect(new Rectangle(i * 8, j * 8, 8, 8), Math.random() * 0xffffffff);
}
parts[0].bmd.fillRect(new Rectangle(0, 0, w, 4), borderColor);
parts[0].bmd.fillRect(new Rectangle(0, 0, 4, h), borderColor);
parts[0].bmd.fillRect(new Rectangle(w-4, 0,4, h), borderColor);
parts[0].bmd.fillRect(new Rectangle(0, h-4, w, 4), borderColor);
vx = 0;
vy = 2+Math.random()*3;
}
public function move():void {
var i:int;
y += vy;
if (y > 465) {
Main.score-= width / 8 * height / 8;
if (Main.score < 0) Main.score = 0;
parts.splice(i, parts.length);
alive = false;
}
}
public function drop():void {
var i:int;
for (i = 0; i < parts.length; i++ ) {
parts[i].vy += 1;
parts[i].x += parts[i].vx;
parts[i].y += parts[i].vy;
if (parts[i].y > 465 || parts[i].x > 465 || parts[i].x < 0) {
Main.score += 1;
Main.tf.textColor = parts[i].color;
parts[i] = null;
parts.splice(i, 1);
}
}
}
public function dead():void {
var i:int, j:int;
alive = false;
for (i = 0; i < width / 8; i++ ) for (j = 0; j < height / 8; j++ ) {
var p:Particle = new Particle(x + i * 8, y + j * 8, 8, 8, Math.random() * 0xffffffff, true);
parts.push(p);
}
parts[0] = null;
parts.splice(0, 1);
}
}
class NParticle extends Particle {
public var n:int;
public function NParticle(x:Number,y:Number,n:int) {
super(x, y, 12, 24, 0x00000000);
vx = Math.random() * 12 - 6;
vy = -Math.random() * 10;
this.n = n;
color = Math.random() * 0xffffff+0xff000000;
switch(n) {
case 1:
bmd.fillRect(new Rectangle(0, 0, 8, 4), color);
bmd.fillRect(new Rectangle(4, 4, 4, 16), color);
bmd.fillRect(new Rectangle(0, 20, 12, 4), color);
break;
case 2:
bmd.fillRect(new Rectangle(0, 0, 12, 4), color);
bmd.fillRect(new Rectangle(8, 4, 4, 6), color);
bmd.fillRect(new Rectangle(0, 10, 12, 4), color);
bmd.fillRect(new Rectangle(0, 14, 4, 6), color);
bmd.fillRect(new Rectangle(0, 20, 12, 4), color);
break;
case 3:
bmd.fillRect(new Rectangle(0, 0, 8, 4), color);
bmd.fillRect(new Rectangle(0, 10, 8, 4), color);
bmd.fillRect(new Rectangle(0, 20, 8, 4), color);
bmd.fillRect(new Rectangle(8, 0, 4, 24), color);
break;
case 4:
bmd.fillRect(new Rectangle(0, 0, 4, 14), color);
bmd.fillRect(new Rectangle(4, 10, 4, 4), color);
bmd.fillRect(new Rectangle(8, 0, 4, 24), color);
break;
case 5:
bmd.fillRect(new Rectangle(0, 0, 12, 4), color);
bmd.fillRect(new Rectangle(0, 4, 4, 6), color);
bmd.fillRect(new Rectangle(0, 10, 12, 4), color);
bmd.fillRect(new Rectangle(8, 14, 4, 6), color);
bmd.fillRect(new Rectangle(0, 20, 12, 4), color);
break;
case 6:
bmd.fillRect(new Rectangle(0, 0, 12, 4), color);
bmd.fillRect(new Rectangle(0, 4, 4, 20), color);
bmd.fillRect(new Rectangle(4, 10, 8, 4), color);
bmd.fillRect(new Rectangle(8, 14, 4, 6), color);
bmd.fillRect(new Rectangle(4, 20, 8, 4), color);
break;
case 7:
bmd.fillRect(new Rectangle(0, 0, 12, 4), color);
bmd.fillRect(new Rectangle(0, 4, 4, 8), color);
bmd.fillRect(new Rectangle(8, 4, 4, 20), color);
break;
case 8:
bmd.fillRect(new Rectangle(0, 0, 12, 4), color);
bmd.fillRect(new Rectangle(0, 4, 4, 20), color);
bmd.fillRect(new Rectangle(8, 4, 4, 20), color);
bmd.fillRect(new Rectangle(4, 10, 4, 4), color);
bmd.fillRect(new Rectangle(4, 20, 4, 4), color);
break;
case 9:
bmd.fillRect(new Rectangle(0, 0, 12, 4), color);
bmd.fillRect(new Rectangle(0, 4, 4, 6), color);
bmd.fillRect(new Rectangle(8, 4, 4, 20), color);
bmd.fillRect(new Rectangle(0, 10, 8, 4), color);
bmd.fillRect(new Rectangle(0, 20, 8, 4), color);
break;
}
}
public function move():void {
x += vx;
y += vy;
vy++;
}
}