-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.py
121 lines (109 loc) · 5.17 KB
/
visualizer.py
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
import sys
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
import time
class NpuzzleVisualizer():
_pieceBorder = 10
_fontColor = (255, 254, 252)
_bgColor=(49, 49, 49)
_evenPieceColor=(93, 93, 93)
_oddPieceColor=(136, 0, 0)
_pieceBorderColor=(0, 0, 0)
_zeroPieceColor=(0, 0, 0)
_timeBetweenStates = 0.8
_puzzleIndex = 0
_fontStyle = "resources/Roboto-Black.ttf"
def __init__(self, puzzleDim, puzzleStates, windowDim=720):
pygame.init()
self._puzzleDim = puzzleDim
self._windowDim = windowDim
self._pieceDim = self._windowDim // (puzzleDim + 1)
self._pieceBorder = self._pieceDim // 10
self._puzzleStates = puzzleStates
self._boardDim = self._pieceDim * self._puzzleDim - (self._pieceBorder * (self._puzzleDim - 1))
self._boardStart = (self._windowDim - self._boardDim) // 2
self._fontSize = self._pieceDim // 2
self._numbers = {}
self._surface = pygame.display.set_mode((self._windowDim, self._windowDim))
def create_font(self, text, font, size, color=(255,255,255), bold=False, italic=False):
font = pygame.font.SysFont(font, size, bold=bold, italic=italic)
text = font.render(text, True, color)
return text
def printPieceNumber(self, row, column, value):
if value not in self._numbers.keys():
self._numbers[value] = self.create_font(str(value), self._fontStyle, self._fontSize, self._fontColor)
textRect = self._numbers[value].get_rect(center=(column + (self._pieceDim // 2), row + (self._pieceDim//2)))
self._surface.blit(self._numbers[value], textRect)
def drawPiece(self, row, column, value):
columnCoor = self._boardStart + self._pieceDim * column - (self._pieceBorder if column else 0) * column
rowCoor = self._boardStart + self._pieceDim * row - (self._pieceBorder if row else 0) * row
if value:
pieceColor = self._oddPieceColor if value % 2 else self._evenPieceColor
pygame.draw.rect(self._surface, pieceColor,
pygame.Rect(columnCoor,
rowCoor,
self._pieceDim,
self._pieceDim))
pygame.draw.rect(self._surface, self._pieceBorderColor,
pygame.Rect(columnCoor,
rowCoor,
self._pieceDim,
self._pieceDim), self._pieceBorder)
self.printPieceNumber(rowCoor, columnCoor, value)
else:
pygame.draw.rect(self._surface, self._zeroPieceColor,
pygame.Rect(columnCoor,
rowCoor,
self._pieceDim,
self._pieceDim))
def mySleep(self, seconds):
start = time.time()
while time.time() - start < seconds:
if self.eventHandler():
break
pygame.time.wait(10)
def drawPieces(self, puzzleState):
row = -1
for piece in range(self._puzzleDim * self._puzzleDim):
self.eventHandler()
column = piece % self._puzzleDim
if column == 0:
row += 1
self.drawPiece(row, column, puzzleState[piece])
pygame.display.flip()
self.mySleep(self._timeBetweenStates)
def pause(self):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
self._puzzleIndex = self._puzzleIndex + 1 if event.key == pygame.K_RIGHT else self._puzzleIndex - 1
self._puzzleIndex = min(max(self._puzzleIndex, 0), len(self._puzzleStates) - 1)
self.drawPieces(self._puzzleStates[self._puzzleIndex])
if event.key == pygame.K_p:
return
def eventHandler(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
self._puzzleIndex = self._puzzleIndex + 1 if event.key == pygame.K_RIGHT else self._puzzleIndex - 1
self._puzzleIndex = min(max(self._puzzleIndex, 0), len(self._puzzleStates) - 1)
self.drawPieces(self._puzzleStates[self._puzzleIndex])
return False
if event.key == pygame.K_p:
self.pause()
return True
return False
def startVisualization(self):
self._surface.fill(self._bgColor)
pygame.display.flip()
while True:
self.eventHandler()
self._puzzleIndex = min(max(self._puzzleIndex, 0), len(self._puzzleStates) - 1)
self.drawPieces(self._puzzleStates[self._puzzleIndex])
self._puzzleIndex += 1