Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygames中的卡片配对问题_Python_Memory_Pygame_Matching_Playing Cards - Fatal编程技术网

Python Pygames中的卡片配对问题

Python Pygames中的卡片配对问题,python,memory,pygame,matching,playing-cards,Python,Memory,Pygame,Matching,Playing Cards,我在pygame中有一个记忆游戏的代码。我试图解决的问题是,一旦用户选择了两张不匹配的卡,就让卡向下翻转。如果有人能帮忙,那就太好了。谢谢 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

我在pygame中有一个记忆游戏的代码。我试图解决的问题是,一旦用户选择了两张不匹配的卡,就让卡向下翻转。如果有人能帮忙,那就太好了。谢谢

 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()
导入pygame
从pygame.locals导入*
#颜色
黑色=(0,0,0)
白色=(255255)
红色=(100,0,0)
绿色=(0153,0)
蓝色=(0,0255)
粉红色=(2550220)
img1=pygame.image.load(“playing card back.jpeg”)
再次=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_uu.png”)
side2=pygame.image.load(“300px-Poker-sm-21A-5s.png”)
side3=pygame.image.load(“扑克牌组clipartace of spades扑克牌---免费剪辑-art-68ezag1x.png”)
side4=pygame.image.load(“Poker-sm-233-Qd.png”)
side5=pygame.image.load(“jackspades.png”)
background=pygame.image.load(“green-fitted-poker-table-feel-400x300.jpeg”)
#屏风
屏幕宽度=1000
屏幕高度=500
大小=(屏幕宽度、屏幕高度)
screen=pygame.display.set_模式(大小)
完成=错误
一个匹配=假
两个匹配=假
三个匹配=假
四个匹配=假
五个匹配=假
timesPressed=0
屏幕光点(背景,(0,0))
pygame.display.flip()
clock=pygame.time.clock()
完成时==False:
对于pygame.event.get()中的事件:
屏幕blit(img1,(10,25100100))
屏幕blit(img2,(10250100100))
屏幕blit(img3,(210,25100100))
屏幕blit(img4,(210250100100))
屏幕blit(img5,(410,25100100))
屏幕blit(img6,(410250100100))
屏幕blit(img7,(6102501000100))
屏幕blit(img8,(610,25100100))
屏幕blit(img9,(810,25100100))
屏幕blit(img10,(810250100100))
##pygame.draw.rect(屏幕,img,[50,10100100])
##pygame.draw.rect(屏幕,图形,[50150100100])
##pygame.draw.rect(屏幕,图形,[200,10100100])
##pygame.draw.rect(屏幕,图形,[200150100100])
##pygame.draw.rect(屏幕,图形,[350,10100100])
##pygame.draw.rect(屏幕,图形,[350150100100])
如果event.type==pygame.QUIT:
打印“用户退出”
完成=正确
elif event.type==pygame.KEYDOWN:
打印“用户按键”
elif event.type==pygame.KEYUP:
打印“用户按键”
elif event.type==pygame.MOUSEBUTTONDOWN和timesPressed<2:
pos=pygame.mouse.get_pos()
x、 y=event.pos
打印位置
#检查单击是否在我们想要的范围内
如果(x在范围(10110)内)和(y在范围(25125)内):
img1=side1
按下时间+=1
如果img2==side2:
一个匹配=正确
如果(x在范围(10110)内)和(y在范围(250350)内):
img2=side2
按下时间+=1
如果(x在范围(210310))中,且(y在范围(25125))中:
img3=侧面3
按下时间+=1
如果(x在范围(210310)内)和(y在范围(250350)内):
img4=side5
按下时间+=1
如果(x在范围内(410510))和(y在范围内(25125)):
img5=侧面2
按下时间+=1
如果(x在范围内(410510))和(y在范围内(250350)):
img6=side1
按下时间+=1
如果(x在范围内(610710))和(y在范围内(250350)):
img7=side5
按下时间+=1
如果(x在范围内(610710))和(y在范围内(25125)):
img8=side4
按下时间+=1
如果(x在范围内(810910))和(y在范围内(25125)):
img9=side4
按下时间+=1
如果(x在范围内(810910))和(y在范围内(250350)):
img10=side3
按下时间+=1
分数=0
##如果时间按下==2:
##img1=再次
打印“点”,点
时钟滴答(30)
打印时间已按下
##图像(0,0)
打印一个匹配的
pygame.display.flip()
如果img2==side3:
如果img9==side3:
三个匹配=真
timesPressed=0
点数+=1
其他:
三个匹配=假
如果img3==side2:
如果img4==side2:
两个匹配=真
timesPressed=0
点数+=1
其他:
两个匹配=假
如果img5==side5:
如果img7==side5:
五个匹配=正确
timesPressed=0
点数+=1
其他:
五个匹配=假
如果img9==side4:
如果img9==side4:
四个匹配=真
timesPressed=0
点数+=1
其他:
四个匹配=假
如果两个_匹配==真:
点数+=1
如果三个_匹配==真:
点数+=1
如果四个_匹配==真:
点数+=1
如果五个匹配==真:
点数+=1
如果一个_匹配==假:
点数==0
如果两个_匹配==假:
点数==0
如果三个_匹配==假:
点数==0
如果四个_匹配==假:
点数==0
如果五个匹配==假:
点数==0
pygame.quit()

如果同一图像相同,则加载多个副本。相反,将
扑克牌加载回.jpeg一次,然后重复使用

您在每个循环中进行多次闪电扫描,每次触发事件一次

for event in pygame.event.get():
    screen.blit(img1,(10,25,100,100))
    screen.blit(img2,(10,250,100,100))
相反,每个fra渲染一次
# 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()
# 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))