• 沒有找到結果。

四方連塊拼圖遊戲

第四章 數位化數學遊戲的程式碼說明

第三節 四方連塊拼圖遊戲

四方連塊拼圖遊戲設計流程:(1)設定遊戲所需要的方格個數,再把這些方 格依序擺放到遊戲面板上,當成遊戲進行時,拼圖塊放上所要貼齊的格線。(2)

在元件庫當中,將每一個四方連塊寫成一個影片片段:利用座標平面的原理,先 決定拼圖的原點,再按照逆(順)時針方向,設定每一個頂點座標。拼圖的原點 將用在旋轉、翻轉以及貼齊格線時的參考點。(3)將所有四方連塊放置到遊戲區 塊中,並設定為按鈕,寫入操作這些按鈕的動作:點擊滑鼠左鍵並拖曳即可移動 拼圖,放開時就會自動貼齊格線。快速點擊滑鼠左鍵兩下,即可旋轉拼圖。滾動 滑鼠滾輪,可讓拼圖翻轉。(4)撰寫遊戲成功條件:每次完成操作拼圖的動作,

就立即計算共有多少塊拼圖是符合放置於粗黑方匡中,而且不和其他拼圖重疊,

若所有四方連塊拼圖都滿足條件,就算成功。

一、面版佈置-第一影格

stop();

h = 6; //設定遊戲方框的長為 6 v = 6; //設定遊戲方框的寬為 6 t = h*v; //設定遊戲方框總數

//將元件庫中的標題以及開始、重來按鈕,提取擺放到操作區塊中

msak1 = _root.attachMovie("msak1", "msak1", _root.getNextHighestDepth

(), {_x:100, _y:58});

restart1 = _root.attachMovie("restart1", "restart1", _root.getNextHighestDepth(), {_x:-50, _y:-10});

restart1._xscale = 70;

restart1._yscale = 70;

//設定重來按鈕按下之後的動作:將所有四方連塊及偵測版、直線、橫線、粗黑 方框移除,並回到影格 1

