[Tutor] crash - switching between text window and graphics/game window (high score)

D. Hartley denise.hartley at gmail.com
Wed Apr 20 00:29:42 CEST 2005


Ok. I got in the working code to have a high score list, and it even
saves/loads one now (!!).  The only problem is, clicking back from the
text window to the game window (only after you entered in your name to
go onto the high score list), closes the program. you dont get to say
y/n to another game, it just shuts down. i cant run a debugger on it,
because the error doesnt happen when I F5-run the game from the python
IDLE script, only when i have ran it by doubleclicking on it (which is
how you're supposed to run it. otherwise it doesnt shut down
properly). i really really REALLY want to keep the high score part of
my program, because i worked really hard to get this to work. but i
have to have this program running with NO crashes in like a week (it's
a birthday present, and the birthday is approaching fast!)

Here's the "end of game" code:

            #game over..
            if lives == 0:
### trying addscore

                def add_score():
 #                   high_scores = [(1000,"Denise"), (945,"Denise"),
  #                                 (883,"Denise"),(823,"Grant"),
   #                                (779,"Aaron"), (702,"Pete"),
    #                               (555,"Tom"), (443,"Tom"),
     #                              (442,"Robin"), (4,"Pete")]
                    high_scores = pickle.load(file("scores.pik"))
                    score = total_enemy_hits
                    if score > high_scores[-1][0]:
                        print "Ta da! You got", total_enemy_hits,
"Ranch Delivery Devices!"
                        name = read_string("You made the high score
list! What's your name? ")
                        user_score = (score,name)
                        high_scores.append(user_score)
                        high_scores.sort(reverse=True)
                        del high_scores[-1]
                        pickle.dump(high_scores, file("scores.pik", "w"))
                        for score, name in high_scores:
                            slip = 30 - len(name)
                            slip_amt = slip*" "
                            prefix = 5*" "
                            print prefix,name,slip_amt,score
                    else:
                        print "Sorry, you only got", total_enemy_hits,
"Ranch Delivery Devices."
                        print "You didn't quite make the high score list!"
                        for score, name in high_scores:
                            slip = 30 - len(name)
                            slip_amt = slip*" "
                            prefix = 5*" "
                            print prefix,name,slip_amt,score
                        print "Better luck next time!"

 #               pdb.set_trace()
                add_score()

                end.play()
                showGameOver(screen, background_image)                
                pygame.display.flip()
                

                answer = ""
                while not answer in ("y","n"):
                   for event in pygame.event.get():
                      if event.type == KEYDOWN:
                         if event.key == K_n:
                            answer = "n"
                         elif event.key == K_y:
                            answer = "y"
                if answer == "n":
                    running = 0
                else:
                    return 1
    
            #refresh the display
            pygame.event.pump()
            pygame.display.flip()

    #well, nice playing with you...
    screen = pygame.display.set_mode((640, 480))
    return 0


Can anyone tell me why it crashes? you click to the text window when
there's the "congrats, you made the high score list, enter your name"
prompt, enter your name and hit enter, and it displays the high
scores, with your name in it, just as it should. but then when you
click back to the game screen, poof! it all closes.  Of course if you
dont make the high score list, you can click to the text window and
click back to the game window and it operates like it should.

Any suggestions would be appreciated!!  Again, I'd like to have it do
all this score stuff in the game/graphics window, and then it wouldnt
have that crash at all. But I dont know how to do that.

Please help!

Thanks again :)

~Denise


More information about the Tutor mailing list