novi
novi

Reputation: 3

Pickle Isn't writing to file for leadboard app?

I am currently creating a game that tests someone's reaction time. I have it mostly done except for the leadboard part. I am trying to create a leaderboard that when the user gets a new low score, then their time is set on the leaderboard. For some reason, only their average time is displaying in the window and don't know how to fix it. Below is my code.

import pygame
import random
import pickle
import os
import bisect
os.system('cls')


pygame.init()


                
screen = pygame.display.set_mode((1200, 800))
pygame.display.set_caption("reaction time test")

font = pygame.font.SysFont(None, 30)

text = font.render("Press enter to start the test, and press any key (except enter) when the screen changes color.", True, (0,0,0))
w = font.render("NOW",0,(0,0,0))
r_surf = None
ar_surf = None
icon = pygame.image.load('unnamed.jpg')
pygame.display.set_icon(icon)

game_state = "start"
start_time = 0
average_time = 0
leaderboard = None
userinprompt = "enter your name:"
user_text = ''
active = True
count = 0
running = True

attempt_number = 0
displaycount = 0
font = pygame.font.Font('freesansbold.ttf', 32)
leaderboardsurf = None

gameactive = False
countprompt = "   / 5"
underline = "__________"

num1 = None
num2 = None
num3 = None
num4 = None
num5 = None
num6 = None
num7 = None
num8 = None
num9 = None
num10 = None

name1 = None
name2 = None
name3 = None
name4 = None
name5 = None
name6 = None
name7 = None
name8 = None
name9 = None
name10 = None

times1 = None
times2 = None
times3 = None
times4 = None
times5 = None
times6 = None
times7 = None
times8 = None
times9 = None
times10 = None

lstnames = ['','','','','','','','','','']
lsttimes = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111]
index = 0

#save the list

pickle.dump(lstnames, open("names.txt", "wb"))

#change the list


#load the saved data

loadnames = pickle.load(open("names.txt", "rb"))
        