restart1.onPress = function() { for (a=1; a<200; a++) {

restart1.onRollOver = function() {this.gotoAndStop(2);};

restart1.onRollOut = function() {this.gotoAndStop(1);};

//依序產生 line1…line4 的影片片段,將粗黑方框的外圍包覆,藉以判斷四方 連塊是否放置在方框之內。

this.createEmptyMovieClip("line1",this.getNextHighestDepth());

line1.beginFill(0xFF0000,70);

this.createEmptyMovieClip("line2",this.getNextHighestDepth());

this.createEmptyMovieClip("line3",this.getNextHighestDepth());

line3.beginFill(0xFF0000,70);

this.createEmptyMovieClip("line4",this.getNextHighestDepth());

line4.beginFill(0xFF0000,70);

line4.endFill();

粗黑方框四周的 line1~line4 所在位置 line1._alpha = 0; //將影片片段設定為可見度 0

line2._alpha = 0; //將影片片段設定為可見度 0 line3._alpha = 0; //將影片片段設定為可見度 0 line4._alpha = 0; //將影片片段設定為可見度 0

//利用 for 迴圈排放直線與橫線於遊戲面板上,排列切割成遊戲所需的正方形方 框,並設定每一個正方形邊長為 56

for (a=0; a<7; a++) {

var line = _root.attachMovie("linev", "linev"+(a+1), _root.getNextHighestDepth());

line._x = 56*a+184;

line._y = 150;

line.gotoAndStop(1);}

for (a=0; a<7; a++) {

var line = _root.attachMovie("lineh", "lineh"+(a+1),

_root.getNextHighestDepth());

line._x = 185;

line._y = 56*a+148;

line.gotoAndStop(1);}

//製作一個名為 square_mc 的影片片段,影片深度自訂,將前述排列完成的正方 形面板加上粗黑的方框

this.createEmptyMovieClip("square_mc",this.getNextHighestDepth());

square_mc.lineStyle(4,0x000000); 訂,命名為 btn1、btn2……btn9,這將是螢幕上可供移動、旋轉、翻轉的按鈕 btn1 = _root.attachMovie("sq6", "sq6", _root.getNextHighestDepth(), {_x:800, _y:260});

btn2 = _root.attachMovie("sq7", "sq7", _root.getNextHighestDepth(), {_x:72, _y:540});

btn3 = _root.attachMovie("sq8", "sq8", _root.getNextHighestDepth(), {_x:632, _y:540});

btn4 = _root.attachMovie("sq9", "sq9", _root.getNextHighestDepth(), {_x:744, _y:428});

btn5 = _root.attachMovie("sq10", "sq10", _root.getNextHighestDepth(), {_x:632, _y:204});

btn6 = _root.attachMovie("sq11", "sq11", _root.getNextHighestDepth(),

{_x:72, _y:372});

btn7 = _root.attachMovie("sq12", "sq12", _root.getNextHighestDepth(), {_x:800, _y:540});

btn8 = _root.attachMovie("sq13", "sq13", _root.getNextHighestDepth(), {_x:352, _y:596});

btn9 = _root.attachMovie("sq14", "sq14", _root.getNextHighestDepth(), {_x:72, _y:204});

sq6._yscale = -100; //將按鈕翻轉

btn1c = _root.attachMovie("sq6c", "sq6c", _root.getNextHighestDepth

(), {_x:btn1._x, _y:btn1._y});

btn2c = _root.attachMovie("sq7c", "sq7c", _root.getNextHighestDepth

(), {_x:btn2._x, _y:btn2._y});

btn3c = _root.attachMovie("sq8c", "sq8c", _root.getNextHighestDepth

(), {_x:btn3._x, _y:btn3._y});

btn4c = _root.attachMovie("sq9c", "sq9c", _root.getNextHighestDepth

(), {_x:btn4._x, _y:btn4._y});

btn5c = _root.attachMovie("sq10c", "sq10c", _root.getNextHighestDepth

(), {_x:btn5._x, _y:btn5._y});

btn6c = _root.attachMovie("sq11c", "sq11c", _root.getNextHighestDepth

(), {_x:btn6._x, _y:btn6._y});

btn7c = _root.attachMovie("sq12c", "sq12c", _root.getNextHighestDepth

(), {_x:btn7._x, _y:btn7._y});

btn8c = _root.attachMovie("sq13c", "sq13c", _root.getNextHighestDepth

(), {_x:btn8._x, _y:btn8._y});

btn9c = _root.attachMovie("sq14c", "sq14c", _root.getNextHighestDepth

(), {_x:btn9._x, _y:btn9._y});

未將四方連塊偵測版隱藏時情況 btn1c._alpha = 0; //將按鈕設定為可見度 0

btn2c._alpha = 0; //將按鈕設定為可見度 0 btn3c._alpha = 0; //將按鈕設定為可見度 0 btn4c._alpha = 0; //將按鈕設定為可見度 0 btn5c._alpha = 0; //將按鈕設定為可見度 0 btn6c._alpha = 0; //將按鈕設定為可見度 0 btn7c._alpha = 0; //將按鈕設定為可見度 0

btn8c._alpha = 0; //將按鈕設定為可見度 0 btn9c._alpha = 0; //將按鈕設定為可見度 0 sq6c._yscale = -100;

sq7c._yscale = -100;

sq11c._yscale = -100;

sq9c._rotation = -90;

sq13c._rotation = -90;

sq10c._rotation = -90;

二、四方連塊遊戲按鈕製作-btn1

//將所要製作的四方連塊視為凹多邊形,先決定原點位置,再將每個頂點座標 化。從任意一個頂點開始描點,並搭配底圖正方格的大小來製作,即可正確畫出。

stop();

this.createEmptyMovieClip("square_mc",this.getNextHighestDepth());

square_mc.beginFill(0xFFFF00,70); //圍成區域填入顏色,透明度為 70

square_mc.lineStyle(2,0x000000); //以單位 2 的黑色線圍出區域 square_mc.moveTo(-1*56,2*56); //(-1,2)

square_mc.lineTo(-1*56,-1*56); //(-1,-1)

square_mc.lineTo(1*56,-1*56); //(1,-1)

square_mc.lineTo(1*56,0); //(1,0)

square_mc.lineTo(0,0); //原點 square_mc.lineTo(0,2*56); //(0,2)

square_mc.endFill();

三、遊戲操作-第二影格

//設定若滑鼠移動到 btn1 上時,給定 pas 的數值為 1;若移出 btn1 時,給定 pas 的數值為 0,藉此判斷將操作哪一個四方連塊。

btn1.onRollOver = function() {pas = 1;};

btn1.onRollOut = function() {pas = 0;};

btn2.onRollOver = function() {pas = 2;};

btn2.onRollOut = function() {pas = 0;};

btn3.onRollOver = function() {pas = 3;};

btn3.onRollOut = function() {pas = 0;};

btn4.onRollOver = function() {pas = 4;};

btn4.onRollOut = function() {pas = 0;};

btn5.onRollOver = function() {pas = 5;};

btn5.onRollOut = function() {pas = 0;};

btn6.onRollOver = function() {pas = 6;};

btn6.onRollOut = function() {pas = 0;};

btn7.onRollOver = function() {pas = 7;};

btn7.onRollOut = function() {pas = 0;};

btn8.onRollOver = function() {pas = 8;};

btn8.onRollOut = function() {pas = 0;};

btn9.onRollOver = function() {pas = 9;};

btn9.onRollOut = function() {pas = 0;};

//當滑鼠移動到 btn1 上並按下滑鼠時,即可拖曳移動位置 btn1.onMouseDown = function() {

if (pas == 1) { originx = this._x;

originy = this._y;

btn1.onMouseUp = function() { this.stopDrag();

btn2.onMouseDown = function() { if (pas == 2) {

originx = this._x;

originy = this._y;

this.startDrag();

this.swapDepths(10000);

btn2test();

count();}};

btn2.onMouseUp = function() { this.stopDrag();

px = this._x;

py = this._y;

ax = Math.ceil(px/56);

ay = Math.ceil(py/56);

newx = ax*56-40;

var num_clickSPD:Number = 150;

var ob_Date_milliSecond:Date = new Date();

var rec_Date_milliSecond:Number = ob_Date_milliSecond.getTime();

btn1.onPress = function() { if (this.switch_1 != 1) {

ob_Date_milliSecond = new Date();

if (ob_Date_milliSecond.getTime()-rec_Date_milliSecond<num_clickSPD)

{

btn1._rotation += 90;

btn1.swapDepths(10000);

btn1c._rotation += 90;

btn1c.swapDepths(10000);

this.switch_1 = 0;

btn1test();

count();}}};

btn1.onRelease = function() { if (this.switch_1 != 1) {

ob_Date_milliSecond = new Date();

rec_Date_milliSecond = ob_Date_milliSecond.getTime();}

btn1test();

count();};

//其他四方連塊旋轉亦是同樣語法,這裡僅列出至 btn2 為止。

btn2.onPress = function() { if (this.switch_1 != 1) {

ob_Date_milliSecond = new Date();

if (ob_Date_milliSecond.getTime()-rec_Date_milliSecond<num_clickSPD)

{

btn2._rotation += 90;

btn2.swapDepths(10000);

btn2c._rotation += 90;

btn2c.swapDepths(10000);

this.switch_1 = 0;

btn2test();

count();}}};

btn2.onRelease = function() { if (this.switch_1 != 1) {

ob_Date_milliSecond = new Date();

rec_Date_milliSecond = ob_Date_milliSecond.getTime();}

btn2test();

count();};

//當滑鼠移動到 btn1 上並滾動滑鼠滾輪時,btn1 將翻轉 180 度。翻轉後並偵測 與計算 btn1 是否擺放到外框之內或是否和其他方塊有重疊

var mouseListener:Object = new Object();

mouseListener.onMouseWheel = function(delta:Number) { if (pas == 1) {

btn1._yscale = 100*q;

btn1c._yscale = 100*q;

btn1test();

count();}};

Mouse.addListener(mouseListener);

//其他四方連塊翻轉亦是同樣語法,這裡僅列出至 btn2 為止。

var mouseListener:Object = new Object();

mouseListener.onMouseWheel = function(delta:Number) { if (pas == 2) {

q = delta;

if (q<0) { q = -1;}

if (q>0) { q = 1;}

btn2._yscale = 100*q;

btn2c._yscale = 100*q;

btn2test();

count();}};

Mouse.addListener(mouseListener);

fscommand("allowscale", false); //設定畫面以 100%大小顯示,不需縮放。

四、判斷獲勝或停止條件

//設定一個新的陣列 matp,此陣列有 9 個元素,matp[1]……matp[9],每個元 素的初始值為 0

var matp = new Array(9);

for (i=1; i<10; i += 1) { matp[i] = 0;}

import com.gskinner.sprites.CollisionDetection;

//設定 btn1 的判斷函數,用以判斷是否在粗黑外框之內,以及是否和其他四方 連塊有重疊,若都符合條件,則給定 matp[1]的數值為 0,反之,則為 1。hitTest 為碰撞測試。

function btn1test() {

if (btn1c.hitTest(line1) || btn1c.hitTest(line2) || btn1c.hitTest

(line3) || btn1c.hitTest(line4)) { matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn2c, 10000)) {

matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn3c, 10000)) {

matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn4c, 10000)) {

matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn5c, 10000)) {

matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn6c, 10000)) {

matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn7c, 10000)) {

matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn8c, 10000)) {

matp[1] = 1;

} else if (CollisionDetection.checkForCollision(btn1, btn9c, 10000)) {

function count() {

s=matp[1]+matp[2]+matp[3]+matp[4]+matp[5]+matp[6]+matp[7]+matp[8]+

相關文件