シューティングの元
// forked from h_sakurai's code on 2009-1-3
// write as3 code here..
package {
import flash.display.*;
import flash.text.*;
import flash.events.*;
public class A extends Sprite {
public var txt:TextField = new TextField();
private var key:Key = new Key(stage, txt);
public var ship:Ship = new Ship(230,450);
private var enemy:Enemy = new Enemy();
public var enemys:Vector.<Enemy> = new Vector.<Enemy>;
public var bullets:Vector.<Bullet>= new Vector.<Bullet>;
public var particles:Vector.<Particle>= new Vector.<Particle>;
public var shots:Vector.<Shot>= new Vector.<Shot>;
public var gameover:TextField = new TextField();
public var score:int = 0;
public var rank:int = 0;
function A() {
gameover.text = "GAME OVER";
gameover.y = 200;
gameover.x = 200;
addChild(gameover);
//this.z= 300;
//this.rotationX = -30;
a = this;
//addChild(enemy);
//enemy.init(230,50,0);
for(var i:int = 0; i < 500;i++)
addChild(bullets[i] = new Bullet());
for(i = 0; i < 500;i++)
addChild(particles[i] = new Particle());
for(i = 0; i < 10;i++)
addChild(shots[i] = new Shot());
for(i = 0; i < 10;i++)
addChild(enemys[i] = new Enemy());
txt.width = 100;
txt.height = 100;
addChild(ship);
addChild(txt);
addEventListener("enterFrame",onEnter);
}
private function onEnter(e:Event):void {
var enemyCnt:int = 0;
for each (var enemy:Enemy in enemys) {
if(enemy.visible) {
enemyCnt++;
enemy.move();
}
}
if(enemyCnt == 0 || Math.random() < 0.01 * rank) {
switch(int(Math.random() * 3)){
case 0: addEnemy(0,Math.random()*200,0); break;
case 1: addEnemy(Math.random()*465,0,Math.PI/2); break;
case 2: addEnemy(465,Math.random()*200,Math.PI); break;
}
}
if(gameover.visible && Key["start"]) {
score = 0;
rank = 1;
gameover.visible = false;
ship.init();
}
if(ship.visible)ship.move();
for each (var b:Bullet in bullets)
if(b.visible) {
b.move();
if(ship.exists && Math.abs(ship.x-b.x)<3 && Math.abs(ship.y-b.y)<3) {
addParticles(ship.x, ship.y, 100);
ship.exists = false;
ship.visible = false;
gameover.visible = true;
}
}
for each (var s:Shot in shots)
if(s.visible) {
for each(enemy in enemys) {
if(enemy.visible && Math.abs(enemy.x-s.x)<30 && Math.abs(enemy.y-s.y)<30) {
score += 100;
rank = score / 1000;
addParticles(enemy.x, enemy.y,30);
enemy.visible = false;
}
}
s.move();
}
for each (var p:Particle in particles)
if(p.visible)
p.move();
txt.text = "SCORE "+score;
}
public function addParticles(x:Number, y:Number, n:int):void {
for (var i:int = 0; i < n; i++)
addParticle(x,y,Math.random() * 100);
}
public function addBulletN(x:Number, y:Number, rad:Number,n:int, r:Number):void {
for (var i:int = 0; i < n; i++) {
addBullet(x,y,rad+(i-n/2.0)*r);
}
}
public function addBullet(x:Number, y:Number, rad:Number):void {
for each (var b:Bullet in bullets) {
if(b.visible) continue;
b.init(x,y,rad);
return;
}
}
public function addShot(x:Number, y:Number, rad:Number):void {
for each (var b:Shot in shots) {
if(b.visible) continue;
b.init(x,y,rad);
return;
}
}
public function addParticle(x:Number, y:Number, rad:Number):void {
for each (var b:Particle in particles) {
if(b.visible) continue;
b.init(x,y,rad);
return;
}
}
public function addEnemy(x:Number, y:Number, rad:Number):void {
for each (var b:Enemy in enemys) {
if(b.visible) continue;
b.init(x,y, rad);
return;
}
}
}
}
import flash.events.*;
import flash.display.*;
import flash.text.*;
var a:A;
class Ship extends Sprite{
private var speed:Number = 10;
public var exists:Boolean;
private var timer:uint = 0;
public function Ship(x:Number, y:Number) {
this.x = x;
this.y = y;
graphics.beginFill(0xffff0000);
graphics.drawRect(-5,-5,10,10);
graphics.endFill();
}
public function init():void {
exists = false;
timer = 0;
visible = true;
}
public function move():void {
timer++;
if(timer > 300) exists = true;
if(Key["shot"])a.addShot(x,y,-Math.PI/2);
if(Key["left" ])x -= speed;
if(Key["right"])x += speed;
if(Key["up" ])y -= speed;
if(Key["down" ])y += speed;
if(x < 0) x = 5; else if(x > 465) x = 465 - 5;
if(y < 0) y = 5; else if(y > 465) y = 465 - 5;
}
}
class Enemy extends Sprite{
private var speed:Number = 5;
private var rad:Number = 0;
private var r:Number = 0.05;
public function Enemy() {
graphics.beginFill(0xffff0000);
graphics.drawRect(-5,-5,10,10);
graphics.endFill();
visible = false;
}
public function init(x:Number, y:Number, rad:Number):void {
this.x = x;
this.y = y;
this.rad = rad = Math.atan((y-a.ship.y)/(x-a.ship.x));
visible = true;
move = move1;
}
public var move:Function;
private function moveBody():Number {
var rad2:Number = Math.atan2(a.ship.y-y, a.ship.x-x);
if(normalRad(rad-rad2)<0)
rad += r else rad -= r;
rad = normalRad(rad);
x += Math.cos(rad)*speed;
y += Math.sin(rad)*speed;
if(x < 0) x = 5; else if(x > 465) x = 465 - 5;
if(y < 0) y = 5; else if(y > 465) y = 465 - 5;
return rad2;
}
private var nextMove:Function;
private var time:int;
private function wait(w:int,next:Function):void {
time = w;
move = moveWait;
nextMove = next;
}
public function moveWait():void {
moveBody();
time -= 1;
if(time <= 0) {
move = nextMove;
}
}
public function move1():void {
var r:Number = moveBody();
if(Math.random() > 0.8) {
a.addBullet(x,y,r);
if(Math.random() > 0.8) {
r2=0;
wait(30,move2);
}
}
}
private var r2:Number = 0;
public function move2():void {
var r:Number = moveBody();
// if(Math.random() > 0.8) {
var rr:int = Math.random()*5+1;
a.addBulletN(x,y,r+r2,rr,0.1);
r2 = r2+rr*0.1;
if(Math.random() < 0.03)wait(60,move1);
// }
}
}
function normalRad(v:Number):Number {
return v + 2 * Math.PI * (
(v > Math.PI) ? -1 :
(v < -Math.PI) ? 1 : 0
);
}
class Particle extends Sprite{
private var speed:Number = 5;
private var rad:Number;
public function Particle() {
graphics.beginFill(0xffff8800);
graphics.drawRect(-2,-2,4,4);
graphics.endFill();
visible = false;
}
public function init(x:Number, y:Number, rad:Number):void {
this.x = x;
this.y = y;
this.rad = rad;
visible = true;
speed = Math.random() * 50 + 10;
}
public function move():Boolean {
x += Math.cos(rad)*speed;
y += Math.sin(rad)*speed;
if(x < 0 || 465 < x || y < 0 || 465 < y) visible = false;
return visible ;
}
}
class Bullet extends Sprite{
private var speed:Number = 10;
private var rad:Number;
public function Bullet() {
graphics.beginFill(0xffff8800);
graphics.drawRect(-5,-5,10,10);
graphics.endFill();
visible = false;
}
public function init(x:Number, y:Number, rad:Number):void {
this.x = x;
this.y = y;
this.rad = rad;
visible = true;
}
public function move():Boolean {
x += Math.cos(rad)*speed;
y += Math.sin(rad)*speed;
if(x < 0 || 465 < x || y < 0 || 465 < y) visible = false;
return visible ;
}
}
class Shot extends Sprite {
public var angle:Number;
private var speed:Number = 25;
private var rad:Number;
public function Shot () {
graphics.beginFill(0xff88ff88);
graphics.drawRect(-15,-5,10,10);
graphics.drawRect(5,-5,10,10);
graphics.endFill();
visible = false;
}
public function init(x:Number, y:Number, rad:Number):void {
this.x = x;
this.y = y;
this.rad = rad;
visible = true;
}
public function move():Boolean {
x += Math.cos(rad)*speed;
y += Math.sin(rad)*speed;
if(x < 0 || 465 < x || y < 0 || 465 < y) visible = false;
return visible ;
}
}
class Key {
function Key(stage:Stage,txt:TextField) {
var kmap:Object = {37:"left", 38:"up",39:"right",40:"down",90:"shot",13:"start"};
for each(var i:String in kmap) Key[i]=false;
stage.addEventListener("keyDown",function(e:KeyboardEvent):void {
//txt.text = ""+e.keyCode + ":"+kmap[e.keyCode];
if(e.keyCode in kmap) Key[kmap[e.keyCode]] = true;
});
stage.addEventListener("keyUp",function(e:KeyboardEvent):void {
if(e.keyCode in kmap) Key[kmap[e.keyCode]] = false;
});
}
}