2014-01-22 6 views
-2

파이 게임에서 메모리 게임 코드가 있습니다. 내가 해결하려고하는 문제는 사용자가 일치하지 않는 두 개의 카드를 선택하면 카드가 뒤집히도록 만드는 것입니다. 만약에 도움이된다면 도움이 될 것입니다. 감사!파이 게임에서 카드 중매 문제

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