2014-01-22 45 views
-2

我有一個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() 
+2

我們懶惰,不喜歡讀書那麼多的代碼。如果不簡化問題,你可能不會得到一個好的答案。 – mhlester

+1

爲什麼你多次載入相同的圖像 – Serial

回答

2

如果圖像相同,可以加載多個副本。反而加載playing-card-back.jpeg一次,並重復使用它。

你每循環多次發生一次,每次發生一次事件。

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

相反,每幀渲染一次:

# 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() 

使用Rect.collide*檢查,看看如果點擊在該範圍內,我們希望

# 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))