-3

I have a code for a memory game in pygame. The problem I am trying to resolve is making the cards flip back down once the user selects two cards that are not matching. If any one could help that would be great. Thanks!

 import pygame


 from pygame.locals import*


 # Colours
 black = (  0,  0,  0)
 white = (255,255,255)
 red   = (100,  0,  0)
 green = (  0,153,  0)
 blue  = (  0,  0,255)
 pink  = (255,  0,220)
 img1 = pygame.image.load("playing-card-back.jpeg")
 again = pygame.image.load("playing-card-back.jpeg")
 img2 = pygame.image.load("playing-card-back.jpeg")
 img3 = pygame.image.load("playing-card-back.jpeg")
 img4 = pygame.image.load("playing-card-back.jpeg")
 img5 = pygame.image.load("playing-card-back.jpeg")
 img6 = pygame.image.load("playing-card-back.jpeg")
 img7 = pygame.image.load("playing-card-back.jpeg")
 img8 = pygame.image.load("playing-card-back.jpeg")
 img9 = pygame.image.load("playing-card-back.jpeg")
 img10 = pygame.image.load("playing-card-back.jpeg")
 pic1 = pygame.image.load("playing-card-back.jpeg")
 side1 = pygame.image.load("200px-Playing_card_diamond_10.svg_.png")
 side2=pygame.image.load("300px-Poker-sm-21A-5s.png")
 side3 = pygame.image.load("deck-of-cards-clipartace-of-spades-playing-card---free-clip-                art-68ezag1x.png")
 side4 = pygame.image.load("Poker-sm-233-Qd.png")
 side5 = pygame.image.load("jackspades.png")
 background = pygame.image.load("green-suited-poker-table-felt-400x300.jpeg")
 #Screen
 screenWidth = 1000
 screenHeight = 500


 size = (screenWidth,screenHeight)
 screen = pygame.display.set_mode(size)


 done = False
 one_matched = False
 two_matched = False
 three_matched = False
 four_matched = False
 five_matched = False


 timesPressed = 0

 screen.blit(background,(0,0))

 pygame.display.flip()

 clock = pygame.time.Clock()

 while done == False:


    for event in pygame.event.get():

        screen.blit(img1,(10,25,100,100))
        screen.blit(img2,(10,250,100,100))
        screen.blit(img3,(210,25,100,100))
        screen.blit(img4,(210,250,100,100))
        screen.blit(img5,(410,25,100,100))
        screen.blit(img6,(410,250,100,100))
        screen.blit(img7,(610,250,100,100))
        screen.blit(img8,(610,25,100,100))
        screen.blit(img9,(810,25,100,100))
        screen.blit(img10,(810,250,100,100))


##        pygame.draw.rect(screen,img,[50,10,100,100])
##        pygame.draw.rect(screen,graphic,[50,150,100,100])
##        pygame.draw.rect(screen,graphic,[200,10,100,100])
##        pygame.draw.rect(screen,graphic,[200,150,100,100])
##        pygame.draw.rect(screen,graphic,[350,10,100,100])
##        pygame.draw.rect(screen,graphic,[350,150,100,100])

        if event.type == pygame.QUIT:

            print"User quit"
            done = True

        elif event.type == pygame.KEYDOWN:

            print "User pressed a key"

        elif event.type == pygame.KEYUP:

            print "User pressed a key"



        elif event.type == pygame.MOUSEBUTTONDOWN and timesPressed < 2:
            pos = pygame.mouse.get_pos()
            x, y = event.pos
            print pos



        # Check to see if the click is in the range we want
            if ( x in range(10,110)) and (y in range(25,125)):
                img1 = side1
                timesPressed +=1
                if img2 == side2:
                    one_matched = True



            if (x in range (10,110)) and (y in range (250,350)):
                img2 = side2
                timesPressed +=1

            if (x in range (210,310)) and (y in range (25,125)):
                img3 = side3
                timesPressed +=1

            if (x in range (210,310)) and (y in range (250,350)):
                img4 = side5
                timesPressed +=1

            if (x in range (410,510)) and (y in range (25,125)):
                img5 =side2
                timesPressed +=1

            if (x in range (410,510)) and (y in range (250,350)):
                img6 = side1
                timesPressed +=1

            if (x in range (610,710)) and (y in range (250,350)):
                img7 = side5
                timesPressed +=1

            if (x in range (610,710)) and (y in range (25,125)):
                img8=side4
                timesPressed +=1

            if (x in range (810,910)) and (y in range (25,125)):
                img9 = side4
                timesPressed +=1

            if (x in range (810,910)) and (y in range (250,350)):
                img10 = side3
                timesPressed +=1



        points = 0


##        if timesPressed == 2:
##            img1 = again


        print "points", points




        clock.tick(30)
        print timesPressed






##        image(0,0)
        print one_matched
        pygame.display.flip()






if img2 == side3:
    if img9 == side3:
        three_matched = True
        timesPressed = 0
        points += 1
    else:
        three_matched = False

if img3 == side2:
    if img4 == side2:
        two_matched = True
        timesPressed = 0
        points += 1
    else:
        two_matched = False


if img5 == side5:
    if img7 == side5:
        five_matched = True
        timesPressed = 0
        points += 1
    else:
        five_matched = False

if img9 == side4:
    if img9 == side4:
        four_matched = True
        timesPressed = 0
        points += 1
    else:
         four_matched = False

if two_matched == True:
    points += 1

if three_matched == True:
    points += 1

if four_matched == True:
    points += 1

if five_matched == True:
    points += 1

if one_matched == False:
    points == 0

if two_matched == False:
    points == 0

if three_matched == False:
    points == 0

if four_matched == False:
    points == 0

if five_matched == False:
    points == 0


pygame.quit()

1 Answers1

2

You load many copies if the same image. Instead load playing-card-back.jpeg once, and use it repeatedly.

You are blitting multiple times per one loop, once for every event fired.

for event in pygame.event.get():
    screen.blit(img1,(10,25,100,100))
    screen.blit(img2,(10,250,100,100))

Instead, render once per frame:

# handle events
for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        # ...
# draw
screen.blit(img1,(10,25,100,100))
# ...

# flip
pygame.display.flip()

Use Rect.collide* to check to see if the click is in the range we want

# instead of
if (x in range (10,110)) and (y in range (250,350)):

# use
for card in cards:
    if card.rect.collidepoint(x,y)
        print("You clicked on {}".format(card))
ninMonkey
  • 7,211
  • 8
  • 37
  • 66