DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> HTML基礎知識 >> HTML5詳解 >> html5記憶翻牌游戲實現思路及代碼
html5記憶翻牌游戲實現思路及代碼
編輯:HTML5詳解     

翻開的2張牌如果配對就會消除,否則2張牌都會返回背面。 

需求分析 

怎麼繪制正的牌面和背的牌面及配對成功後怎麼消除牌面 
怎麼生成牌組並且確定每張牌的位置和對應的圖片 

怎麼洗牌 
怎麼記錄牌組的配對信息 
怎麼確定點擊事件是第一次點擊還是第二次點擊 

作弊事件的處理:兩次點擊同一張牌 點擊已經消除的牌面 點擊牌面之外的區域 
牌翻開之後需要給一定時間讓玩家看清楚,怎麼實現暫停 

鼠標點擊事件的響應及獲得鼠標點擊位置的坐標從而確定點擊的是哪一張牌 

MYCode: 

復制代碼代碼如下:
<html XMLns="http://www.w3.org/1999/xHtml"> 
<head> 
<meta charset="utf-8" /> 
<title>test </title> 
<script type="text/Javascript"> 
var ctx; 
var canvas; 
var card; 
var first_pick = true; //第一次點擊的標志 
var first_card = -1; 
var second_card; 
var back_color = "rgb(255,0,0)"; //卡片背面顏色 
var table_color = "#FFF"; 
var deck = []; //note 
var first_x = 10; 
var first_y = 50; 
var margin = 30; 
var card_width = 50; 
var card_height = 50; 
var pairs = [ 
["1_a.jpg", "1_b.jpg"], 
["2_a.jpg", "2_b.jpg"], 
["3_a.jpg", "3_b.jpg"], 
["4_a.jpg", "4_b.jpg"], 
["5_a.jpg", "5_b.jpg"] 
]; 
function draw_back()//繪制卡片背面 

ctx.fillStyle = back_color; 
ctx.fillRect(this.sx, this.sy, this.swidth, this.sheight); 

function Card(sx, sy, swidth, sheight, img, info)//構造函數 

this.sx = sx; 
this.sy = sy; 
this.swidth = swidth; 
this.sheight = sheight; 
this.info = info; 
this.img = img; 
this.draw = draw_back; 

function make_deck()//生成卡組並繪制 

var i; 
var a_card; 
var b_card; 
var a_pic; 
var b_pic; 
var cx = first_x; 
var cy = first_y; 
for (i = 0; i < pairs.length; i++) 

a_pic = new Image(); 
a_pic.src = pairs[i][0]; 
a_card = new Card(cx, cy, card_width, card_height, a_pic, i); 
deck.push(a_card); 
b_pic = new Image(); 
b_pic.src = pairs[i][1]; 
b_card = new Card(cx, cy + card_height + margin, card_width, card_height, b_pic, i); 
deck.push(b_card); 
cx = cx + card_width + margin; //note 
a_card.draw(); 
b_card.draw(); 


function shuffle()//洗牌 

var i; 
var j; 
var temp_info; 
var temp_img; 
var deck_length = deck.length; 
var k; 
for (k = 0; k < 3 * deck_length; k++) 

i = Math.floor(Math.random() * deck_length); 
j = Math.floor(Math.random() * deck_length); 
temp_info = deck[i].info; 
temp_img = deck[i].img; 
deck[i].info = deck[j].info; 
deck[i].img = deck[j].img; 
deck[j].info = temp_info; 
deck[j].img = temp_img; 


function choose(ev) 

//var out; 
var mx; 
var my; 
//var pick1; 
//var pick2; 
var i; 
//note 
if (ev.layerX || ev.layerX == 0) { // Firefox 
mx = ev.layerX; 
my = ev.layerY; 
} else if (ev.offsetX || ev.offsetX == 0) { // Opera 
mx = ev.offsetX; 
my = ev.offsetY; 

for (i = 0; i < deck.length; i++) 

card = deck[i]; 
if (card.sx >= 0)//牌未被消除 

//判斷點擊的是哪一張牌 
if (mx > card.sx && mx < card.sx + card.swidth && my > card.sy && my < card.sy + card.sheight) 

if (i != first_card)//如果兩次點擊同一張牌不做處理 
break; 



if (i < deck.length) 

if (first_pick)//如果是第一次點擊 

first_card = i; 
first_pick = false; //note 
ctx.drawImage(card.img, card.sx, card.sy, card.swidth, card.sheight); 

else 

first_pick = true; //note 
second_card = i; 
ctx.drawImage(card.img, card.sx, card.sy, card.swidth, card.sheight); 
tid=setTimeout(flip_back,1000); 



function flip_back() 

if (card.info == deck[first_card].info)//配對成功 

ctx.fillStyle = table_color; 
ctx.fillRect(deck[first_card].sx, deck[first_card].sy, deck[first_card].swidth, deck[first_card].sheight); 
ctx.fillRect(deck[second_card].sx, deck[second_card].sy, deck[second_card].swidth, deck[second_card].sheight); 
deck[first_card].sx = -1; 
deck[second_card].sy = -1; 
first_card=-1; 

else 

deck[first_card].draw(); 
deck[second_card].draw(); 
first_card=-1; 


function init() 

canvas = document.getElementById('canvas'); 
canvas.addEventListener('click', choose, false); 
ctx = canvas.getContext('2d'); 
make_deck(); 
shuffle(); 

</script> 
</head> 
<body onLoad="init();"> 
<canvas id="canvas" width="400" height="400"/> 
123142 
</body> 
</Html> 
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved