LIVE CODING 一日でシューティングゲーム作る on 2010-3-30
Live Coding 2010 3/30 9:50~0:03
action script 3.0の勉強のために今日一日でシューティングゲーム作る。
画面クリックでシューティングが始まります。
STAGE1だけ完成。
今日中にSTAGE3まで作りたかったけど、さすがにむりげーだった。
今日はもう寝る。
/**
* Copyright shohei909 ( http://wonderfl.net/user/shohei909 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jP5P
*/
package {
/*
Live Coding 2010 3/30 9:50~0:03
action script 3.0の勉強のために今日一日でシューティングゲーム作る。
画面クリックでシューティングが始まります。
STAGE1だけ完成。
今日中にSTAGE3まで作りたかったけど、さすがにむりげーだった。
今日はもう寝る。
*/
import flash.display.Sprite;
import flash.events.*;
import flash.text.*;
import flash.utils.*;
public class Game extends Sprite {
private var point:int=0;
private var time:int=0;
private var startTime:int=0;
private var shadow:MyShipShape = new MyShipShape();
private var myShip:MyShip;
private var field:Sprite = new Sprite();
private var textfield:TextField;
private var title:TitleField;
private var myShot:Array = new Array();
private var bomb:Array = new Array();
private var enemy:Array = new Array();
private var dead:Array = new Array();
private var shotType:int = 0;
private var wave:int =0;
private var waveCount:int = 0;
private var frameCount:uint=0;
private var lifeBar:LifeBar = new LifeBar();
private var play:Boolean = false;
public function Game():void{
field= new Sprite();
textfield= new TextField();
title= new TitleField();
with(textfield){
width=400;
selectable=false;
}
with(title){
antiAliasType = AntiAliasType.ADVANCED;
y=425;
x=0;
width=455;
selectable=false;
}
title.setText("Please click to start",-1);
stage.addChild(field);
stage.addChild(textfield);
stage.addChild(title);
lifeBar = new LifeBar();
stage.addChild(lifeBar);
with(field.graphics){
lineStyle (4, 0x000000, 1.0); // 線のスタイル指定
beginFill (0xFFFFFF, 0.0); // 面のスタイル設定
drawRoundRect ( 0,0, 465 , 465 , 15 , 16);
}
field.addEventListener(MouseEvent.MOUSE_DOWN,onClick);
}
public function init():void{
title.setText("",-1);
while(field.numChildren>0){
field.removeChildAt(0);
}
point = 0;
time = 0;
startTime = getTimer();
shadow = new MyShipShape();
myShot = new Array();
bomb = new Array();
enemy = new Array();
dead = new Array();
shotType = 0;
wave =0;
waveCount = 0;
frameCount = 0;
play = true;
myShip = new MyShip();
myShip.skeleton=true;
lifeBar.x=10;
lifeBar.y=440;
lifeBar.life = 3;
field.addChild(myShip);
//リスナーの追加
this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
public function addMyShot(dx:int, dy:int, power:int):void{
var shot:MyShot = new MyShot();
shot.setData(dx, dy, 10, power);
shot.x = myShip.x;
shot.y = myShip.y;
myShot.push(shot);
field.addChildAt(shot,0);
}
public function addEnemy1(x:int, y:int):void{
var ship:Enemy1 = new Enemy1();
ship.x = x;
ship.y = y;
enemy.push(ship);
field.addChild(ship);
}
private function onEnterFrame(e:Event):void{
textfield.text="";
myShip.x=(field.mouseX+myShip.x*3)/4;
myShip.y=(field.mouseY+myShip.y*3)/4;
if(play){
frameCount++;
if(frameCount==5){
switch(shotType){
case 0:
addMyShot(0,-18,10);
break;
case 1:
addMyShot(12,-12,10);
addMyShot(-12,-12,10);
break;
case 2:
addMyShot(18,0,10);
addMyShot(-18,0,10);
addMyShot(12,12,10);
addMyShot(-12,12,10);
addMyShot(0,18,10);
break;
}
frameCount=0;
}
//自機の弾と敵のあたり判定
for(i=0; i<myShot.length; i++){
for(var j:int=0; j<enemy.length; j++){
if(enemy[j].hitTestPoint ( myShot[i].x , myShot[i].y , true )){
enemy[j].life -= myShot[i].power;
myShot[i].life=0;
enemy[j].x+=myShot[i].dx/2;
enemy[j].y+=myShot[i].dy/2;
point+=10;
}
}
}
//自機と敵のあたり判定
if(myShip.skeleton == false){
for(j=0; j<enemy.length; j++){
if(enemy[j].hitTestObject(myShip)){
myShip.skeleton = true;
if(lifeBar.life==0){
gameOver();
}else{
lifeBar.life--;
}
shadow.x=myShip.x;
shadow.y=myShip.y;
shadow.count=0;
shadow.alpha=1;
field.addChildAt(shadow,0);
}
}
}
time=getTimer()-startTime;
textfield.appendText("クリックで弾切り替え point:" + point + " time:"+time);
}
//自機の弾の移動
for(var i:int=0; i<myShot.length; i++){
myShot[i].x += myShot[i].dx;
myShot[i].y += myShot[i].dy;
if(myShot[i].life == 0 || myShot[i].y < -200 || myShot[i].y > 650 || myShot[i].x < -200 ||myShot[i].x > 650){
myShot[i].count=0;
field.removeChild(myShot[i]);
bomb.push(myShot[i]);
field.addChildAt(bomb[bomb.length-1],0);
myShot.splice(i,1);
i--;
}
}
//敵の移動
for(i=0; i<enemy.length; i++){
switch(enemy[i].type){
case 1:
if(enemy[i].count == 60){
enemy[i].count = 0;
}
if(enemy[i].count == 0){
enemy[i].targetX = enemy[i].x + 100;
enemy[i].targetY = enemy[i].y + 100;
}
if(enemy[i].count == 30){
enemy[i].targetX = enemy[i].x - 100;
enemy[i].targetY = enemy[i].y + 100;
}
enemy[i].x = (enemy[i].x*15 + enemy[i].targetX)/16
enemy[i].y = (enemy[i].y*15 + enemy[i].targetY)/16
break;
}
enemy[i].count++;
if(enemy[i].x < -50){
enemy[i].x=510;
enemy[i].count = 0;
}
if(enemy[i].x > 510){
enemy[i].x=-50;
enemy[i].count = 0;
}
if(enemy[i].y > 510){
enemy[i].y= -50;
enemy[i].count = 0;
}
if(enemy[i].y <-50){
enemy[i].y= 510;
enemy[i].count = 0;
}
if(enemy[i].life <= 0){
point+=500
enemy[i].count=0;
field.removeChild(enemy[i]);
dead.push(enemy[i]);
field.addChild(dead[dead.length-1]);
enemy.splice(i,1);
i--;
}
}
//敵の破壊エフェクト
for(i=0; i<dead.length; i++){
dead[i].count++;
dead[i].alpha= 1 - dead[i].count/5;
dead[i].scaleX= dead[i].count/2;
dead[i].scaleY= dead[i].count/2;
if(dead[i].count == 100){
field.removeChild(dead[i]);
dead.splice(i,0);
}
}
//弾の爆発エフェクト
for(i=0; i<bomb.length; i++){
bomb[i].count++;
bomb[i].scaleX = bomb[i].count;
bomb[i].scaleY = bomb[i].count;
if(bomb[i].count == 5){
field.removeChild(bomb[i]);
bomb.splice(i,0);
}
}
if(field.contains(shadow)){
shadow.count++;
shadow.alpha= 1 - shadow.count/5;
shadow.scaleX= shadow.count/3;
shadow.scaleY= shadow.count/3;
if(shadow.count == 10){
field.removeChild(shadow);
}
}
switch(wave){
case 0:
title.setText("STAGE 1",100);
waveCount++;if(waveCount>30){wave++;}
break;
case 1:
addEnemy1(200,-40);addEnemy1(150,-35);addEnemy1(100,-25);
waveCount=0;wave++;
break;
case 2:
waveCount++;if(waveCount>40){wave++;}
break;
case 3:
addEnemy1(365,-40);addEnemy1(315,-35);addEnemy1(265,-25);
waveCount=0;wave++;
break;
case 4:
if(enemy.length==0){wave++;}
break;
case 5:
addEnemy1(0,-20); addEnemy1(50,-25); addEnemy1(200,-40);addEnemy1(150,-35);addEnemy1(100,-30);addEnemy1(415,-45);addEnemy1(365,-40);addEnemy1(315,-35);addEnemy1(265,-20);
waveCount=0;wave++;
break;
case 6:
waveCount++;if(waveCount>60){wave++;}
break;
case 7:
addEnemy1(50,-25); addEnemy1(200,-40);addEnemy1(365,-40);
waveCount=0;wave++;
break;
case 8:
if(enemy.length==0){wave++;}
break;
case 9:
addEnemy1(200,-40);addEnemy1(150,-35);addEnemy1(100,-25);
waveCount=0;wave++;
break;
case 10:
waveCount++;if(waveCount>40){wave++;}
break;
case 11:
addEnemy1(365,-40);addEnemy1(315,-35);addEnemy1(265,-25);
waveCount=0;wave++;
break;
case 12:
waveCount++;if(waveCount>40){wave++;}
break;
case 13:
addEnemy1(0,-20); addEnemy1(50,-25); addEnemy1(200,-40);addEnemy1(150,-35);addEnemy1(100,-30);addEnemy1(415,-45);addEnemy1(365,-40);addEnemy1(315,-35);addEnemy1(265,-20);
waveCount=0;wave++;
break;
case 14:
waveCount++;if(waveCount>60){wave++;}
break;
case 15:
addEnemy1(50,-25); addEnemy1(200,-40);addEnemy1(365,-40);
waveCount=0;wave++;
break;
case 16:
waveCount++;if(waveCount>10){wave++;}
break;
case 17:
addEnemy1(50,-25);addEnemy1(150,-35);addEnemy1(100,-25);
waveCount=0;wave++;
break;
case 18:
waveCount++; if(waveCount>10){wave++;}
break;
case 19:
addEnemy1(365,-40);addEnemy1(315,-35);addEnemy1(265,-25);
waveCount=0;wave++;
break;
case 20:
waveCount++;if(waveCount>10){wave++;}
break;
case 21:
addEnemy1(50,-25);addEnemy1(150,-35);addEnemy1(100,-25);
waveCount=0;wave++;
break;
case 22:
waveCount++; if(waveCount>10){wave++;}
break;
case 23:
addEnemy1(365,-40);addEnemy1(315,-35);addEnemy1(265,-25);
waveCount=0;wave++;
break;
case 24:
waveCount++;if(waveCount>10){wave++;}
break;
case 25:
addEnemy1(50,-25);addEnemy1(150,-35);addEnemy1(100,-25);
waveCount=0;wave++;
break;
case 26:
waveCount++; if(waveCount>10){wave++;}
break;
case 27:
addEnemy1(365,-40);addEnemy1(315,-35);addEnemy1(265,-25);
waveCount=0;wave++;
break;
case 28:
if(enemy.length==0){wave++;}
break;
default:
wave=0;
}
}
private function onClick(e:MouseEvent):void{
if(play){
shotType++;
if(shotType>2){
shotType=0;
}
}else{
init();
}
}
private function gameOver():void{
play = false;
field.removeChild(myShip);
title.setText("GAME OVER",-1);
}
}
}
import flash.display.Sprite;
import flash.events.*;
import flash.utils.*;
import flash.text.*;
class Enemy extends Sprite{
public var type:int;
public var life:int;
public var maxLife:int;
public var targetX:int;
public var targetY:int;
public var count:int;
public function Enemy():void{}
}
class Enemy1 extends Enemy{
public function Enemy1():void{
type = 1
maxLife = 100;
life = 50;
targetX = x;
targetY = y;
with(graphics){
lineStyle (4, 0x000000, 1.0); // 線のスタイル指定
beginFill (0xDDCCFF, 1.0); // 面のスタイル設定
drawRoundRect (-20 ,-20, 40 , 40 , 7 , 7);
}
}
}
class MyShot extends Sprite{
public var type:int;
public var life:int;
public var power:int;
public var dx:int;
public var dy:int;
public var count:int;
public function MyShot():void{
with(graphics){
lineStyle (4, 0x000000, 1.0); // 線のスタイル指定
beginFill (0xFFCCDD, 1.0); // 面のスタイル設定
drawCircle(0, 0 , 5);
}
}
public function setData(ndx:int, ndy:int, nlife:int,npower:int):void{
dx=ndx;dy=ndy;life=nlife;power=npower;
}
}
class LifeBar extends Sprite{
private var _life:int;
public function set life(l:int):void{
_life=l;
graphics.clear();
for(var i:int=0;i<l;i++){
with(graphics){
lineStyle (4, 0x000000, 1.0); // 線のスタイル指定
beginFill (0xFFCCDD, 1.0); // 面のスタイル設定
drawRoundRect (i*20 ,0, 15 , 15 , 7 , 7);
}
}
}
public function get life():int{
return _life;
}
public function MyShot():void{
life = 10;
}
}
class MyShip extends Sprite{
private var _skeleton:Boolean = false;
private var myTimer:Timer = new Timer(1000);
public function set skeleton(s:Boolean):void{
_skeleton=s;
if(s){
myTimer.start();
}
alpha=0.3;
}
public function get skeleton():Boolean{
return _skeleton;
}
public function MyShip(){
with(graphics){
lineStyle (4, 0x000000, 1.0); // 線のスタイル指定
beginFill (0xFFCCDD, 1.0); // 面のスタイル設定
drawRoundRect ( -8,-8, 16 , 16 , 7 , 7);
}
myTimer.addEventListener("timer", visi);
}
public function visi(e:TimerEvent):void{
myTimer.stop();
_skeleton=false;
alpha=1;
}
}
class MyShipShape extends Sprite{
public var count:int = 0;
public function MyShipShape(){
with(graphics){
lineStyle (4, 0x000000, 1.0); // 線のスタイル指定
beginFill (0xFFCCDD, 1.0); // 面のスタイル設定
drawRoundRect ( -8,-8, 16 , 16 , 7 , 7);
}
}
}
class TitleField extends TextField{
public var timer:int = 0;
public var count:int = 0;
public var b:Number = 50;
public var db:Number = 2;
public var vis:Boolean = true;
public var textFormat:TextFormat = new TextFormat();
public function TitleField(){
alpha=0.5
with(textFormat){
align="right";
font="メイリオ";
bold=true;
color=0xFF0000;
size = 30;
}
addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
public function setText(s:String,t:int):void{
text=s;
vis=true;
b= 30;
db= 1;
setTextFormat(textFormat);
timer=t;
}
public function onEnterFrame(e:Event):void{
if(vis){
if(timer==0){
alpha=0;
b+=db;
if(b>60){b=60;db=-1;}
if(b<0){
b=0;
vis = false;
}
alpha = b/100;
}else{
timer--;
b+=db;
if(b>60){b=60;db=-1;}
if(b<40){b=40;db=1;}
alpha = b/100;
}
}
}
}