我在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()
答案 0 :(得分:2)
如果是同一张图片,则会加载许多副本。而是加载playing-card-back.jpeg
一次,然后重复使用。
每个循环都会进行多次blitting,每次触发事件一次。
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))