简体   繁体   中英

Memory game, problem with detecting that both images are same

I'm trying to make a small memory game. However, I have to problem with detecting that the two cards the player clicked on are the same so the cards can be removed.

I'm trying to make it so that a variable will take the cards image and then see if the both images are the same, and if they are they will be removed. However, this doesn't seem to work.

So I'm wondering, what would you guys recommend me to do?

if mousehovercard0 == 1 and button1 == 1:
    if click == 1:
        cardclick1 = card0.image
    if click == 2:
        cardclick2 = card0.image
if mousehovercard1 == 1 and button1 == 1:
    if click == 1:
        cardclick1 = card1.image
    if click == 2:
        cardclick2 = card1.image
if mousehovercard2 == 1 and button1 == 1:
    if click == 1:
        cardclick1 = card2.image
    if click == 2:
        cardclick2 = card2.image
if mousehovercard3 == 1 and button1 == 1:
    if click == 1:
        cardclick1 = card3.image
    if click == 2:
        cardclick2 = card3.image


if cardclick1 == cardclick2:
    cardclick1.rect.x = -100
    cardclick2.rect.x = -100

A simple solution is create a variable for the different images and one clicked variable:

clicked_=False
image1_click=False
image2_click=False
...

When a "memory tile" is pressed, the corresponding variable is set to true. If the second is clicked as well, then you execute the command to remove both. For example, this is applied to the two tiles with image1:

if clicked:
    if image1_click:
        #Command to remove both buttons
    else:
        #Incorrect click, reset table
        image1_click=False
    clicked=False
else:
    clicked=True
    image1_click=True

As a bonus, when all image1_click are true, the game is completed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM