def help_loop(): font = pygame.font.SysFont("arial", 24) font_height = font.get_linesize() pygame.display.set_caption("Welcome To Snake") background = 'data/bg.jpg' bg = pygame.image.load(background).convert_alpha() while True: screen.fill((0, 0, 0)) screen.blit(bg, (0, 0)) pygame.draw.polygon(screen, whiteColour, [[175, 225], [175, 450], [900, 450], [900, 225]], 4) show_message(screen, 'Press WASD to move snake around!', blackColour, 40, 200, 250) show_message(screen, 'move your snake to yellow wormhole to upgrade!', blackColour, 40, 200, 300) show_message(screen, 'eat as much food as possible!', blackColour, 40, 200, 350) show_message(screen, 'Press F to return', blackColour, 40, 350, 400) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_f: menu_loop() if event.key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT))
def game_end(total_score, score2=-1): # update total_score if score2 == -1: updatePlayerData(player_name, total_score) else: updatePlayerData(player1_name, total_score) updatePlayerData(player2_name, score2) # display ending message while True: pygame.init() pygame.mixer.init() font = pygame.font.SysFont("arial", 24) font_height = font.get_linesize() end_screen = pygame.display.set_mode(Display_Size, pygame.DOUBLEBUF, 32) pygame.display.set_caption("Welcome To Snake") background = 'data/bg.jpg' bg = pygame.image.load(background).convert_alpha() # check input while True: end_screen.fill((0, 0, 0)) end_screen.blit(bg, (0, 0)) show_message(end_screen, 'Game End', whiteColour, 60, 400, 250) show_message(end_screen, 'Press S to come back menu.', blackColour, 40, 250, 350) show_message(end_screen, 'Press L to show the leaderBoard.', blackColour, 40, 250, 400) if score2 == -1: show_message( end_screen, player_name + " you got " + str(total_score) + " points", whiteColour, 30, 250, 450) else: show_message( end_screen, player1_name + " you got " + str(total_score) + " points", whiteColour, 30, 250, 450) show_message( end_screen, player2_name + " you got " + str(score2) + " points", whiteColour, 30, 250, 500) pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_s: menu_loop() if event.key == pygame.K_l: leader_board() if event.type == pygame.QUIT: return quitHelper()
def leader_board(): # set the data, before displaying setData() pygame.init() pygame.mixer.init() font = pygame.font.SysFont("arial", 24) font_height = font.get_linesize() board_screen = pygame.display.set_mode(Display_Size, pygame.DOUBLEBUF, 32) pygame.display.set_caption("Welcome To Snake") background = 'data/bg.jpg' bg = pygame.image.load(background).convert_alpha() # load firebase db = firebase.database() storage = firebase.storage() # display the top 20 players with highest scores users_by_score = db.child("players").get() with open(SCORE_PATH, 'r') as f: score_data = json.load(f) while True: board_screen.fill((0, 0, 0)) board_screen.blit(bg, (0, 0)) show_message(board_screen, 'Welcome to leaderBoard, press F to return', blackColour, 30, 250, 200) # the position of the first displaying data x = 250 y = 520 # display avatar, name and score for each player top_users = [] for user in users_by_score.each(): player_name = user.val()['name'] player_score = user.val()['score'] image_url = storage.child( user.val()["avatarFilePath"]).get_url(None) top_users.append((player_name, player_score, image_url)) top_users.sort(key=lambda x: x[1], reverse=False) top_users = top_users[:5] for user in top_users: player_name, player_score, image_url = user[0], user[1], user[2] response = requests.get(image_url) image_file = io.BytesIO(response.content) avatar_img = pygame.image.load(image_file) board_screen.blit(avatar_img, (x, y)) show_message(board_screen, player_name + ": " + str(player_score), blackColour, 25, x + 60, y + 20) y -= 70 if (y < 240): y = 520 x -= 220 # check input pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_f: menu_loop() if event.key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT)) if event.type == pygame.QUIT: quitHelper()
# generate avatar, changing according to name entered avatar = generateAvatar(name) global avatar_image avatar_image = pygame.image.load(avatar) screen.fill((0, 0, 0)) show_message(screen, 'Please Enter Your Name: ', whiteColour, 50, 10, 10) screen.blit(avatar_image, (WIDTH // 2 - 25, HEIGHT // 2 + 50)) block = font.render(name, True, (255, 255, 255)) rect = block.get_rect() rect.center = screen.get_rect().center screen.blit(block, rect) pygame.display.flip() # quit, clean up all local data def quitHelper(): pygame.quit() # set data every time you quit setData() sys.exit() if __name__ == "__main__": pygame.init() pygame.mixer.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) total_score = 0 menu_loop()
def leader_board(): # set the data, before displaying setData() pygame.init() pygame.mixer.init() font = pygame.font.SysFont("arial", 24) font_height = font.get_linesize() board_screen = pygame.display.set_mode(Display_Size, pygame.DOUBLEBUF, 32) pygame.display.set_caption("Welcome To Snake") background = 'data/bg.jpg' bg = pygame.image.load(background).convert_alpha() # load firebase db = firebase.database() storage = firebase.storage() # display the top 20 players with highest scores users_by_score = db.child("players").order_by_child("score").limit_to_last( 20).get().val() print(users_by_score) # with open(SCORE_PATH, 'r') as f: # score_data = json.load(f) back_button = Button() while True: board_screen.fill((0, 0, 0)) board_screen.blit(bg, (0, 0)) back_button.create_button(screen, whiteColour, 30, 100, 100, 50, 60, "Back", steelBlue) # the position of the first displaying data (800,520) bottom-left to top-right x = 720 y = 520 # display avatar, name and score for each player for playerName in users_by_score: player = users_by_score[playerName] # avatar_img = pygame.image.load(player["avatarFilePath"]) image_url = storage.child(player["avatarFilePath"]).get_url(None) response = requests.get(image_url) image_file = io.BytesIO(response.content) avatar_img = pygame.image.load(image_file) board_screen.blit(avatar_img, (x, y)) show_message(board_screen, playerName + ": " + str(player["score"]), blackColour, 25, x + 60, y + 20) y -= 70 if (y < 240): y = 520 x -= 220 # check input pygame.display.update() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_f: menu_loop() if event.key == pygame.K_ESCAPE: pygame.event.post(pygame.event.Event(pygame.QUIT)) if event.type == pygame.QUIT: quitHelper() elif event.type == pygame.MOUSEBUTTONUP: if back_button.pressed(pygame.mouse.get_pos()): menu_loop()