Пример #1
0
class InGame(GameState):
    def __init__(self, runner, mainmenuState, rows, col):
        super().__init__(runner)
        self.mainmenuState = mainmenuState
        self.minesImage = pygame.image.load("assets/mine.png")
        self.openedImage = pygame.image.load("assets/opened.png")
        self.hiddenImage = pygame.image.load("assets/hidden.png")
        self.font = BitmapFont("assets/colorfont.png", 12, 12)
        self.rows = rows
        self.col = col
        self.endFlag = False

    def on_enter(self, OptionMenuState):
        self.mines = OptionMenuState.mines
        self.board = Board(self.col, self.rows, self.mines)
        self.endFlag = False
        #self.board.openCell(5,5)

    def draw(self, surface):
        for i in range(self.rows):
            for j in range(self.col):
                x, y = j * 32, i * 32
                rect = Rect(x, y, 32, 32)
                currentCell = self.board.arr[j][i]
                if currentCell.state == game.CLOSED:
                    surface.blit(self.hiddenImage, rect)
                elif currentCell.blockType == game.MINES:
                    surface.blit(self.minesImage, rect)
                else:
                    surface.blit(self.openedImage, rect)
                    if currentCell.num > 0:
                        self.font.draw(surface, str(currentCell.num), x + 10,
                                       y + 10)

    def update(self, deltatime):
        if self.runner.mouseClick is not None and not self.endFlag:
            mouseX, mouseY = self.runner.mouseClick
            x = int(mouseX / 32)
            y = int(mouseY / 32)

            openCell = self.board.openCell(x, y)
            #print(openCell)
            if openCell != game.CONTINUE:
                self.endFlag = True
        elif self.endFlag:
            keys = pygame.key.get_pressed()
            if keys[K_ESCAPE]:
                self.runner.change_state(self.mainmenuState)
Пример #2
0
class InGame(GameState):
    def __init__(self, runner, mainmenuState):
        super().__init__(runner)
        self.mainmenuState = mainmenuState
        self.endFlag = False
        self.font = BitmapFont("fasttracker2-style_12x12.png", 12, 12)
        self.delayTime = 200

    def on_enter(self, OptionMenuState):
        self.size = optionmenu.size
        self.useSolver = optionmenu.useSolver
        self.slidingPuzzle = SlidingPuzzle(self.size)
        self.endFlag = False
        self.slidingPuzzle.shuffle()
        self.slidingPuzzle.canbeSolved()
        if self.useSolver == "Yes":
            self.astar = AStar(self.slidingPuzzle)
            self.answer = self.astar.solved()
            self.i = 1

    def draw(self, surface):
        self.width = 600 / self.size
        for i in range(self.size):
            for j in range(self.size):
                x, y = j * self.width, i * self.width
                rect = Rect(x, y, self.width, self.width)
                pygame.draw.rect(surface, (100, 100, 100), rect, 3)
                currentCell = self.slidingPuzzle.arr[i][j]
                fontWidth = 12 * len(str(currentCell))
                self.font.draw(surface, str(currentCell),
                               x + ((self.width - fontWidth) / 2),
                               y + ((self.width - 12) / 2))

    def update(self, deltatime):
        keys = pygame.key.get_pressed()
        self.endFlag = self.slidingPuzzle.isSolved()
        if self.delayTime > 0:
            self.delayTime -= deltatime
        else:
            if keys[K_ESCAPE]:
                self.runner.change_state(self.mainmenuState)
                self.delayTime = 200
            if not self.endFlag:
                if self.useSolver == "No":
                    if keys[K_UP]:
                        self.slidingPuzzle.moves((0, -1))
                        self.delayTime = 200
                    if keys[K_LEFT]:
                        self.slidingPuzzle.moves((-1, 0))
                        self.delayTime = 200
                    if keys[K_DOWN]:
                        self.slidingPuzzle.moves((0, 1))
                        self.delayTime = 200
                    if keys[K_RIGHT]:
                        self.slidingPuzzle.moves((1, 0))
                        self.delayTime = 200
                else:
                    #print(self.answer)
                    if keys[K_n]:
                        if self.answer[self.i] is not None:
                            self.slidingPuzzle.moves(self.answer[self.i])
                        self.delayTime = 200
                        if self.i < len(self.answer) - 1:
                            self.i += 1
                    if keys[K_p]:
                        directionA = self.answer[self.i - 1]
                        if directionA is not None:
                            if directionA == (1, 0):
                                directionA = (-1, 0)
                            elif directionA == (-1, 0):
                                directionA = (1, 0)
                            elif directionA == (0, 1):
                                directionA = (0, -1)
                            elif directionA == (0, -1):
                                directionA = (0, 1)
                            self.slidingPuzzle.moves(directionA)
                        self.delayTime = 200
                        if self.i > 1:
                            self.i -= 1

        if self.runner.mouseClick is not None:
            mouseX, mouseY = self.runner.mouseClick

            if self.useSolver == "No":
                #distance from mousepress position to space position
                x = int(mouseX / self.width)
                y = int(mouseY / self.width)
                directionX = x - self.slidingPuzzle.space[0]
                directionY = y - self.slidingPuzzle.space[1]

                #make directionX into 1 or -1 and no diagonal
                if directionX > 0:
                    directionX = 1
                if directionX < 0:
                    directionX = -1
                if directionY > 0:
                    directionY = 1
                if directionY < 0:
                    directionY = -1
                for i in direction:
                    if (directionX, directionY) == i:
                        self.slidingPuzzle.moves((directionX, directionY))
                        break