ゲームっぽくしてみたforked from: なんとかディウスっぽい背景
ゲームっぽくしてみた
十字キーで移動
スペースキーで弾発射
だれか当たり判定つけて><
// forked from gyuque's なんとかディウスっぽい背景
// ゲームっぽくしてみた
// 十字キーで移動
// スペースキーで弾発射
// だれか当たり判定つけて><
package
{
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.ui.Keyboard;
[SWF(width="320", height="240", backgroundColor="0x000000", frameRate="30")]
public class Nemesis extends Sprite
{
public static const W:int = 320;
public static const H:int = 240;
private var mScroll:int = 0;
private var mMyPos:Point;
private var mBGen:MountainGen = new MountainGen(160);
private var mTGen:MountainGen = new MountainGen(160, 1);
private var mBGScreen:BitmapData = new BitmapData(W, H, true, 0);
private var mBGScreenBmp:Bitmap;
private var mBGScreenBmp2:Bitmap;
private var mStarbg:StarBG = new StarBG(W, H, 30);
private var ship:Ship = new Ship(0x00ffff);
private var speed:int = 4;
private var isUp:Boolean = false;
private var isDown:Boolean = false;
private var isLeft:Boolean = false;
private var isRight:Boolean = false;
private var isShot:Boolean = false;
function Nemesis()
{
addChild(mStarbg);
mBGScreenBmp = new Bitmap(mBGScreen);
addChild(mBGScreenBmp);
mBGScreenBmp2 = new Bitmap(mBGScreen);
addChild(mBGScreenBmp2);
addChild(ship);
ship.x = 40;
ship.y = H / 2;
var i:int;
mBGen.step();
mTGen.step();
for (i = 0;i < W;i++) {
mBGen.step();
mTGen.step();
mBGen.draw(mBGScreen, i, 80);
mTGen.draw(mBGScreen, i, 0, false);
}
addEventListener(Event.ENTER_FRAME, tick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
}
private var _clrrc:Rectangle = new Rectangle(0, 0, 1, H);
private function tick(e:Event):void
{
var ox:int = mScroll % W;
_clrrc.x = ox;
mBGScreen.fillRect(_clrrc, 0);
mStarbg.tick();
mBGen.step();
mTGen.step();
mStarbg.draw();
mBGen.draw(mBGScreen, ox, 80);
mTGen.draw(mBGScreen, ox, 0, false);
mBGScreenBmp.x = -ox;
mBGScreenBmp2.x = W-ox;
mScroll++;
ship.y -= isUp? speed : 0;
ship.y += isDown? speed: 0;
ship.x -= isLeft? speed: 0;
ship.x += isRight? speed: 0;
if (isShot && !(mScroll%5)) {
var bullet:Bullet = new Bullet(0xff00ff, 3, 20);
bullet.x = ship.x;
bullet.y = ship.y;
addChild(bullet);
}
if (ship.x - ship.width< 0) ship.x = ship.width;
else if (ship.x > W) ship.x = W;
if (ship.y - ship.height/2 < 0) ship.y = ship.height/2;
else if (ship.y + ship.height/2 > H) ship.y = H - ship.height/2;
}
private function onKeyDownHandler(e:KeyboardEvent):void {
switch(e.keyCode) {
case Keyboard.UP: isUp = true; break;
case Keyboard.DOWN: isDown = true; break;
case Keyboard.LEFT: isLeft = true; break;
case Keyboard.RIGHT: isRight = true; break;
case Keyboard.SPACE: isShot = true; break;
}
}
private function onKeyUpHandler(e:KeyboardEvent):void
{
switch(e.keyCode) {
case Keyboard.UP: isUp = false; break;
case Keyboard.DOWN: isDown = false; break;
case Keyboard.LEFT: isLeft = false; break;
case Keyboard.RIGHT: isRight = false; break;
case Keyboard.SPACE: isShot = false; break;
}
}
}
}
import flash.display.*;
import flash.events.Event;
import flash.geom.Point;
class Ship extends Sprite
{
public var color:uint;
function Ship(color:uint)
{
this.color = color;
this.graphics.lineStyle(0, color);
this.graphics.lineTo( -30, -10);
this.graphics.lineTo( -30, 10);
this.graphics.lineTo(0, 0);
this.graphics.endFill();
}
}
class Bullet extends Sprite
{
public var color:uint;
public var size:uint;
public var speed:uint;
function Bullet(color:uint = 0x0000ff, size:uint = 3, speed:uint = 5)
{
this.color = color;
this.size = size;
this.speed = speed;
this.graphics.lineStyle(0, color);
this.graphics.drawCircle(0, 0, size);
this.graphics.endFill();
addEventListener(Event.ENTER_FRAME, updateHandler);
}
private function updateHandler(e:Event):void
{
this.x += speed;
if (this.x + size / 2 > Nemesis.W) {
this.parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, updateHandler);
}
}
}
class MountainGen
{
private var mPrevBuffer:Array;
private var mHeight:int;
private var mCount:int = 0;
private var tmpBuffer:Array;
private var mGenFunc:Function;
function MountainGen(h:int, generator:int = 0)
{
mHeight = h;
mGenFunc = generator ? genWav2 : genWav;
mPrevBuffer = new Array(h);
tmpBuffer = new Array(h);
}
public function draw(b:BitmapData, x:int, y:int, rev:Boolean = true):void
{
var i:int;
for (i = 0;i < mHeight;i++) {
if (tmpBuffer[i]) {
var c:int = tmpBuffer[i];
b.setPixel32(x, rev ? (y+mHeight-i) : (y+i), 0xff000000 | (c/3+11) | ((c/5 + 170)<<16) | ((c/2+60) << 8));
}
}
}
public function step():void
{
var t:Number = Number(mCount) * 0.02;
var h:int = mGenFunc(t) * mHeight;
var i:int, k:int, m:int;
for (i = 0;i < mHeight;i++) {
mPrevBuffer[i] = tmpBuffer[i];
}
for (i = 0;i < mHeight;i++) {
tmpBuffer[i] = (i < h) ? (Math.random()*80 + 80) : 0;
if (tmpBuffer[i]) {
if (mPrevBuffer[i])
tmpBuffer[i] = (tmpBuffer[i] + mPrevBuffer[i]*7)/8;
if (mPrevBuffer[i] == 0 || i == (h-1)) {
m = 50;
for (k = i;k >= 0 && m > 0;k--, m-=4) {
if (m > 33) m--;
tmpBuffer[k] += m;
m += Math.random()*7;
}
}
if (mPrevBuffer[i+1] && i == (h-1)) {
m = -48;
for (k = i;k >= 0 && m < 0;k--, m++) {
tmpBuffer[k] += m;
if (tmpBuffer[k]<1) tmpBuffer[k] = 1;
}
}
}
}
for (i = 0;i < mHeight;i++)
tmpBuffer[i] = (tmpBuffer[i] < 0) ? 0 : (tmpBuffer[i] > 255) ? 255 : tmpBuffer[i];
mCount++;
}
private static function genWav(t:Number, nest:int = 0):Number
{
var v:Number = Math.sin(t);
v += Math.cos(t*3) * 0.1;
v += Math.cos(0.1 + t*10) * 0.02;
v *= Math.cos(t*0.1);
if (nest < 5)
v += genWav(t+1, ++nest);
v = v*0.2 + 0.3;
return (v<0) ? 0 : (v>1) ? 1 : v;
}
private static function genWav2(t:Number, nest:int = 0):Number
{
var v:Number = Math.sin(t);
v += Math.cos(t*3) * 0.1;
v += Math.cos(0.1 + t*9) * 0.02;
v *= Math.cos(0.2 + t*0.15);
if (nest < 5)
v += genWav2(t+1, ++nest);
v = v*0.2 + 0.2;
return (v<0) ? 0 : (v>1) ? 1 : v;
}
}
class StarBG extends Sprite
{
private var mWidth:int;
private var mHeight:int;
private var mStars:Array;
private var mStarVs:Array;
private var mN:int;
function StarBG(w:int, h:int, n:int)
{
mWidth = w;
mHeight = h;
mN = n;
mStars = new Array(n);
mStarVs = new Array(n);
for (var i:int = 0;i < n;i++) {
mStars[i] = new Point(int( Math.random()*w ), int( Math.random()*h ));
mStarVs[i] = Math.random() + 0.2;
}
}
public function tick():void
{
var n:int = mN;
for (var i:int = 0;i < n;i++) {
mStars[i].x -= Number(mStarVs[i]);
if (mStars[i].x < 0) {
mStars[i].x += mWidth;
mStars[i].y = int( Math.random()*mHeight );
}
}
}
public function draw():void
{
var g:Graphics = graphics;
var n:int = mN;
g.clear();
for (var i:int = 0;i < n;i++) {
g.beginFill(0xffffff);
g.drawCircle( mStars[i].x, mStars[i].y , 0.4);
}
}
}