def draw_menus(screen): for menu in Welcome.List: if menu.type != 'exit': # then draw the menu in the frame - not the exit menu, that is draw just in the help windown screen.blit(menu.img, menu); text_to_screen(screen, "By: Thiago Santos.", 740,600,15,BEGE) text_to_screen(screen, "Copyright. All rights reserved.",680,620,15,BEGE)
def display_scores(screen): screen.blit(Welcome.score_show_img, (0,0) ) # open and read the entire file try: # make sure there's always the file scores # if there's not, it just create one if not os.path.exists('scores.txt'): open('scores.txt', 'w').close() text_file = open("scores.txt", "r") lines = text_file.readlines() text_to_screen(screen, "Player's Name:", 50,50,25, RED) text_to_screen(screen, "Score:", 340,50,25,RED) xNamePos = 50 yNamePos = 100 xScorePos = 340 yScorePos = 100 index =1 for line in lines: lineWithoutN = "" for char in line: if char != "\n": lineWithoutN += char if index %2 !=0: text_to_screen(screen, lineWithoutN, xNamePos, yNamePos, 20, WHITE) yNamePos +=50 else: text_to_screen(screen, lineWithoutN, xScorePos, yScorePos, 20, WHITE) yScorePos +=50 index +=1 text_file.close() except: print("Error opennig file") # draw the exit button for menu in Welcome.List: if menu.type == 'exit': # draw the button exit screen.blit(menu.img, menu);
def get_user_score(screen, playerScore): events = pygame.event.get() for event in events: if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_SPACE: Welcome.getUserScoredone = True screen.blit(Welcome.score_show_img, (0,0) ) text_to_screen(screen, "Congratulations, you're in the top 5 of best scores", 80, 35, 25, RED) text_to_screen(screen, "Name", 80,100,25, RED) text_to_screen(screen, "Final Score", 330,100,25, RED) text_to_screen(screen, playerScore, 330,200,22, RED) # while user doen's press space to save score, he can still typing if not Welcome.getUserScoredone: # update txtbx Welcome.txtbx.update(events) text_to_screen(screen, "Press Space to save", 130,250,17, WHITE) else: # if already save the score in the file if Welcome.txtbxScoreSaved: # show message saved with sucess text_to_screen(screen, "Your score was successfully saved", 50,400,25, BEGE) # otherwise, save the score else: listNames =[] listScores = [] listCopyScores = [] listIndexOrder = [] if not os.path.exists('scores.txt'): open('scores.txt', 'w').close() text_file = open("scores.txt", "r") lines = text_file.readlines() index =1 done = False for line in lines: # save a diction with the names, with index being the key if index %2 !=0: name = line else: score = line listNames.append(name) listScores.append(score) listCopyScores.append(score) index +=1 text_file.close() # order the list highest = -1 indexHighest = 0 count = 1 # add player score to the list listScores.append( str( str(playerScore) + "\n") ) listCopyScores.append(playerScore) # add player name to the list of name listNames.append( str(Welcome.txtbx.getValue() +"\n") ) while not done: for i in range(0,len(listCopyScores)): if int(listCopyScores[i]) > highest: highest = int(listCopyScores[i]) indexHighest = i listIndexOrder.append(indexHighest) listCopyScores[indexHighest] = "-2" indexHighest = -1 highest = -1 if count >= len(listCopyScores): done = True count +=1 # after sort the file, time to write the "new" sequence of the file text_file = open("scores.txt", "w") for i in range(0,len(listScores)): # Game allowed to save just the top 5 scores if i <5: name = str(listNames[listIndexOrder[i]]) score = str(listScores[listIndexOrder[i]]) text_file.write(name) text_file.write(score) text_file.close() Welcome.txtbxScoreSaved = True Welcome.txtbx.resetValue() # blit txtbx on the sceen Welcome.txtbx.draw(screen) return Welcome.getUserScoredone