while running:
    current_time = pygame.time.get_ticks()
    pygame.display.update()
    screen.fill(pygame.Color("white"))
    
    if gameactive == False:
        text_surface = font.render(user_text,True,(0,0,0))
        screen.blit(text_surface, (290, 10))
        count_surface = font.render(userinprompt,True,(0,0,0))
        screen.blit(count_surface, (10, 10))
        underlineprompt = font.render(underline,True,(0,0,0))
        screen.blit(underlineprompt,(290, 15))
        
    if gameactive == True: 
        text_surface = font.render(user_text,True,(0,0,0))
        screen.blit(text_surface, (10, 10))
        count_surface = None
        displaycounts = font.render(str(displaycount),True,(0,0,0))
        screen.blit(displaycounts, (10, 45))
        displaypromptcount = font.render(countprompt,True,(0,0,0))
        screen.blit(displaypromptcount, (10,45))
        
        if ar_surf:
            screen.blit(ar_surf, ar_surf.get_rect(center = (center[0], 100)))
        
        if game_state == "leaderboard":
            screen.blit(leaderboard, leaderboard.get_rect(center = (center[0], 70)))    
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            active = False
            
        if event.type == pygame.KEYDOWN:
            if active == True:
                if event.key == pygame.K_BACKSPACE:
                    user_text = user_text[:-1]
                else:
                    user_text += event.unicode 
        
        if event.type == pygame.KEYDOWN:  
            if event.key == pygame.K_RETURN:
                active = False
                user_text = user_text[:-1]
                gameactive = True
                
                if game_state == "start":
                    game_state = "wait"
                start_time = current_time + random.randint(1000, 4000)
                
            if game_state == "wait_for_reaction": 
                game_state = "wait" 
                reaction_time = (current_time - start_time) / 1000
                start_time = current_time + random.randint(1000, 4000)
                count += 1
                average_time = (average_time * (count-1) + reaction_time) / count
                r_surf = font.render(f"REACTION TIME: {reaction_time:.03f} S",5,(0,0,0))
                ar_surf = font.render(f"YOUR AVERAGE REACTION TIME IS: {average_time:.03f} S",5,(0,0,0))
                displaycount += 1
                
                if count == 5 and game_state == "wait":
                    game_state = "leaderboard"
                    text = None
                    r_surf = None
                    ar_surf = None
                    
                    lsttimes.append(average_time)
                    lsttimes.sort()
                    pickle.dump(lsttimes, open("times.txt", "wb"))
                    loadedtimes = pickle.load(open("times.txt", "rb"))
 
                    num1 = font.render("1.", True, (0,0,0))
                    num2 = font.render("2.", True, (0,0,0))
                    num3 = font.render("3.", True, (0,0,0))
                    num4 = font.render("4.", True, (0,0,0))
                    num5 = font.render("5.", True, (0,0,0))
                    num6 = font.render("6.", True, (0,0,0))
                    num7 = font.render("7.", True, (0,0,0))
                    num8 = font.render("8.", True, (0,0,0))
                    num9 = font.render("9.", True, (0,0,0))
                    num10 = font.render("10.", True, (0,0,0))
                    
                    name1 = font.render(lstnames[0], True, (0,0,0))
                    name2 = font.render(lstnames[1], True, (0,0,0))
                    name3 = font.render(lstnames[2], True, (0,0,0))
                    name4 = font.render(lstnames[3], True, (0,0,0))
                    name5 = font.render(lstnames[4], True, (0,0,0))
                    name6 = font.render(lstnames[5], True, (0,0,0))
                    name7 = font.render(lstnames[6], True, (0,0,0))
                    name8 = font.render(lstnames[7], True, (0,0,0))
                    name9 = font.render(lstnames[8], True, (0,0,0))
                    name10 = font.render(lstnames[9], True, (0,0,0))
                    
                    times1 = font.render(str(loadedtimes[0]), True, (0,0,0))
                    times2 = font.render(str(loadedtimes[1]), True, (0,0,0))
                    times3 = font.render(str(loadedtimes[2]), True, (0,0,0))
                    times4 = font.render(str(loadedtimes[3]), True, (0,0,0))
                    times5 = font.render(str(loadedtimes[4]), True, (0,0,0))
                    times6 = font.render(str(loadedtimes[5]), True, (0,0,0))
                    times7 = font.render(str(loadedtimes[6]), True, (0,0,0))
                    times8 = font.render(str(loadedtimes[7]), True, (0,0,0))
                    times9 = font.render(str(loadedtimes[8]), True, (0,0,0))
                    times10 = font.render(str(loadedtimes[9]), True, (0,0,0))
                    
                    pickle.dump(loadedtimes, open("times.txt", "wb"))

                    

                   
    if game_state == "wait":
        if current_time >= start_time:
            game_state = "wait_for_reaction"
            
    center = screen.get_rect().center
    if game_state == "start":
        screen.blit(text, text.get_rect(center = center))
    if game_state == "wait_for_reaction":
        screen.blit(w, w.get_rect(center = center))
        screen.fill(pygame.Color("pink"))
    if r_surf:
        screen.blit(r_surf, r_surf.get_rect(center = (center[0], 350)))
    if ar_surf:
        screen.blit(ar_surf, ar_surf.get_rect(center = (center[0], 100)))
    if leaderboardsurf:
        screen.blit(leaderboardsurf, leaderboardsurf.get_rect(center = (center[0], 120)))
    if num1 and num2 and num3 and num4 and num5 and num6 and num7 and num8 and num9 and num10:
        
        leaderboardsurf = font.render(f"YOUR AVERAGE REACTION TIME IS: {average_time:.03f} S",5,(0,0,0))
        leaderboard = font.render(f"TOP 10 LEADERBOARD",True,(0,0,0))
        
        screen.blit(num1, (100, 200))
        screen.blit(num2, (100, 250))
        screen.blit(num3, (100, 300))
        screen.blit(num4, (100, 350))
        screen.blit(num5, (100, 400))
        screen.blit(num6, (100, 450))
        screen.blit(num7, (100, 500))
        screen.blit(num8, (100, 550))
        screen.blit(num9, (100, 600))
        screen.blit(num10, (100, 650))
        
        screen.blit(name1, (250, 200))
        screen.blit(name2, (250, 250))
        screen.blit(name3, (250, 300))
        screen.blit(name4, (250, 350))
        screen.blit(name5, (250, 400))
        screen.blit(name6, (250, 450))
        screen.blit(name7, (250, 500))
        screen.blit(name8, (250, 550))
        screen.blit(name9, (250, 600))
        screen.blit(name10, (250, 650))
        
        screen.blit(times1, (500, 200))
        screen.blit(times2, (500, 250))
        screen.blit(times3, (500, 300))
        screen.blit(times4, (500, 350))
        screen.blit(times5, (500, 400))
        screen.blit(times6, (500, 450))
        screen.blit(times7, (500, 500))
        screen.blit(times8, (500, 550))
        screen.blit(times9, (500, 600))
        screen.blit(times10, (500, 650))
        
        
        
    pygame.display.flip()

Upvotes: 0

Views: 32

Answers (0)

Related Questions