예제 #1
0
def main(grid, showPcnt, showMaze):
    width = grid[0][0].w * len(grid[0])
    height = grid[0][0].w * len(grid)
    pygame.init()
    screen = pygame.display.set_mode((width, height))
    pygame.display.set_caption("Maze Game")
    current = [0, 0]
    for row in grid:
        for cell in row:
            cell.playing = True
    player = Player()

    while True:
        MazeGeneratorCode.drawGrid(grid, screen)
        drawFinish(grid, screen)
        player.drawSelf(grid, screen)
        pygame.display.update()

        if player.x == len(grid[0]) - 1 and player.y == len(grid) - 1:
            win(width, height, screen, grid, showPcnt, showMaze)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT: player.move(grid, xchange=1)
                elif event.key == pygame.K_LEFT: player.move(grid, xchange=-1)
                elif event.key == pygame.K_UP: player.move(grid, ychange=-1)
                elif event.key == pygame.K_DOWN: player.move(grid, ychange=1)
예제 #2
0
def win(width, height, screen, grid, showPcnt, showMaze):

    if width <= height:
        tinyFont = pygame.font.Font("8-BIT WONDER.TTF", int(width / 20))
        smallFont = pygame.font.Font("8-BIT WONDER.TTF", int(width / 15))
        bigFont = pygame.font.Font("8-BIT WONDER.TTF", int(width / 8))
        xpadding = int(width / 15)
        ypadding = int(width / 40)
    else:
        tinyFont = pygame.font.Font("8-BIT WONDER.TTF", int(height / 20))
        smallFont = pygame.font.Font("8-BIT WONDER.TTF", int(height / 15))
        bigFont = pygame.font.Font("8-BIT WONDER.TTF", int(height / 8))
        xpadding = int(height / 5)
        ypadding = int(height / 25)

    labelWin = bigFont.render("You Win", 1, MazeGeneratorCode.Colours.blue)
    winPos = labelWin.get_rect()
    winPos.center = (int(width / 2), int(height / 5))
    screen.blit(labelWin, winPos)

    texts = ["Same", "Easier", "Harder"]
    colours = [
        MazeGeneratorCode.Colours.yellow, MazeGeneratorCode.Colours.green,
        MazeGeneratorCode.Colours.red
    ]
    order = [2, 1, 3]
    Rects = []

    for i in range(0, 3):
        label = smallFont.render(texts[i], 1, MazeGeneratorCode.Colours.white)
        pos = label.get_rect()
        pos.center = (width / 2, height / 5 * (order[i] + 1))

        if order[i] == 2:
            x = pos.x - xpadding
            w = pos.width + xpadding * 2
            h = pos.height + ypadding * 2
        Rect = (x, pos.y - ypadding, w, h)
        Rects.append(Rect)
        pygame.draw.rect(
            screen, colours[i], Rect
        )  #(int(width/3.6),int(height/5*(i+0.75)),width/2.22,width/10))
        screen.blit(label, pos)

    label = tinyFont.render("Press S for setup", 1,
                            MazeGeneratorCode.Colours.blue)
    pos = label.get_rect()
    pos.bottomleft = (width / 40, height - width / 40)
    screen.blit(label, pos)

    pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()
                if x > Rects[0][0] and x < Rects[0][0] + Rects[0][2]:
                    if y > Rects[0][1] and y < Rects[0][1] + Rects[0][3]:
                        change = 0
                        MazeGeneratorCode.main(change=change,
                                               height=height,
                                               width=width,
                                               cellWidth=grid[0][0].w,
                                               showPcnt=showPcnt,
                                               showMaze=showMaze)
                    if y > Rects[1][1] and y < Rects[1][1] + Rects[1][3]:
                        change = -1
                        MazeGeneratorCode.main(change=change,
                                               height=height,
                                               width=width,
                                               cellWidth=grid[0][0].w,
                                               showPcnt=showPcnt,
                                               showMaze=showMaze)
                    if y > Rects[2][1] and y < Rects[2][1] + Rects[2][3]:
                        change = 1
                        MazeGeneratorCode.main(change=change,
                                               height=height,
                                               width=width,
                                               cellWidth=grid[0][0].w,
                                               showPcnt=showPcnt,
                                               showMaze=showMaze)
            if event.type == pygame.KEYDOWN:
                if event.key == ord("s"):
                    MazeGeneratorCode.setup()
