def ask_clear_state(window, control) -> int: flag = True flag2 = False joy_ctrl = read_all_controls() dark = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA) dark.fill((0, 0, 0, 96)) window.blit(dark, (0, 0)) background = pygame.Surface((WIDTH // 2 - 150, HEIGHT // 2 - 150)) button_font = pygame.font.SysFont("calibri", 41, True) colors = ((0, 0, 0), (255, 255, 255)) button1 = Button(WIDTH // 2, HEIGHT // 2 - 40, (16, 16, 255), button_font, "COMMIT", colors, True).set_offset_pos().set_selected() button2 = Button(WIDTH // 2, HEIGHT // 2 + 10, (16, 16, 255), button_font, "CANCEL", colors, True).set_offset_pos() buttons = (button1, button2) ask_clear = Room(buttons, button_sound) q = 0 while ask_clear.run: for event in pygame.event.get(): if event.type == pygame.QUIT: control["state"] = switch_state(ask_clear, states.QUIT) q = 1 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: ask_clear.update_button("up") elif event.key == pygame.K_DOWN: ask_clear.update_button("down") if ask_clear.button_pressed() == 0: clear_data() ask_clear.exit() elif ask_clear.button_pressed() == 1: ask_clear.exit() if not no_joystick: if str(joy.get_hat(0)) == joy_ctrl["up"] and flag: ask_clear.update_button("up") flag = False elif str(joy.get_hat(0)) == joy_ctrl["down"] and flag: ask_clear.update_button("down") flag = False if joy.get_hat(0) == (0, 0): flag = True if joy.get_button(joy_ctrl["accept"]) and flag2: flag2 = False if ask_clear.button_pressed(True) == 0: clear_data() ask_clear.exit() elif ask_clear.button_pressed(True) == 1: ask_clear.exit() elif not joy.get_button(joy_ctrl["accept"]): flag2 = True window.blit(background, (WIDTH // 4 + 75, HEIGHT // 4 + 75)) background.fill((16, 16, 216)) ask_clear.show(window, 0, 0) pygame.display.flip() clock.tick(48) return q
def get_control(window, press_text) -> str: window.blit(press_text, (WIDTH // 2 - 100, HEIGHT // 2 + 100)) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return "None" if not no_joystick: if event.type == pygame.JOYBUTTONDOWN: for i in range(joy.get_numbuttons()): button = joy.get_button(i) if button: return str(i) hat = str(joy.get_hat(0)) if hat in ("(1, 0)", "(0, 1)", "(-1, 0)", "(0, -1)"): return hat else: print("No joystick detected.") return "None"
def set_controls_state(window, control): flag = True flag2 = False joy_ctrl = read_all_controls() button_font = pygame.font.SysFont("calibri", 55, True) title_font = pygame.font.SysFont("calibri", 65, True) controls_font = pygame.font.SysFont("calibri", 24, True) title_text = title_font.render("Controls", True, (240, 240, 240)) press_text = pygame.font.SysFont("calibri", 30, True).render("Press any button", True, (240, 240, 240)) left_text = controls_font.render(joy_ctrl["left"], True, (240, 240, 240)) right_text = controls_font.render(joy_ctrl["right"], True, (240, 240, 240)) up_text = controls_font.render(joy_ctrl["up"], True, (240, 240, 240)) down_text = controls_font.render(joy_ctrl["down"], True, (240, 240, 240)) accept_text = controls_font.render(str(joy_ctrl["accept"]), True, (240, 240, 240)) pause_text = controls_font.render(str(joy_ctrl["pause"]), True, (240, 240, 240)) colors = ((0, 0, 0), (255, 255, 255)) button1 = Button(WIDTH // 2 - 190, HEIGHT // 2 - 100, (16, 16, 255), button_font, "left", colors, True).set_offset_pos() button2 = Button(WIDTH // 2 - 30, HEIGHT // 2 - 100, (16, 16, 255), button_font, "right", colors, True).set_offset_pos() button3 = Button(WIDTH // 2 - 190, HEIGHT // 2, (16, 16, 255), button_font, "up", colors, True).set_offset_pos() button4 = Button(WIDTH // 2 - 30, HEIGHT // 2, (16, 16, 255), button_font, "down", colors, True).set_offset_pos() button5 = Button(WIDTH // 2 + 160, HEIGHT // 2 - 100, (16, 16, 255), button_font, "accept", colors, True).set_offset_pos() button6 = Button(WIDTH // 2 + 160, HEIGHT // 2, (16, 16, 255), button_font, "pause", colors, True).set_offset_pos() button7 = Button(WIDTH // 2, HEIGHT // 2 + 170, (16, 16, 255), button_font, "BACK", colors, True).set_offset_pos().set_selected() buttons = (button1, button2, button3, button4, button5, button6, button7) controls = Settings(title_text, buttons, button_sound, (16, 16, 216)) while controls.run: for event in pygame.event.get(): if event.type == pygame.QUIT: control["state"] = switch_state(controls, states.QUIT) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: controls.update_button("up") elif event.key == pygame.K_DOWN: controls.update_button("down") if controls.button_pressed() == 0: # left button = get_control(window, press_text) set_control("left", button) left_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed() == 1: # right button = get_control(window, press_text) set_control("right", button) right_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed() == 2: # up button = get_control(window, press_text) set_control("up", button) up_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed() == 3: # down button = get_control(window, press_text) set_control("down", button) down_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed() == 4: # accept button = get_control(window, press_text) set_control("accept", button) accept_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed() == 5: # pause button = get_control(window, press_text) set_control("pause", button) pause_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed() == 6: control["state"] = switch_state(controls, states.OPTIONS) if not no_joystick: if str(joy.get_hat(0)) == joy_ctrl["up"] and flag: controls.update_button("up") flag = False elif str(joy.get_hat(0)) == joy_ctrl["down"] and flag: controls.update_button("down") flag = False elif joy.get_hat(0) == (0, 0): flag = True if joy.get_button(joy_ctrl["accept"]) and flag2: flag = False flag2 = False if controls.button_pressed(True) == 0: button = get_control(window, press_text) set_control("left", button) left_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed(True) == 1: button = get_control(window, press_text) set_control("right", button) right_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed(True) == 2: button = get_control(window, press_text) set_control("up", button) up_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed(True) == 3: button = get_control(window, press_text) set_control("down", button) down_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed(True) == 4: button = get_control(window, press_text) set_control("accept", button) accept_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed(True) == 5: button = get_control(window, press_text) set_control("pause", button) pause_text = controls_font.render(button, True, (240, 240, 240)) elif controls.button_pressed(True) == 6: control["state"] = switch_state(controls, states.OPTIONS) elif not joy.get_button(joy_ctrl["accept"]): flag2 = True controls.show(window, WIDTH // 2 - 110, 100) window.blit(left_text, (WIDTH // 2 - 215, HEIGHT // 2 - 40)) window.blit(right_text, (WIDTH // 2 - 55, HEIGHT // 2 - 40)) window.blit(up_text, (WIDTH // 2 - 215, HEIGHT // 2 + 60)) window.blit(down_text, (WIDTH // 2 - 55, HEIGHT // 2 + 60)) window.blit(accept_text, (WIDTH // 2 + 150, HEIGHT // 2 - 40)) window.blit(pause_text, (WIDTH // 2 + 150, HEIGHT // 2 + 60)) pygame.display.flip() clock.tick(48)
def menu_state(window, control): flag = True flag2 = False joy_ctrl = read_all_controls() button_font = pygame.font.SysFont("calibri", 55, True) best_score = load_best_score() best_font = pygame.font.SysFont("calibri", 30, True) score_text = best_font.render("Best Score: " + str(best_score[0]), True, (216, 216, 216)) date_text = pygame.font.SysFont('calibri', 30, True).render(best_score[1], True, (216, 216, 216)) title_text = pygame.font.SysFont("calibri", 85, True).render("Snakesss...", True, (240, 240, 240)) joy_text = pygame.font.SysFont("calibri", 27, True).render("You can use a gamepad!", True, (240, 240, 240)) joy_text = pygame.transform.rotate(joy_text, -26) colors = ((0, 0, 0), (255, 255, 255)) button1 = Button(WIDTH // 2, HEIGHT // 2 - 100, (16, 16, 255), button_font, "PLAY", colors, True).set_offset_pos().set_selected() button2 = Button(WIDTH // 2, HEIGHT // 2 - 30, (16, 16, 255), button_font, "OPTIONS", colors, True).set_offset_pos() button3 = Button(WIDTH // 2, HEIGHT // 2 + 40, (16, 16, 255), button_font, "INSTRUCTIONS", colors, True).set_offset_pos() button4 = Button(WIDTH // 2, HEIGHT // 2 + 110, (16, 16, 255), button_font, "HIGH SCORES", colors, True).set_offset_pos() button5 = Button(WIDTH // 2, HEIGHT // 2 + 180, (16, 16, 255), button_font, "QUIT", colors, True).set_offset_pos() buttons = (button1, button2, button3, button4, button5) menu = MainMenu(title_text, buttons, button_sound, (16, 16, 216)) while menu.run: for event in pygame.event.get(): if event.type == pygame.QUIT: control["state"] = switch_state(menu, states.QUIT) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: menu.update_button("up") elif event.key == pygame.K_DOWN: menu.update_button("down") if menu.button_pressed() == 0: control["state"] = switch_state(menu, states.GAME_START) elif menu.button_pressed() == 1: control["state"] = switch_state(menu, states.OPTIONS) elif menu.button_pressed() == 2: pass elif menu.button_pressed() == 3: pass elif menu.button_pressed() == 4: control["state"] = switch_state(menu, states.QUIT) if not no_joystick: if str(joy.get_hat(0)) == joy_ctrl["up"] and flag: menu.update_button("up") flag = False elif str(joy.get_hat(0)) == joy_ctrl["down"] and flag: menu.update_button("down") flag = False if joy.get_hat(0) == (0, 0): flag = True if joy.get_button(joy_ctrl["accept"]) and flag2: flag2 = False if menu.button_pressed(True) == 0: control["state"] = switch_state(menu, states.GAME_START) elif menu.button_pressed(True) == 1: control["state"] = switch_state(menu, states.OPTIONS) elif menu.button_pressed(True) == 2: pass elif menu.button_pressed(True) == 3: pass elif menu.button_pressed(True) == 4: control["state"] = switch_state(menu, states.QUIT) elif not joy.get_button(joy_ctrl["accept"]): flag2 = True menu.show(window, 55, 40) window.blit(score_text, (50, HEIGHT // 2 - 155)) window.blit(date_text, (50, HEIGHT // 2 - 123)) window.blit(joy_text, (490, 110)) pygame.display.flip() clock.tick(48)