def open_login(logged_in): clock = pygame.time.Clock() login_win = pygame.display.set_mode((1920, 1080)) pygame.display.set_caption("Log In") game_background = pygame.image.load("assets/game/game_background.jpg") username_text = Text(500, 375, "freesansbold", 70) username_text.set_text("Username: "******"freesansbold", 70) password_text.set_text("Password: "******"Login", 66) texts_to_draw = [username_text, password_text] input_boxes = [username_input, password_input] login_run = True while login_run: clock.tick(60) login_win.blit(game_background, (0, 0)) events = pygame.event.get() for event in events: pos = pygame.mouse.get_pos() if event.type == pygame.MOUSEBUTTONDOWN: if login_button.isOver(pos): print(f"username - {username_input.text}") print(f"password - {password_input.actual_text}") user = db.log_in(username_input.text, password_input.actual_text) if not user: logged_in = False else: logged_in = True print("Logged in is {}".format(logged_in)) mixer.fadeout(1) run_menu(user, logged_in, music=False) exit() if event.type == pygame.MOUSEMOTION: if login_button.isOver(pos): login_button.color = (54, 150, 90) else: login_button.color = (53, 189, 103) if event.type == pygame.QUIT: login_run = False for box in input_boxes: box.handle_event(event) #exit() for box in input_boxes: box.draw(login_win) for text in texts_to_draw: text.draw(login_win) login_button.draw(login_win, outline=True) pygame.display.update()
def start_play(user): """start_play starts the game screen """ #print(mixer.music.get_busy()) mixer.music.play(-1) mixer.music.set_volume(0.15) CLOCK = pygame.time.Clock() FPS = 60 textinput = InputBox(20, 20, 200, 50, '', True) textinput2 = InputBox(500, 500, 200, 50, '', False) game_win = pygame.display.set_mode((1920, 1080)) game_background = pygame.image.load("assets\\game\\game_background.jpg") player = Player("assets\\player\\snake.png", 700, 700) language1 = Language() language2 = Language() language3 = Language() language4 = Language() language5 = Language() current_lang_to_blit_index = 0 time_counter = 3 first_image = True time_to_blit = 80 failed = False images_to_blit = [[game_background, (0, 0)]] languages = [language1, language2, language3, language4, language5] input_boxes = [textinput, textinput2] best_score_text = Text(800, 200, "freesans", 40) score_text = Text(30, 25, "freesans", 30) score_text.set_text("Score: ") try_again_button = Button((255, 255, 255), 1185, 155, 350, 127, "Play Again!", 72) running = True while running: CLOCK.tick(FPS) current_fps = CLOCK.get_fps() events = pygame.event.get() for event in events: if event.type == pygame.QUIT: running = False for box in input_boxes: box.handle_event(event) if event.type == pygame.MOUSEMOTION: if try_again_button.isOver(pygame.mouse.get_pos()): try_again_button.color = (12, 152, 245) else: try_again_button.color = (255, 255, 255) for image in images_to_blit: game_win.blit(image[0], (image[1][0], image[1][1])) player.move(game_win) if time_counter % time_to_blit == 0: if first_image: first_image = False time_counter = 0 if languages[current_lang_to_blit_index].isOnScreen == False: languages[current_lang_to_blit_index].isOnScreen = True if current_lang_to_blit_index == len(languages) - 1: current_lang_to_blit_index = 0 # for lang in languages: # lang.set_on_screen() else: current_lang_to_blit_index += 1 time_counter += 1 for lang in languages: if lang.isOnScreen: if is_collided(player.x + 100, player.y, lang.x, lang.y): catch_sound.play() lang.reset_place() player.update_score() if not failed: if lang.move(game_win, player, languages) == False: failed = True score_text.replace(345, 100, 190) #! if db.update_bestscore(user, player.score): #! best_score_text.set_text("new record!") for lang in languages: lang.isOnScreen = False mixer.music.stop() failed_sound.play() for heart in player.hearts: heart.draw(game_win) best_score_text.draw(game_win) score_text.set_text(f"Score: {player.score}") score_text.draw(game_win) if failed: try_again_button.draw(game_win, True) pygame.display.update()
def run_menu(user=None, logged_in=False, music=True): """run_menu starts the menu """ if music: mixer.music.play(-1) mixer.music.set_volume(0.25) CLOCK = pygame.time.Clock() FPS = 60 white = (255, 255, 255) green = (0, 255, 0) blue = (0, 0, 128) started_playing = False # assigning values to X and Y variable menu_win = pygame.display.set_mode((1920, 1080)) pygame.display.set_caption("Main Menu") game_background = pygame.image.load("assets/game/game_background.jpg") # create a font object. # 1st parameter is the font file # which is present in pygame. # 2nd parameter is size of the font # create a text suface object, # on which text is drawn on it. if user is not None: user_text = Text(100, 100, "arial", 42) user_text.set_text(f"Welcome Back - {user['username']}") title_text = Text(830, 370, "arial", 80) title_text.set_text("Snake It!", bold=True) play_button = Button((0, 0, 0), 850, 500, 200, 100, "Play!", 46) login_button = Button((0, 0, 0), 400, 650, 200, 100, "Login", 42) signup_button = Button((0, 0, 0), 1325, 650, 200, 100, "Sign Up", 42) # set the center of the rectangular object. images_to_blit = [[game_background, (0, 0)]] #input_boxes = [textinput, textinput2] # create the display surface object # of specific dimension..e(X, Y). # set the pygame window name menu_run = True while menu_run: title_text.draw(menu_win) CLOCK.tick(FPS) current_fps = CLOCK.get_fps() events = pygame.event.get() for event in events: pos = pygame.mouse.get_pos() if event.type == pygame.MOUSEBUTTONDOWN: if play_button.isOver(pos) and logged_in: menu_run = False start_play(user) mixer.music.fadeout(1) exit() if login_button.isOver(pos): menu_run = False open_login(logged_in) exit() if event.type == pygame.MOUSEMOTION: #if play_button.isOver(pos) and logged_in: if play_button.isOver(pos): play_button.color = (92, 176, 86) else: play_button.color = (255, 255, 255) if login_button.isOver(pos) and not logged_in: login_button.color = (17, 63, 143) else: login_button.color = (255, 255, 255) if signup_button.isOver(pos) and not logged_in: signup_button.color = (76, 29, 145) else: signup_button.color = (255, 255, 255) if event.type == pygame.QUIT: menu_run = False menu_win.blit(game_background, (0, 0)) title_text.draw(menu_win) if user is not None: user_text.draw(menu_win) if logged_in == True: play_button.draw(menu_win, outline=(0, 0, 0)) else: login_button.draw(menu_win, (0, 0, 0)) signup_button.draw(menu_win, (0, 0, 0)) pygame.display.update()