翻开的2张牌如果配对就会消除,否则2张牌都会返回背面。 需求分析 怎么绘制正的牌面和背的牌面及配对成功后怎么消除牌面 怎么生成牌组并且确定每张牌的位置和对应的图片 怎么洗牌 怎么记录牌组的配对信息 怎么确定点击事件是第一次点击还是第二次点击 作弊事件的处理:两次点击同一张牌 点击已经消除的牌面 点击牌面之外的区域 牌翻开之后需要给一定时间让玩家看清楚,怎么实现暂停 鼠标点击事件的响应及获得鼠标点击位置的坐标从而确定点击的是哪一张牌 MYCode: 复制代码代码如下: test <br>var ctx; <br>var canvas; <br>var card; <br>var first_pick = true; //第一次点击的标志 <br>var first_card = -1; <br>var second_card; <br>var back_color = "rgb(255,0,0)"; //卡片背面颜色 <br>var table_color = "#FFF"; <br>var deck = []; //note <br>var first_x = 10; <br>var first_y = 50; <br>var margin = 30; <br>var card_width = 50; <br>var card_height = 50; <br>var pairs = [ <br>["1_a.jpg", "1_b.jpg"], <br>["2_a.jpg", "2_b.jpg"], <br>["3_a.jpg", "3_b.jpg"], <br>["4_a.jpg", "4_b.jpg"], <br>["5_a.jpg", "5_b.jpg"] <br>]; <br>function draw_back()//绘制卡片背面 <br>{ <br>ctx.fillStyle = back_color; <br>ctx.fillRect(this.sx, this.sy, this.swidth, this.sheight); <br>} <br>function Card(sx, sy, swidth, sheight, img, info)//构造函数 <br>{ <br>this.sx = sx; <br>this.sy = sy; <br>this.swidth = swidth; <br>this.sheight = sheight; <br>this.info = info; <br>this.img = img; <br>this.draw = draw_back; <br>} <br>function make_deck()//生成卡组并绘制 <br>{ <br>var i; <br>var a_card; <br>var b_card; <br>var a_pic; <br>var b_pic; <br>var cx = first_x; <br>var cy = first_y; <br>for (i = 0; i < pairs.length; i ) <br />{ <br />a_pic = new Image(); <br />a_pic.src = pairs[i][0]; <br />a_card = new Card(cx, cy, card_width, card_height, a_pic, i); <br />deck.push(a_card); <br />b_pic = new Image(); <br />b_pic.src = pairs[i][1]; <br />b_card = new Card(cx, cy card_height margin, card_width, card_height, b_pic, i); <br />deck.push(b_card); <br />cx = cx card_width margin; //note <br />a_card.draw(); <br />b_card.draw(); <br />} <br />} <br />function shuffle()//洗牌 <br />{ <br />var i; <br />var j; <br />var temp_info; <br />var temp_img; <br />var deck_length = deck.length; <br />var k; <br />for (k = 0; k < 3 * deck_length; k ) <br />{ <br />i = Math.floor(Math.random() * deck_length); <br />j = Math.floor(Math.random() * deck_length); <br />temp_info = deck[i].info; <br />temp_img = deck[i].img; <br />deck[i].info = deck[j].info; <br />deck[i].img = deck[j].img; <br />deck[j].info = temp_info; <br />deck[j].img = temp_img; <br />} <br />} <br />function choose(ev) <br />{ <br />//var out; <br />var mx; <br />var my; <br />//var pick1; <br />//var pick2; <br />var i; <br />//note <br />if (ev.layerX || ev.layerX == 0) { // Firefox <br />mx = ev.layerX; <br />my = ev.layerY; <br />} else if (ev.offsetX || ev.offsetX == 0) { // Opera <br />mx = ev.offsetX; <br />my = ev.offsetY; <br />} <br />for (i = 0; i < deck.length; i ) <br />{ <br />card = deck[i]; <br />if (card.sx >= 0)//牌未被消除 <br>{ <br>//判断点击的是哪一张牌 <br>if (mx > card.sx && mx < card.sx card.swidth && my > card.sy && my < card.sy card.sheight) <br />{ <br />if (i != first_card)//如果两次点击同一张牌不做处理 <br />break; <br />} <br />} <br />} <br />if (i < deck.length) <br />{ <br />if (first_pick)//如果是第一次点击 <br />{ <br />first_card = i; <br />first_pick = false; //note <br />ctx.drawImage(card.img, card.sx, card.sy, card.swidth, card.sheight); <br />} <br />else <br />{ <br />first_pick = true; //note <br />second_card = i; <br />ctx.drawImage(card.img, card.sx, card.sy, card.swidth, card.sheight); <br />tid=setTimeout(flip_back,1000); <br />} <br />} <br />} <br />function flip_back() <br />{ <br />if (card.info == deck[first_card].info)//配对成功 <br />{ <br />ctx.fillStyle = table_color; <br />ctx.fillRect(deck[first_card].sx, deck[first_card].sy, deck[first_card].swidth, deck[first_card].sheight); <br />ctx.fillRect(deck[second_card].sx, deck[second_card].sy, deck[second_card].swidth, deck[second_card].sheight); <br />deck[first_card].sx = -1; <br />deck[second_card].sy = -1; <br />first_card=-1; <br />} <br />else <br />{ <br />deck[first_card].draw(); <br />deck[second_card].draw(); <br />first_card=-1; <br />} <br />} <br />function init() <br />{ <br />canvas = document.getElementById('canvas'); <br />canvas.addEventListener('click', choose, false); <br />ctx = canvas.getContext('2d'); <br />make_deck(); <br />shuffle(); <br />} <br /> 123142