예제 #3
0
    pygame.display.set_caption("Maze Game")
    current = [0, 0]
    for row in grid:
        for cell in row:
            cell.playing = True
    player = Player()

    while True:
        MazeGeneratorCode.drawGrid(grid, screen)
        drawFinish(grid, screen)
        player.drawSelf(grid, screen)
        pygame.display.update()

        if player.x == len(grid[0]) - 1 and player.y == len(grid) - 1:
            win(width, height, screen, grid, showPcnt, showMaze)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT: player.move(grid, xchange=1)
                elif event.key == pygame.K_LEFT: player.move(grid, xchange=-1)
                elif event.key == pygame.K_UP: player.move(grid, ychange=-1)
                elif event.key == pygame.K_DOWN: player.move(grid, ychange=1)


########################################################################################################################

if __name__ == "__main__":
    MazeGeneratorCode.main()
예제 #4
0
def main(cols=10,
         rows=10,
         w=40,
         d=1,
         showMaze=False,
         showPcnt=False,
         defslct=False):
    import MazeGeneratorCode
    pygame.init()
    screen = pygame.display.set_mode((400, 600))
    pygame.display.set_caption("Maze Set-up")
    background = pygame.image.load("setupBackground.png")
    screen.fill(MazeGeneratorCode.Colours.white)
    screen.blit(background, (0, 0))
    changedSettings = False

    colsBox = TextBox(Coords.colsText, 10, screen, background, 10, 1)
    rowsBox = TextBox(Coords.rowsText, 10, screen, background, 10, 2)
    cellBox = TextBox(Coords.cellText, 40, screen, background, 5, 3)
    wallBox = TextBox(Coords.wallText, 1, screen, background, 1, 4)
    boxes = [colsBox, rowsBox, cellBox, wallBox]

    colsbtnM = Button(Coords.colsbtns[0],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=colsBox,
                      change=-10)
    colsbtnP = Button(Coords.colsbtns[1],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=colsBox,
                      change=10)
    rowsbtnM = Button(Coords.rowsbtns[0],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=rowsBox,
                      change=-10)
    rowsbtnP = Button(Coords.rowsbtns[1],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=rowsBox,
                      change=10)
    cellbtnM = Button(Coords.cellbtns[0],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=cellBox,
                      change=-5)
    cellbtnP = Button(Coords.cellbtns[1],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=cellBox,
                      change=5)
    wallbtnM = Button(Coords.wallbtns[0],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=wallBox,
                      change=-1)
    wallbtnP = Button(Coords.wallbtns[1],
                      False,
                      screen,
                      background,
                      "Small",
                      textbox=wallBox,
                      change=1)

    mazebtnY = Button(Coords.mazebtns[0], False, screen, background, "Medium")
    mazebtnN = Button(Coords.mazebtns[1],
                      True,
                      screen,
                      background,
                      "Medium",
                      linked=mazebtnY)
    pcntbtnY = Button(Coords.pcntbtns[0], False, screen, background, "Medium")
    pcntbtnN = Button(Coords.pcntbtns[1],
                      True,
                      screen,
                      background,
                      "Medium",
                      linked=pcntbtnY)

    applybtn = Button(Coords.applybtn,
                      False,
                      screen,
                      background,
                      "Large",
                      ref="apply")
    defaultbtn = Button(Coords.defbtn,
                        defslct,
                        screen,
                        background,
                        "Large",
                        ref="default")
    finishbtn = Button(Coords.finbtn,
                       False,
                       screen,
                       background,
                       "Large",
                       ref="finish")

    buttons = [
        colsbtnM, colsbtnP, rowsbtnM, rowsbtnP, cellbtnM, cellbtnP, wallbtnM,
        wallbtnP, mazebtnY, mazebtnN, pcntbtnY, pcntbtnN, applybtn, defaultbtn,
        finishbtn
    ]

    #pygame.display.update()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                x, y = pygame.mouse.get_pos()

                for box in boxes:
                    if box.isClicked(x, y):
                        box.capture(screen, boxes, buttons)
                for button in buttons:
                    if button.isClicked(x, y, boxes):
                        type = button.isClicked(x, y, boxes)
                        if type == 1:
                            changedSettings = True
                            cols, rows, w, d, showMaze, showPcnt = applySettings(
                                boxes, buttons)
                        elif type == 2:
                            if changedSettings:
                                main(cols,
                                     rows,
                                     w,
                                     d,
                                     showMaze,
                                     showPcnt,
                                     defslct=True)
                            else:
                                main(defslct=True)
                        elif type == 3:
                            import MazeGeneratorCode
                            if changedSettings:
                                MazeGeneratorCode.main(height=rows * w,
                                                       width=cols * w,
                                                       cellWidth=w,
                                                       wallDepth=d,
                                                       showMaze=showMaze,
                                                       showPcnt=showPcnt)
                            else:
                                MazeGeneratorCode.main()

        drawApplied(boxes, buttons, screen)