1 def playing_update (self): self.mousePos = pygame.mouse.get_pos () для кнопки в self.playingButtons: button.update (self.mousePos)
def playing_draw(self):
self.window.fill(WHITE)
for button in self.playingButtons:
button.draw(self.window)
if self.selected:
self.drawSelection(self.window, self.selected)
self.drawGrid(self.window)
pygame.display.update()
######## HELPER FUNCTION
def drawSelection(self, window, pos):
pygame.draw.rect(window, LIGHTBLUE,
((pos[0] * cellSize) + gridPos[0], (pos[1] * cellSize) + gridPos[1], cellSize, cellSize))
def drawGrid(self, window):
pygame.draw.rect(window, BLACK, (gridPos[0], gridPos[1], WIDTH - 150, HEIGHT - 150), 2)
for x in range(9):
if x % 3 != 0:
pygame.draw.line(window, BLACK, (gridPos[0] + (x * cellSize), gridPos[1]),
(gridPos[0] + (x * cellSize), gridPos[1] + 450))
pygame.draw.line(window, BLACK, (gridPos[0], gridPos[1] + (x * cellSize)),
(gridPos[0] + 450, gridPos[1] + +(x * cellSize)))
else:
pygame.draw.line(window, BLACK, (gridPos[0] + (x * cellSize), gridPos[1]),
(gridPos[0] + (x * cellSize), gridPos[1] + 450), 2)
pygame.draw.line(window, BLACK, (gridPos[0], gridPos[1] + (x * cellSize)),
(gridPos[0] + 450, gridPos[1] + +(x * cellSize)), 2)
def mouseOnGrid(self):
if self.mousePos[0] < gridPos[0] or self.mousePos[1] < gridPos[1]:
return False
if self.mousePos[0] > gridPos[0] + gridSize or self.mousePos[1] > gridPos[1] + gridSize:
return False
return (self.mousePos[0] - gridPos[0]) // cellSize, (self.mousePos[1] - gridPos[1]) // cellSize
def loadButtons(self):
self.playingButtons.append(Button())
2 из импорта в Pygame *
кнопка класса: def init (self, x, y, ширина, высота, текст = нет, цвет = (73, 73, 73), выделенный цвет = (1898, 189, 189 ), function = None, params = None): self.image = pygame.surface ((width, height)) self.pos = (x, y) self.Rect = self.image.get_Rect () self.Rect.topleft = self.pos self.text = текст self.colour = цвет self.highlightedColour = selectedColour self.function = функция self.params = params self.highlighted = False
def update(self, mouse):
if self.Rect.collidepoint(mouse):
self.highlighted = True
else:
self.highlighted = False
def draw(self, window):
self.image.fill(self.highlightedColour if self.highlighted else self.colour)
self.window.blit(self.image, self.pos)