pygame memory puzzle
发布时间:2020-12-17 17:25:09 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import pygame,random,sysfrom pygame.locals import *FPS = 30boxwidth = 3boxheight = 2assert boxwidth*boxheight%2 == 0,'board number needs to
|
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 import pygame,random,sys
from pygame.locals import *
FPS = 30
boxwidth = 3
boxheight = 2
assert boxwidth*boxheight%2 == 0,'board number needs to be even'
winx = 640
winy = 480
between = 10
boxsize = 40
xmargin = int((winx-(boxsize+between)*boxwidth)/2)
ymargin = int((winy-(boxsize+between)*boxheight)/2)
speed = 8
#color inuse
navyblue = (60,60,100)#dispfill
white = (255,255,255)#boardcover
blue = (0,255) #highlighrect
#icons color
red = (255,0)
yellow = (255,0)
purple = (255,255)
green = (0,0)
gray = (0,255)
#icons shape
rectan = 'rectan'
lines = 'lines'
circ = 'circ'
poly = 'poly'
allcolor = (red,yellow,purple,green)
allshape = (rectan,lines,circ,poly)
assert len(allcolor)*len(allshape*2)>= boxwidth*boxheight,'board is too big for the number of color and shape'
def main():
global fpsclock,disp
pygame.init()
disp = pygame.display.set_mode((winx,winy))
pygame.display.set_caption('memory puzzle')
fpsclock = pygame.time.Clock()
disp.fill(navyblue)
reveal = getrevealed(False)
mainboard = randomboard()
drawboard(mainboard,reveal)
startanimation(mainboard)
first = None
mousex = 0
mousey = 0
while True:
mouseclicked = False
disp.fill(navyblue)
drawboard(mainboard,reveal)
for event in pygame.event.get():
if event.type == QUIT or ( event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == MOUSEMOTION:
mousex,mousey = event.pos
elif event.type == MOUSEBUTTONUP:
mousex,mousey = event.pos
mouseclicked = True
boxx,boxy = getpixel(mousex,mousey)
if boxx!=None and boxy!= None:
if not reveal[boxx][boxy]:
highlighrect(boxx,boxy)
if not reveal[boxx][boxy] and mouseclicked:
revealboardanimation(mainboard,[(boxx,boxy)])
reveal[boxx][boxy] = True
if first == None:
first = (boxx,boxy)
else:
icon1shape,icon1color = getshapeandcolor(mainboard,first[0],first[1])
icon2shape,icon2color = getshapeandcolor(mainboard,boxx,boxy)
if icon1shape!= icon2shape or icon1color!= icon2color:
pygame.time.wait(1000)
coverboardanimation(mainboard,[(first[0],first[1]),(boxx,boxy)])
reveal[first[0]][first[1]] = False
reveal[boxx][boxy] = False
elif haswon(reveal):
wonanimation(mainboard)
pygame.time.wait(2000)
mainboard = randomboard()
startanimation(mainboard)
reveal = getrevealed(False)
drawboard(mainboard,reveal)
first = None
pygame.display.update()
fpsclock.tick(FPS)
def leftop(boxx,boxy):
left = xmargin + boxx*(boxsize + between)
top = ymargin + boxy*(boxsize + between)
return left,top
def highlighrect(boxx,boxy):
left,top = leftop(boxx,boxy)
pygame.draw.rect(disp,blue,(left-between/2,top-between/2,boxsize+between,boxsize+between),4)
def randomboard():
box = []
for shape in allshape:
for color in allcolor:
box.append((shape,color))
random.shuffle(box)
iconsnum = boxwidth*boxheight/2
icons = box[:iconsnum]*2
random.shuffle(icons)
board = []
for boxx in range(boxwidth):
column = []
for boxy in range(boxheight):
column.append(icons[0])
del icons[0]
board.append(column)
return board
def getrevealed(val):
revealedbox = []
for i in range(boxwidth):
revealedbox.append([val]*boxheight)
return revealedbox
def getpixel(x,y):
for boxx in range(boxwidth):
for boxy in range(boxheight):
left,boxy)
b = pygame.Rect((left,top,boxsize,boxsize))
if b.collidepoint(x,y):
return(boxx,boxy)
return(None,None)
def split(group,thelist):
result = []
for i in range(0,len(thelist),group):
result.append(thelist[i:group+i])
random.shuffle(result)
return result
def getshapeandcolor(board,boxy):
return board[boxx][boxy][0],board[boxx][boxy][1]
def drawicon(shape,color,boxy)
if shape == rectan:
pygame.draw.rect(disp,(left+5,top+5,boxsize-10,boxsize-10))
elif shape == lines:
for i in range(0,10):
pygame.draw.line(disp,(left+i,top),(left,top+i))
pygame.draw.line(disp,(left+boxsize-i,top+boxsize),(left+boxsize,top+boxsize-i))
elif shape == circ:
pygame.draw.circle(disp,(left+boxsize/2,top+boxsize/2),boxsize/2-5)
elif shape == poly:
pygame.draw.polygon(disp,((left+boxsize/2,top+boxsize/2)))
def drawoneboard(board,boxes,coverage):
for box in boxes:
shape,color = getshapeandcolor(board,box[0],box[1])
left,top = leftop(box[0],box[1])
pygame.draw.rect(disp,navyblue,boxsize))
drawicon(shape,box[1])
if coverage>0:
pygame.draw.rect(disp,white,coverage,boxsize))
pygame.display.update()
fpsclock.tick(FPS)
def coverboardanimation(board,boxcover):
for coverage in range(0,boxsize+speed,speed):
drawoneboard(board,boxcover,coverage)
def revealboardanimation(board,boxreveal):
for coverage in range(boxsize,-speed-1,-speed):
drawoneboard(board,boxreveal,coverage)
def drawboard(board,revealbox):
for boxx in range(boxwidth):
for boxy in range(boxheight):
left,boxy)
if not revealbox[boxx][boxy]:
pygame.draw.rect(disp,boxsize))
else:
shape,boxy)
drawicon(shape,boxy)
def startanimation(board):
boxes = []
for boxx in range(boxwidth):
for boxy in range(boxheight):
boxes.append((boxx,boxy))
random.shuffle(boxes)
revealbox = getrevealed(False)
drawboard(board,revealbox)
boxgroups = split(8,boxes)
for boxgroup in boxgroups:
revealboardanimation(board,boxgroup)
pygame.time.wait(300)
coverboardanimation(board,boxgroup)
pygame.display.update()
fpsclock.tick(FPS)
def wonanimation(board):
color1 = gray
color2 = navyblue
revealbox = getrevealed(True)
for i in range(13):
color1,color2 = color2,color1
drawboard(board,revealbox)
disp.fill(color1)
pygame.time.wait(300)
pygame.display.update()
def haswon(reveal):
for i in reveal:
if False in i:
return False
return True
if __name__ == '__main__':
main()
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
