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 game_over_state(window, control): flag = True joy_ctrl = read_all_controls() background = pygame.Surface((WIDTH // 2, HEIGHT // 2)) button_font = pygame.font.SysFont("calibri", 45, True) title_text = pygame.font.SysFont("calibri", 60, True).render("Game Over", True, (240, 240, 240)) colors = ((0, 0, 0), (255, 255, 255)) button1 = Button(WIDTH // 2, HEIGHT // 2 - 20, (16, 16, 255), button_font, "RESTART", colors, True).set_offset_pos().set_selected() button2 = Button(WIDTH // 2, HEIGHT // 2 + 35, (16, 16, 255), button_font, "EXIT", colors, True).set_offset_pos() button3 = Button(WIDTH // 2, HEIGHT // 2 + 90, (16, 16, 255), button_font, "QUIT", colors, True).set_offset_pos() buttons = (button1, button2, button3) game_over = Room(buttons, button_sound) if control["game_mode"] == "easy": game_mode = states.GAME_2 elif control["game_mode"] == "hard": game_mode = states.GAME_3 else: game_mode = states.GAME_1 while game_over.run: for event in pygame.event.get(): if event.type == pygame.QUIT: control["state"] = switch_state(game_over, states.QUIT) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: game_over.update_button("up") elif event.key == pygame.K_DOWN: game_over.update_button("down") if game_over.button_pressed() == 0: control["state"] = switch_state(game_over, game_mode) elif game_over.button_pressed() == 1: control["state"] = switch_state(game_over, states.MENU) elif game_over.button_pressed() == 2: control["state"] = switch_state(game_over, states.QUIT) if not no_joystick: if str(joy.get_hat(0)) == joy_ctrl["up"] and flag: game_over.update_button("up") flag = False elif str(joy.get_hat(0)) == joy_ctrl["down"] and flag: game_over.update_button("down") flag = False elif joy.get_hat(0) == (0, 0): flag = True if joy.get_button(joy_ctrl["accept"]): if game_over.button_pressed(True) == 0: control["state"] = switch_state(game_over, game_mode) elif game_over.button_pressed(True) == 1: control["state"] = switch_state(game_over, states.MENU) elif game_over.button_pressed(True) == 2: control["state"] = switch_state(game_over, states.QUIT) window.blit(background, (WIDTH // 4, HEIGHT // 4)) background.fill((16, 16, 216)) window.blit(title_text, (WIDTH // 2 - 140, HEIGHT // 2 - 110)) game_over.show(window, 0, 0) pygame.display.flip() clock.tick(48)
def pause_state(window, control) -> int: flag = True joy_ctrl = read_all_controls() background = pygame.Surface((WIDTH // 2, HEIGHT // 2)) button_font = pygame.font.SysFont("calibri", 50, True) colors = ((0, 0, 0), (255, 255, 255)) button1 = Button(WIDTH // 2, HEIGHT // 2 - 110, (16, 16, 255), button_font, "RESUME", colors, True).set_offset_pos().set_selected() button2 = Button(WIDTH // 2, HEIGHT // 2 - 50, (16, 16, 255), button_font, "RESTART", colors, True).set_offset_pos() button3 = Button(WIDTH // 2, HEIGHT // 2 + 10, (16, 16, 255), button_font, "EXIT", colors, True).set_offset_pos() button4 = Button(WIDTH // 2, HEIGHT // 2 + 70, (16, 16, 255), button_font, "QUIT", colors, True).set_offset_pos() buttons = (button1, button2, button3, button4) pause = Room(buttons, button_sound) q = 0 if control["game_mode"] == "easy": game_mode = states.GAME_2 elif control["game_mode"] == "hard": game_mode = states.GAME_3 else: game_mode = states.GAME_1 while pause.run: for event in pygame.event.get(): if event.type == pygame.QUIT: control["state"] = switch_state(pause, states.QUIT) q = 1 elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: pause.update_button("up") elif event.key == pygame.K_DOWN: pause.update_button("down") if pause.button_pressed() == 0: pause.exit() elif pause.button_pressed() == 1: control["state"] = switch_state(pause, game_mode) q = 1 elif pause.button_pressed() == 2: control["state"] = switch_state(pause, states.MENU) q = 1 elif pause.button_pressed() == 3: control["state"] = switch_state(pause, states.QUIT) q = 1 if not no_joystick: if str(joy.get_hat(0)) == joy_ctrl["up"] and flag: pause.update_button("up") flag = False elif str(joy.get_hat(0)) == joy_ctrl["down"] and flag: pause.update_button("down") flag = False if joy.get_hat(0) == (0, 0): flag = True if joy.get_button(joy_ctrl["accept"]): if pause.button_pressed(True) == 0: pause.exit() elif pause.button_pressed(True) == 1: control["state"] = switch_state(pause, game_mode) q = 1 elif pause.button_pressed(True) == 2: control["state"] = switch_state(pause, states.MENU) q = 1 elif pause.button_pressed(True) == 3: control["state"] = switch_state(pause, states.QUIT) q = 1 window.blit(background, (WIDTH // 4, HEIGHT // 4)) background.fill((16, 16, 216)) pause.show(window, 0, 0) pygame.display.flip() clock.tick(48) return q
def options_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) title_text = title_font.render("Options", True, (240, 240, 240)) colors = ((0, 0, 0), (255, 255, 255)) button1 = Button(WIDTH // 2, HEIGHT // 2 - 110, (16, 16, 255), button_font, "CLEAR DATA", colors, True).set_offset_pos().set_selected() button2 = Button(WIDTH // 2, HEIGHT // 2 - 40, (16, 16, 255), button_font, "VOLUME", colors, True).set_offset_pos() button3 = Button(WIDTH // 2, HEIGHT // 2 + 30, (16, 16, 255), button_font, "TOGGLE FULLSCREEN", colors, True).set_offset_pos() button4 = Button(WIDTH // 2, HEIGHT // 2 + 100, (16, 16, 255), button_font, "SET CONTROLS", colors, True).set_offset_pos() button5 = Button(WIDTH // 2, HEIGHT // 2 + 170, (16, 16, 255), button_font, "BACK", colors, True).set_offset_pos() buttons = (button1, button2, button3, button4, button5) options = MainMenu(title_text, buttons, button_sound, (16, 16, 216)) while options.run: for event in pygame.event.get(): if event.type == pygame.QUIT: control["state"] = switch_state(options, states.QUIT) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: options.update_button("up") elif event.key == pygame.K_DOWN: options.update_button("down") if options.button_pressed() == 0: switch_state(options, ask_clear_st.ask_clear_state, window, control) elif options.button_pressed() == 1: pass elif options.button_pressed() == 2: window = toggle_fullscreen(control) set_fullscreen(control) elif options.button_pressed() == 3: control["state"] = switch_state(options, states.SET_CONTROLS) elif options.button_pressed() == 4: control["state"] = switch_state(options, states.MENU) if not no_joystick: if str(joy.get_hat(0)) == joy_ctrl["up"] and flag: options.update_button("up") flag = False elif str(joy.get_hat(0)) == joy_ctrl["down"] and flag: options.update_button("down") flag = False elif joy.get_hat(0) == (0, 0): flag = True if joy.get_button(joy_ctrl["accept"]) and flag2: flag2 = False if options.button_pressed(True) == 0: switch_state(options, ask_clear_st.ask_clear_state, window, control) elif options.button_pressed(True) == 1: pass elif options.button_pressed(True) == 2: window = toggle_fullscreen(control) set_fullscreen(control) elif options.button_pressed(True) == 3: control["state"] = switch_state(options, states.SET_CONTROLS) elif options.button_pressed(True) == 4: control["state"] = switch_state(options, states.MENU) elif not joy.get_button(joy_ctrl["accept"]): flag2 = True options.show(window, WIDTH // 2 - 110, 100) pygame.display.flip() clock.tick(48)
def game_start_state(window, control): flag = True flag2 = False joy_ctrl = read_all_controls() button_font = pygame.font.SysFont("calibri", 55, True) title_text = pygame.font.SysFont("calibri", 65, True).render("Choose a difficulty:", True, (240, 240, 240)) colors = ((0, 0, 0), (255, 255, 255)) rect_color = ((16, 16, 255), (14, 14, 160)) button1 = Button(WIDTH // 2 - 300, HEIGHT // 2 + 60, rect_color[0], button_font, "EASY", colors, True).set_offset_pos() button2 = Button(WIDTH // 2 - 110, HEIGHT // 2 + 60, rect_color[0] if game_unlocked(1) else rect_color[1], button_font, "NORMAL", colors, True).set_offset_pos().set_selected() button3 = Button(WIDTH // 2 + 90, HEIGHT // 2 + 60, rect_color[0] if game_unlocked(2) else rect_color[1], button_font, "HARD", colors, True).set_offset_pos() button4 = Button(WIDTH // 2 + 280, HEIGHT // 2 + 60, rect_color[0], button_font, "<None>", colors, True).set_offset_pos() button5 = Button(WIDTH // 2, HEIGHT // 2 + 180, rect_color[0], button_font, "BACK", colors, True).set_offset_pos() buttons = (button1, button2, button3, button4, button5) start = MainMenu(title_text, buttons, button_sound, (16, 16, 216)) while start.run: for event in pygame.event.get(): if event.type == pygame.QUIT: control["state"] = switch_state(start, states.QUIT) elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: start.update_button("up") elif event.key == pygame.K_DOWN: start.update_button("down") if start.button_pressed() == 0: control["state"] = switch_state(start, states.GAME_2) control["game_mode"] = "easy" elif start.button_pressed() == 1: if game_unlocked(1): control["state"] = switch_state(start, states.GAME_1) control["game_mode"] = "normal" elif start.button_pressed() == 2: if game_unlocked(2): control["state"] = switch_state(start, states.GAME_3) control["game_mode"] = "hard" elif start.button_pressed() == 3: pass elif start.button_pressed() == 4: control["state"] = switch_state(start, states.MENU) if not no_joystick: if str(joy.get_hat(0)) == joy_ctrl["up"] and flag: start.update_button("up") flag = False elif str(joy.get_hat(0)) == joy_ctrl["down"] and flag: start.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 start.button_pressed(True) == 0: control["state"] = switch_state(start, states.GAME_2) control["game_mode"] = "normal" elif start.button_pressed(True) == 1: control["state"] = switch_state(start, states.GAME_1) control["game_mode"] = "easy" elif start.button_pressed(True) == 2: control["state"] = switch_state(start, states.GAME_3) control["game_mode"] = "hard" elif start.button_pressed(True) == 3: pass elif start.button_pressed(True) == 4: control["state"] = switch_state(start, states.MENU) elif not joy.get_button(joy_ctrl["accept"]): flag2 = True start.show(window, 140, 160) pygame.display.flip() clock.tick(48)
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)