Html5 memory flop game Implementation ideas and code

Source: Internet
Author: User

Comments: The two cards opened by the main function will be eliminated if paired; otherwise, the two cards will be returned to the back. The following describes the demand analysis and sample code, interested friends can learn

The two cards opened will be eliminated if paired; otherwise, the two cards will be returned to the back.

Requirement Analysis

How to draw the positive and back faces and how to eliminate them after successful pairing
How to generate a card group and determine the position and corresponding image of each card

Shuffling
How to record card group pairing Information
How to determine whether a click event is the first or second click

Cheating event handling: two clicks on the same card, click the eliminated card, and click the area outside the card.
After opening a card, you need to give the player a certain amount of time to see how to pause it.

The response of the mouse click event and the coordinates of the mouse click position are obtained to determine which card is clicked.

MYCode:

The Code is as follows:
<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; // the flag for the first click
Var first_card =-1;
Var second_card;
Var back_color = "rgb (255, 0, 0)"; // the color of the back of the card.
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 () // draw the back of the card
{
Ctx. fillStyle = back_color;
Ctx. fillRect (this. sx, this. sy, this. swidth, this. sheight );
}
Function Card (sx, sy, swidth, sheight, img, info) // Constructor
{
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 () // generates and draws a card group.
{
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 () // shuffling
{
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) // The card is not eliminated
{
// Determine which card is clicked
If (mx> card. sx & mx <card. sx + card. swidth & my> card. sy & my <card. sy + card. sheight)
{
If (I! = First_card) // if you click the same card twice, no processing is performed.
Break;
}
}
}
If (I <deck. length)
{
If (first_pick) // if it is the first time you click
{
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) // pairing successful
{
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>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.