電流イライラ男
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.ui.Keyboard;
public class IrairaMan extends Sprite
{
private var MAP:Array = [
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0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 0, 0, 0, 0, 0, 1],
];
private var BLOCK_SIZE:int;
private var field:Sprite;
private var man:Man;
private var levelText:TextField;
private var wallMap:Object;
private var avx:Number = 0.3;
private var vx:Number = 0;
private var level:int = 1;
private var speed:Number = 3;
private var _rightKey:Boolean = false;
private var _leftKey:Boolean = false;
private var _upKey:Boolean = false;
private var _downKey:Boolean = false;
private var _spaceKey:Boolean = false;
public function IrairaMan()
{
init();
prepareStart();
}
private function init():void{
BLOCK_SIZE = stage.stageWidth / 10;
man = new Man();
field = new Sprite();
var tmpX:int = 0;
var tmpY:int = 0;
wallMap = new Object();
for each (var line:Array in MAP){
for each(var wallNum:int in line){
var wall:Wall = new Wall(wallNum, BLOCK_SIZE, tmpX, tmpY);
wall.x = tmpX * BLOCK_SIZE;
wall.y = tmpY * BLOCK_SIZE;
field.addChild(wall);
wallMap[tmpX + "," + tmpY] = wall;
tmpX += 1;
}
tmpX = 0;
tmpY += 1;
}
stage.addChild(field);
stage.addChild(man);
levelText = new TextField();
levelText.autoSize = TextFieldAutoSize.LEFT;
levelText.textColor = 0x000000;
levelText.background = true;
levelText.backgroundColor = 0xCCCCCC;
levelText.selectable = false;
levelText.x = 10;
levelText.y = 10;
updateLevel();
stage.addChild(levelText);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeydown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyup);
}
private function updateLevel():void{
levelText.text = "Level: " + level;
}
private function prepareStart():void{
vx = 0;
field.x = (stage.stageWidth - field.width) / 2;
field.y = stage.stageHeight - field.height;
man.x = stage.stageWidth / 2;
man.y = stage.stageHeight - man.height / 2;
man.redraw(man.NORMAL_COLOR);
var confirm:TextField = makeMessage("スペースキーを押してください");
stage.addChild(confirm);
addEventListener(Event.ENTER_FRAME,
function(e:Event):void{
if(_spaceKey){
removeEventListener(Event.ENTER_FRAME, arguments.callee);
stage.removeChild(confirm);
start();
}
}
);
}
private function start():void{
addEventListener(Event.ENTER_FRAME, walk);
}
private function gameOver():void{
removeEventListener(Event.ENTER_FRAME, walk);
var message:TextField = makeMessage("GAME OVER\nスペースキーで再挑戦");
stage.addChild(message);
//GAME OVER アニメーション
man.redraw(man.SHOCKED_COLOR);
addEventListener(Event.ENTER_FRAME,
function(e:Event):void{
if(_spaceKey){
removeEventListener(Event.ENTER_FRAME, arguments.callee);
stage.removeChild(message);
prepareStart();
}
}
);
}
private function gameClear():void{
removeEventListener(Event.ENTER_FRAME, walk);
var message:TextField = makeMessage("お見事!!\nスペースキーでLevelアップ");
stage.addChild(message);
addEventListener(Event.ENTER_FRAME,
function(e:Event):void{
if(_spaceKey){
removeEventListener(Event.ENTER_FRAME, arguments.callee);
stage.removeChild(message);
speed *= 1.4;
avx *= 1.4;
level += 1;
updateLevel();
prepareStart();
}
}
);
}
private function makeMessage(str:String):TextField{
var message:TextField = new TextField();
message.textColor = 0xFFFFFF;
message.autoSize = TextFieldAutoSize.CENTER;
message.text = str;
message.background = true;
message.backgroundColor = 0x000000;
message.x = (stage.stageWidth - message.width) / 2;
message.y = stage.stageHeight / 2;
return message;
}
private function onKeydown(e:KeyboardEvent):void{
keyCheck(e.keyCode, true);
}
private function onKeyup(e:KeyboardEvent):void{
keyCheck(e.keyCode, false);
}
private function keyCheck(keyCode:uint, bool:Boolean):void{
switch(keyCode){
case Keyboard.RIGHT:
_rightKey = bool;
break;
case Keyboard.LEFT:
_leftKey = bool;
break;
case Keyboard.UP:
_upKey = bool;
break;
case Keyboard.DOWN:
_downKey = bool;
break;
case Keyboard.SPACE:
_spaceKey = bool;
break;
case 229:
_spaceKey = bool;
break;
default:
break;
}
}
private function checkHit():Boolean{
var rect:Rectangle = man.getBounds(field);
var topLeftWall:Wall = getWallFromPoint(rect.topLeft);
var bottomRightWall:Wall = getWallFromPoint(rect.bottomRight);
if(topLeftWall == null) topLeftWall = bottomRightWall;
if(bottomRightWall == null) bottomRightWall = topLeftWall;
if(topLeftWall != null || bottomRightWall != null){
for(var i:int = topLeftWall.indexX; i<=bottomRightWall.indexX; i++){
for(var j:int = topLeftWall.indexY; j<=bottomRightWall.indexY; j++){
var hit:Boolean = Wall(wallMap[i + "," + j]).hitTestObject(man);
if(hit) return true;
}
}
}
return false;
}
private function getWallFromPoint(p:Point):Wall{
var indexX:int = p.x / BLOCK_SIZE;
var indexY:int = p.y / BLOCK_SIZE;
return wallMap[indexX + "," + indexY];
}
private function walk(e:Event):void{
if(_rightKey){
vx += avx;
}
if(_leftKey){
vx -= avx;
}
if(_upKey){
man.scaleX = man.scaleY *= 1.1;
man.y = stage.stageHeight - man.height / 2;
}
if(_downKey && man.scaleX > 0.2){
man.scaleX = man.scaleY /= 1.1;
man.y = stage.stageHeight - man.height / 2;
}
man.x += vx;
field.y += speed;
if(checkHit()){
gameOver();
}else if(field.y > stage.stageHeight){
gameClear();
}
}
}
}
import flash.display.Sprite;
class Man extends Sprite
{
public var NORMAL_COLOR:uint = 0x000000;
public var SHOCKED_COLOR:uint = 0xFF0000;
public function Man()
{
redraw(NORMAL_COLOR);
}
public function redraw(col:uint):void{
graphics.clear();
graphics.beginFill(col);
graphics.drawCircle(0, -10, 5);
graphics.drawRect(-2, -5, 4, 12);
graphics.endFill();
graphics.moveTo(-5, -5);
graphics.lineStyle(3, col);
graphics.curveTo(0, 0, 5, -5);
graphics.moveTo(-5, 10);
graphics.curveTo(0, 0, 5, 10);
}
}
class Wall extends Sprite
{
private var WALL_COLOR:uint = 0x333333;
public var indexX:int;
public var indexY:int;
public function Wall(type:int, size:int, indexX:int, indexY:int)
{
this.indexX = indexX;
this.indexY = indexY;
switch(type){
case 0:
break;
case 1:
graphics.beginFill(WALL_COLOR);
graphics.drawRect(0, 0, size, size);
graphics.endFill();
break;
}
}
}