Fill the Grid! 15x15
あまり深く考えずに、このゲームの制限時間をTimerEventで制御していたところ、
「ゲームの制限時間をTimerEventで制御するのは処理落ちで時間が伸びるので良くない」
とのご指摘がありましたので修正しました。
(修正前のコードはコメントアウトしています)
マウスを使って60秒間ひたすらマス目の中を30パーセント以上塗りつぶすだけという
ものすごーく退屈な、罰ゲームか、なにかの修行のようなゲームです。
/**
* Copyright mousepancyo ( http://wonderfl.net/user/mousepancyo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wWb7
*/
/*
あまり深く考えずに、このゲームの制限時間をTimerEventで制御していたところ、
「ゲームの制限時間をTimerEventで制御するのは処理落ちで時間が伸びるので良くない」
とのご指摘がありましたので修正しました。
(修正前のコードはコメントアウトしています)
*/
/*
マウスを使って60秒間ひたすらマス目の中を30パーセント以上塗りつぶすだけという
ものすごーく退屈な、罰ゲームか、なにかの修行のようなゲームです。
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getTimer;
[SWF(width="465", height="465", backgroundColor="0", frameRate="30")]
public class Main extends Sprite{
private const XNUM:int = 15;
private const YNUM:int = 15;
private const GAME_TIME:int = 60;
private var _startTime:int;
private var _currentTime:Date;
private var _rectList:Vector.<Rect> = new Vector.<Rect>();
//private var _timer:Timer
//private var _count:int
private var _countOK:int;
private var _startView:StartView;
private var _title:TextField = new TextField()
private var _titlefmt:TextFormat = new TextFormat("_typewriter", 16, 0xCC0000, true, false, false, null, null, "center", null, null, null, 2)
private var _tx:TextField = new TextField()
private var _fmt:TextFormat = new TextFormat("_typewriter", 14, 0x666666, true, false, false, null, null, "center", null, null, null, 2)
private var _scoreBoard:ScoreAPI
public function Main() {
var base:Base = new Base(0xF3F3F3, stage.stageWidth, stage.stageHeight)
addChild(base)
//
_title.defaultTextFormat = _titlefmt
_title.selectable = false
_title.y = 10
_title.width = 465
_title.height = 30
_title.text = "Fill the Grid! 15x15"
addChild(_title)
//
_tx.defaultTextFormat = _fmt
_tx.selectable = false
_tx.y = 430
_tx.width = 465
_tx.height = 30
addChild(_tx)
//
createRect(48, 40)
//
_startView = new StartView()
addChild(_startView)
_startView.addEventListener(MouseEvent.CLICK, startGame)
//
_scoreBoard = new ScoreAPI(stage)
}
private function createRect(ofsetX:int, ofsetY:int):void{
for(var i:int=0; i<YNUM; i++){
for(var j:int=0; j<XNUM; j++){
var rect:Rect = new Rect()
addChild(rect)
rect.x = j * 25 + ofsetX
rect.y = i * 25 + ofsetY
rect.addEventListener(MouseEvent.MOUSE_DOWN, rect.onMousedowm)
rect.addEventListener(MouseEvent.MOUSE_UP, rect.onMouseup)
rect.addEventListener(MouseEvent.MOUSE_OUT, rect.onMouseup)
_rectList.push(rect)
}
}
}
private function drawCheck(rect:Rect, w:Number, h:Number):Number{
var n:Number = 0
var bmd:BitmapData = new BitmapData(w, h, false, 0xFFFFFF)
bmd.draw(rect)
//
for(var i:int=0; i<w; i++){
for(var j:int=0; j<h; j++){
if(bmd.getPixel(j, i) != 0xFFFFFF){
n++
}
}
}
return n / (w * h) * 100
}
private function startGame(e:MouseEvent):void{
_startView.removeEventListener(MouseEvent.CLICK, startGame)
_startView.visible = false
_countOK = 0
_tx.text = "Time remaining: 60 sec"
//
_startTime = getTimer()
_currentTime = new Date()
addEventListener(Event.ENTER_FRAME, countDown)
}
private function countDown(e:Event):void{
var time:int = (getTimer() - _startTime)/1000
//
if(GAME_TIME - time >= 10){
_tx.text = "Time remaining: " + (GAME_TIME - time) + " sec"
}else if(GAME_TIME - time < 10 && GAME_TIME - time >= 0){
_tx.text = "Time remaining: 0" + (GAME_TIME - time) + " sec"
}else{
endGame()
}
//
if(_currentTime > new Date()){
endGame()
}else{
_currentTime = new Date()
}
}
private function endGame():void{
removeEventListener(Event.ENTER_FRAME, countDown)
//
_tx.text = "TIME UP!"
for(var i:int=0; i<_rectList.length; i++){
if(drawCheck(_rectList[i], 20, 20) > 30){
_countOK++
}
}
//
var timer:Timer = new Timer(1000, 1)
timer.addEventListener(TimerEvent.TIMER_COMPLETE, delayComplete)
timer.start()
function delayComplete(e:TimerEvent):void{
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, delayComplete)
timer = null
addScore(_countOK)
}
}
/*private function startGame(e:MouseEvent):void{
_startView.removeEventListener(MouseEvent.CLICK, startGame)
_startView.visible = false
//
_count = 0
_countOK = 0
//
_timer = new Timer(1000, GAME_TIME)
_timer.addEventListener(TimerEvent.TIMER, countDown)
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, endGame)
_timer.start()
_tx.text = "Time remaining: " + (GAME_TIME - _count) + " sec"
}
private function countDown(e:TimerEvent):void{
_count++
if(GAME_TIME - _count >= 10){
_tx.text = "Time remaining: " + (GAME_TIME - _count) + " sec"
}else{
_tx.text = "Time remaining: 0" + (GAME_TIME - _count) + " sec"
}
}
private function endGame(e:TimerEvent):void{
_tx.text = "TIME UP!"
_timer.removeEventListener(TimerEvent.TIMER, countDown)
_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, endGame)
_timer = null
for(var i:int=0; i<_rectList.length; i++){
if(drawCheck(_rectList[i], 20, 20) > 30){
_countOK++
}
}
//
var timer:Timer = new Timer(1000, 1)
timer.addEventListener(TimerEvent.TIMER_COMPLETE, delayComplete)
timer.start()
function delayComplete(e:TimerEvent):void{
timer.removeEventListener(TimerEvent.TIMER_COMPLETE, delayComplete)
timer = null
addScore(_countOK)
}
}*/
private function addScore(score:int):void{
_scoreBoard.showScoreForm(score)
_scoreBoard.addEventListener(ScoreAPI.CLOSED, rankingClosed)
}
private function rankingClosed(e:Event):void{
_scoreBoard.removeEventListener(ScoreAPI.CLOSED, rankingClosed)
_startView.addEventListener(MouseEvent.CLICK, startGame)
_startView.visible = true
//
for(var i:int=0; i<_rectList.length; i++){
_rectList[i].removed()
}
}
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.events.MouseEvent;
class Rect extends Sprite{
private var _canvas:Shape = new Shape()
public function Rect(){
var s:Shape = new Shape()
addChild(s)
s.graphics.beginFill(0xFFFFFF)
s.graphics.lineStyle(1,0x999999)
s.graphics.drawRect(0,0,20,20)
s.graphics.endFill()
//
addChild(_canvas)
_canvas.graphics.clear()
_canvas.graphics.lineStyle(2,Math.random()*0xFF0000)
}
private function startPaint(e:Event):void{
_canvas.graphics.lineTo(mouseX, mouseY)
}
public function onMousedowm(e:MouseEvent):void{
_canvas.graphics.moveTo(mouseX, mouseY)
addEventListener(Event.ENTER_FRAME, startPaint)
}
public function onMouseup(e:MouseEvent):void{
removeEventListener(Event.ENTER_FRAME, startPaint)
}
public function removed():void{
_canvas.graphics.clear()
_canvas.graphics.lineStyle(2,Math.random()*0xFF0000)
}
}
//
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
class StartView extends Sprite{
public function StartView(){
var tx:TextField = new TextField()
var fmt:TextFormat = new TextFormat("_typewriter", 14, 0xFFFFFF, true, false, false, null, null, "center", null, null, null, 6)
//
graphics.beginFill(0, 0.8)
graphics.drawRect(0, 0, 465, 465)
graphics.endFill()
//
tx.defaultTextFormat = fmt
tx.selectable = false
tx.y = 100
tx.width = 465
tx.height = 300
tx.text = "----- Fill the Grid! 15x15 -----\n\n\nマス目の中を60秒間ひたすら塗りつぶすだけのゲーム。\nマス目はマウスで塗りつぶすことができます。\n1マスに対して30パーセント以上塗りつぶしたマスの数が\n終了後に結果としてカウント・表示されます。\n(一応ランキング機能付き)\n\n\n----- 画面クリックでスタートです -----"
addChild(tx)
}
}
//class Base
import flash.display.Sprite
class Base extends Sprite{
public function Base(color:int,w:int,h:int):void{
graphics.beginFill(color, 1)
graphics.drawRect(0,0,w,h)
graphics.endFill()
}
}
//class ScoreAPI
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.display.DisplayObjectContainer;
import flash.display.Stage;
import flash.utils.Timer;
import com.bit101.components.Component;
import com.bit101.components.PushButton;
import net.wonderfl.score.basic.BasicScoreForm;
import net.wonderfl.score.basic.BasicScoreRecordViewer;
class ScoreAPI extends EventDispatcher {;
private var _form:BasicScoreForm;
private var _ranking:BasicScoreRecordViewer
private var _stage:Stage
private var _timer:Timer
public static const CLOSED:String = "closed"
public function ScoreAPI(target:Stage) {
_stage = target
Component.initStage(_stage);
}
public function showScoreForm(score:int):void{
_form = new BasicScoreForm(_stage, (465-BasicScoreForm.WIDTH)/2, (465-BasicScoreForm.HEIGHT)/2, score, 'SAVE SCORE', showRanking);
}
private function showRanking($didSavedScore:Boolean):void {
//removes form
_stage.removeChild(_form);
//
_ranking = new BasicScoreRecordViewer(_stage, (465-BasicScoreRecordViewer.WIDTH)/2,(465-BasicScoreRecordViewer.HEIGHT)/2,'RANKING', 99, true, onClosed);
}
private function onClosed():void{
_stage.removeChild(_ranking)
//
_timer = new Timer(300, 1)
_timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete)
_timer.start()
}
private function timerComplete(e:TimerEvent):void{
_timer.removeEventListener(TimerEvent.TIMER_COMPLETE, timerComplete)
_timer = null
dispatchEvent(new Event(ScoreAPI.CLOSED))
}
}