Esempio n. 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()
Esempio n. 2
0
def start_game_screen(screen, clock, welcome_music, welcome_bg, help):
    global play_intro_music
    pygame.mouse.set_visible(False)
    button_list = pygame.sprite.Group()
    button = Button('src/level01_gray.jpg', 280, 250, 1)
    button_list.add(button)
    button = Button('src/level02_gray.jpg', 550, 250, 2)
    button_list.add(button)
    button = Button('src/help.png', 930, 20, 3)
    button_list.add(button)

    x = pygame.mouse.get_pos()[0]
    y = pygame.mouse.get_pos()[1]
    cursor_list = pygame.sprite.Group()
    cursor = Cursor('src/cursor.png', x, y)
    cursor_list.add(cursor)
    welcome_music.set_volume(.5)
    if play_intro_music:
        welcome_music.play()
    pygame.display.flip()
    waiting = True
    isOverOut = False
    while waiting:
        x = pygame.mouse.get_pos()[0]
        y = pygame.mouse.get_pos()[1]
        cursor.update(x, y)
        clock.tick(60)
        isOverOut = False
        for button in pygame.sprite.spritecollide(cursor, button_list, False):
            isOverOut = True
            level = button.selected()
            if level == 1:
                button.isOver('src/level01_color.jpg')
            elif level == 2:
                button.isOver('src/level02_color.jpg')
            else:
                button.isOver('src/help_over.png')
        if not isOverOut:
            for button in button_list:
                level = button.selected()
                if level == 1:
                    button.isOver('src/level01_gray.jpg')
                elif level == 2:
                    button.isOver('src/level02_gray.jpg')
                else:
                    button.isOver('src/help.png')
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                choice = -1
                waiting = False
            if event.type == pygame.MOUSEBUTTONDOWN and pygame.sprite.spritecollide(cursor, button_list, False):
                buttons = pygame.sprite.spritecollide(cursor, button_list, False)
                for button in buttons:
                    choice = button.selected()
                    if choice == 1:
                        play_intro_music = True
                        waiting = False
                        welcome_music.stop()
                    elif choice == 2:
                        play_intro_music = True
                        waiting = False
                        welcome_music.stop()
                    elif choice == 3:
                        waiting = False

        screen.blit(welcome_bg, (0, 0))
        button_list.draw(screen)
        cursor_list.draw(screen)
        pygame.display.flip()
    return choice