Example #1
0
def main():
    """
    Main function used to run the Sudoku game
    uses pygame functionality to track game events
    """
    # initialize pygame
    pygame.init()
    pygame.display.set_caption("Sudoku")

    # Initialize the board, check board button the game window and FPS clock and the variable holding key strokes
    global FPSCLOCK, DISPLAYSURF
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode(
        (WINDOW_WIDTH, WINDOW_HEIGHT + BOTTOMGAP))
    key = None
    sudoku_board = Board(WINDOW_WIDTH, WINDOW_HEIGHT, SQUARE_SIZE, CELL_SIZE,
                         DISPLAYSURF, board)
    check_board = Button((LIGHTGRAY), 20, WINDOW_HEIGHT + 20, 50, 25, 25,
                         "check")

    # Fill window background with Black
    DISPLAYSURF.fill(BLACK)

    # Call board and button commands to draw initial board state
    sudoku_board.draw_grid()
    check_board.draw(DISPLAYSURF)

    # game loop watches for events such as key strokes and mouse clicks
    # redraws board and button every loop instance
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_1:
                    key = 1
                if event.key == pygame.K_2:
                    key = 2
                if event.key == pygame.K_3:
                    key = 3
                if event.key == pygame.K_4:
                    key = 4
                if event.key == pygame.K_5:
                    key = 5
                if event.key == pygame.K_6:
                    key = 6
                if event.key == pygame.K_7:
                    key = 7
                if event.key == pygame.K_8:
                    key = 8
                if event.key == pygame.K_9:
                    key = 9
                if event.key == pygame.K_KP1:
                    key = 1
                if event.key == pygame.K_KP2:
                    key = 2
                if event.key == pygame.K_KP3:
                    key = 3
                if event.key == pygame.K_KP4:
                    key = 4
                if event.key == pygame.K_KP5:
                    key = 5
                if event.key == pygame.K_KP6:
                    key = 6
                if event.key == pygame.K_KP7:
                    key = 7
                if event.key == pygame.K_KP8:
                    key = 8
                if event.key == pygame.K_KP9:
                    key = 9
                if event.key == pygame.K_DELETE:
                    sudoku_board.delete_pos()
                    key = None
                if event.key == pygame.K_RETURN:
                    sudoku_board.place(key)
                    key = None
                if event.key == pygame.K_SPACE:
                    sudoku_board.solve_self()

            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                clicked = sudoku_board.click(pos)
                if clicked:
                    sudoku_board.selected = clicked
                elif check_board.isOver(pos):
                    sudoku_board.check_board()
                key = None

            if sudoku_board.selected != (-1, -1) and key is not None:
                sudoku_board.guess(key)
        redraw(DISPLAYSURF, sudoku_board, check_board, WINDOW_HEIGHT,
               BOTTOMGAP)
        pygame.display.update()
Example #2
0
    args['b'].set_on_click(new_big_button_click, {'b': args['b']})
    # This overwrites big_button's action
    # Now big button becomes a random color whenever it's clicked


sm_button_2.set_on_click(sm_button_2_click, {'b': big_button})
sm_button_2.set_default_action(sm_button_default, {'b': sm_button_2})
sm_button_2.set_enabled(False)

backgroundview = View(0, 0, 800, 600)
backgroundview.fill(pygame.Color(255, 255, 255))

# Always display the board and notecard
# ENTER MAIN GAME LOOP
while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True
    # Make a white background
    backgroundview.draw(pygame.mouse, gameDisplay)

    big_button.draw(pygame.mouse, gameDisplay)

    sm_button_2.draw(pygame.mouse, gameDisplay)

    pygame.display.update()

    clock.tick(60)
Example #3
0
    args['d'].set_is_visible(True)

button.set_on_click(show_dialog, {'d': dialog})

backgroundview = View(0, 0, 1000, 1000)
backgroundview.fill(pygame.Color(255, 255, 255))
backgroundview.set_alpha(255)



# ENTER MAIN GAME LOOP
while not crashed:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    backgroundview.draw(pygame.mouse, gameDisplay)

    # When a view isn't available, it's best to never draw it at all

    button.draw(pygame.mouse, gameDisplay)

    # Draw the dialog over the button if visible
    dialog.draw(pygame.mouse, gameDisplay)


    pygame.display.update()

    clock.tick(60)
Example #4
0
def main():
    pygame.init()
    pygame.display.set_caption("maze generator :D")
    mazes: list[Maze] = []
    frames_per_sec = pygame.time.Clock()
    fps = 200
    # left right up down / cannot be zero / must add up to 1
    maze_index = 0
    initalizemazes(number_of_mazes, mazes)
    render_walls = True
    render_prev = False
    running = True
    current_maze = mazes[maze_index]
    rendering_maze = True
    add_maze_button = Button(
        width * 0.1,
        height * 0.05,
        (0, 0, 0),
        (sidebar_pos_x, sidebar_pos_y + (height * 0.1)),
    )
    print(len(mazes))
    print(len(mazes[0].board))

    # main loopI
    while running:
        screen.fill(background_color)
        # event handling, gets all event from the event queue
        for event in pygame.event.get():
            # only do something if the event is of type QUIT
            if event.type == pygame.QUIT:
                # change the value to False, to exit the main loop
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                pos = pygame.mouse.get_pos()
                if event.button == 1 and not current_maze.generated:
                    if on_board(board_width, board_height, pos):
                        current_maze.generate_maze(rendering_maze)
                    add_maze_button.click(
                        pos, mazes,
                        Maze(rows,
                             cols,
                             board_width / cols,
                             board_height / rows,
                             Wilson(screen),
                             weights=(0.25, 0.25, 0.25, 0.25),
                             static_locations=staticlocations))
                if event.button == 3:
                    for tile in current_maze.board:
                        if tile.rect.collidepoint(pos):
                            tile.on_click()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    render_walls = not render_walls
                if event.key == pygame.K_w:
                    render_prev = not render_prev
                if event.key == pygame.K_RIGHT:
                    maze_index = min(maze_index + 1, len(mazes) - 1)
                    current_maze = mazes[maze_index]
                if event.key == pygame.K_LEFT:
                    maze_index = max(maze_index - 1, 0)
                    current_maze = mazes[maze_index]
                if event.key == pygame.K_e:
                    generate_all(mazes)
                if event.key == pygame.K_SPACE:
                    fps = increment(fps)
                if event.key == pygame.K_KP_ENTER:
                    rendering_maze = not rendering_maze

        if current_maze.generating:
            current_maze.update(rendering_maze)
        render.rendermaze(screen, current_maze.board, render_walls,
                          render_prev)
        render.render_text(
            screen, "maze: " + str(maze_index + 1) + " / " + str(len(mazes)),
            (board_width + (width * (1 - scale) / 2), height * 0.05),
            background_color, 32)
        render.render_text(screen, str(fps),
                           (board_width + (width *
                                           (1 - scale) / 4), height * 0.01),
                           background_color, 12)
        add_maze_button.draw(screen, "Add Maze")
        pygame.display.flip()
        frames_per_sec.tick(fps)