1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import pygame, sys, time from pygame.locals import * from puzzle import *
puzzle=puzzle()
pygame.init() WINDOWWIDTH = 300 WINDOWHEIGHT = 300 BASICFONT = pygame.font.Font('freesansbold.ttf',50) windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) pygame.display.set_caption('8 Puzzle')
BLACK = (96, 123, 139) RED = (255, 0, 0) GREEN = (255, 222, 173) BLUE = (0, 0, 255) WHITE=(255,255,255) Text=(96, 123, 139)
blockTOP=0 blockLEFT=0 blocks=[] blockNumber=1
for i in range(3): for j in range(3): if blockNumber>8: blocks.append({'rect':pygame.Rect(blockLEFT,blockTOP,99,99),'color':BLACK,'block':str(0)}) else: blocks.append({'rect':pygame.Rect(blockLEFT,blockTOP,99,99),'color':GREEN,'block':str(blockNumber)}) blockNumber+=1 blockLEFT+=100 blockTOP+=100 blockLEFT=0
for b in blocks: pygame.draw.rect(windowSurface, b['color'], b['rect']) textSurf = BASICFONT.render(b['block'], True,Text) textRect = textSurf.get_rect() textRect.center = b['rect'].left+50,b['rect'].top+50 windowSurface.blit(textSurf, textRect) pygame.display.update() numShufles=50 evt=False while True: for event in pygame.event.get(): if event.type==MOUSEBUTTONDOWN and event.button==1: evt=True while numShufles>0: puzzle.shufler() puzzle.PreviousNode.extend(puzzle.StartNode) block=0 for b in blocks: b['block']=str(puzzle.StartNode[block]) block+=1 if b['block']=='0': b['color']=BLACK else: b['color']=GREEN pygame.draw.rect(windowSurface, b['color'], b['rect']) textSurf = BASICFONT.render(b['block'], True,Text) textRect = textSurf.get_rect() textRect.center = b['rect'].left+50,b['rect'].top+50 windowSurface.blit(textSurf, textRect) pygame.display.update() time.sleep(0.04) numShufles-=1 if evt==True: puzzle.sucessor(puzzle.StartNode) nxNode=puzzle.getNextNode() block=0 for b in blocks: b['block']=str(nxNode[block]) block+=1 if b['block']=='0': b['color']=BLACK else: b['color']=GREEN pygame.draw.rect(windowSurface, b['color'], b['rect']) textSurf = BASICFONT.render(b['block'], True,Text) textRect = textSurf.get_rect() textRect.center = b['rect'].left+50,b['rect'].top+50 windowSurface.blit(textSurf, textRect) pygame.display.update() time.sleep(0.3) count=1 while nxNode!=puzzle.GoalNode: count+=1 puzzle.sucessor(nxNode) nxNode=puzzle.getNextNode() block=0 for b in blocks: b['block']=str(nxNode[block]) block+=1 if b['block']=='0': b['color']=BLACK else: b['color']=GREEN pygame.draw.rect(windowSurface, b['color'], b['rect']) textSurf = BASICFONT.render(b['block'], True,Text) textRect = textSurf.get_rect() textRect.center = b['rect'].left+50,b['rect'].top+50 windowSurface.blit(textSurf, textRect) pygame.display.update() time.sleep(0.03) break while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit()
|