flash on 2010-8-22
♥0 |
Line 204 |
Modified 2010-08-22 04:46:04 |
MIT License
archived:2017-03-10 09:49:09
ActionScript3 source code
/**
* Copyright kihon ( http://wonderfl.net/user/kihon )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/5AQX
*/
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import frocessing.math.Random;
public class Main extends Sprite
{
private const WIDTH:int = 5; // 横のマス数
private const HEIGHT:int = 5; // 縦のマス数
private const MASUSIZE_W:int = 30; // 横のマスサイズ
private const MASUSIZE_H:int = 30; // 縦のマスサイズ
private const NUM_BOMB:int = 2; // 爆弾の数
private var blocks:Array; // クリックするブロックを入れる二次元配列
private var map:Array; // 爆弾の位置が入っている二次元配列
private var clickable:Boolean = false; // clickableがfalseだとクリックしても反応しないようにする
private var openCount:int = 0; // 開いたブロックの数
public function Main()
{
var bombMap:Array = [];
var bombIndex:int = 0;
for (var i:int = 0; i < WIDTH * HEIGHT; i++)
{
if (bombIndex++ < NUM_BOMB) bombMap[i] = Status.BOMB;
else bombMap[i] = Status.FIELD;
}
bombMap = Random.shake(bombMap);
map = [];
for (var y:int = 0; y < HEIGHT; y++)
{
map[y] = [];
for (var x:int = 0; x < WIDTH; x++)
{
map[y][x] = bombMap[y * WIDTH + x];
}
}
for (y = 0; y < HEIGHT; y++)
{
for (x = 0; x < WIDTH; x++)
{
if (map[y][x] != Status.BOMB) continue;
for (var yy:int = -1; yy <= 1; yy++)
{
for (var xx:int = -1; xx <= 1; xx++)
{
if (yy == 0 && xx == 0) continue;
if (y + yy < 0 || HEIGHT <= y + yy || x + xx < 0 || WIDTH <= x + xx) continue;
if (map[y + yy][x + xx] != Status.BOMB) map[y + yy][x + xx]++;
}
}
}
}
for (y = 0; y < HEIGHT; y++)
{
for (x = 0; x < WIDTH; x++)
{
graphics.lineStyle(2.0);
graphics.drawRect(x * MASUSIZE_W, y * MASUSIZE_H, MASUSIZE_W, MASUSIZE_H);
var char:String = "*";
if (map[y][x] >= 1) char = map[y][x].toString();
if (map[y][x] != 0)
{
var tf:TextField = Utils.createTextField(char, 20, 0x0);
tf.y = y * MASUSIZE_H;
tf.x = x * MASUSIZE_W + 7;
addChild(tf);
}
}
}
blocks = [];
for (y = 0; y < HEIGHT; y++)
{
blocks[y] = [];
for (x = 0; x < WIDTH; x++)
{
var block:Block = new Block(MASUSIZE_W, MASUSIZE_H);
block.y = y * MASUSIZE_H;
block.x = x * MASUSIZE_W;
addChild(block);
blocks[y][x] = block;
}
}
clickable = true;
stage.addEventListener(MouseEvent.CLICK, onMouseClick);
}
private function openBlock(y:int, x:int):void
{
for (var yy:int = -1; yy <= 1; yy++)
{
for (var xx:int = -1; xx <= 1; xx++)
{
if (yy == 0 && xx == 0) continue;
var ty:int = y + yy;
var tx:int = x + xx;
if (ty < 0 || HEIGHT <= ty || tx < 0 || WIDTH <= tx) continue;
if (map[ty][tx] != Status.BOMB)
{
var block:Block = blocks[ty][tx];
if (block != null)
{
openCount++;
removeChild(block);
blocks[ty][tx] = null;
}
else continue;
}
if (map[ty][tx] == Status.FIELD) openBlock(ty, tx);
}
}
}
private function onMouseClick(event:MouseEvent):void
{
if (!clickable) return;
var ty:int = mouseY / MASUSIZE_H;
var tx:int = mouseX / MASUSIZE_W;
if (ty < 0 || HEIGHT <= ty || tx < 0 || WIDTH <= tx) return;
var block:Block = blocks[ty][tx];
if (block != null)
{
if (event.shiftKey)
{
block.setFlag(!block.flag);
return;
}
else if (block.flag) return;
removeChild(block);
blocks[ty][tx] = null;
}
else return;
if (map[ty][tx] == Status.BOMB)
{
var gameOverTF:TextField = Utils.createTextField("GAME OVER", 20, 0x0);
gameOverTF.x = 100;
gameOverTF.y = 420;
addChild(gameOverTF);
clickable = false;
return;
}
if (map[ty][tx] == Status.FIELD) openBlock(ty, tx);
openCount++;
if (openCount == WIDTH * HEIGHT - NUM_BOMB)
{
var clearTF:TextField = Utils.createTextField("CLEAR", 20, 0x0);
clearTF.x = 100;
clearTF.y = 420;
addChild(clearTF);
clickable = false;
return;
}
}
}
}
import flash.display.Sprite;
class Block extends Sprite
{
public var flag:Boolean = false;
public var w:Number;
public var h:Number;
public function Block(width:Number, height:Number)
{
this.w = width;
this.h = height;
setFlag(false);
}
public function setFlag(flag:Boolean):void
{
this.flag = flag;
graphics.clear();
if (flag)
{
graphics.lineStyle(2.0);
graphics.beginFill(0xFF9900);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
}
else
{
graphics.lineStyle(2.0);
graphics.beginFill(0x888888);
graphics.drawRect(0, 0, w, h);
graphics.endFill();
}
}
}
import flash.text.TextField;
import flash.text.TextFormat;
class Utils
{
public static function createTextField(text:String, size:int, color:int):TextField
{
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat("Consolas,メイリオ,_typeWriter", size, color, true);
tf.autoSize = "left";
tf.selectable = false;
tf.text = text;
return tf;
}
}
class Status
{
public static const FIELD:int = 0;
public static const BOMB:int = -1;
}