python-text window vs. game/graphics window

D. Hartley denise.hartley at gmail.com
Wed Apr 20 14:57:48 EDT 2005


Hello, group!

I am asking anyone who is knowledgeable about entering text into the
text window while a game/graphics window is running (am using pygame).
 I have asked this on the 'tutor' mailing list and no one could/would
answer it.  I am making a space invaders clone for my Python Teacher's
birthday, and so obviously can not ask him for help! Even the debugger
wouldn't shed light on the situation, due to when it happens:

I am trying to make a high score list.  If you do not score high
enough, the list displays in the text window (fine), and you can type
y/n in the game window for a new game. If you DO make the high score
list, it prompts you to enter your name on the text window (and hit
enter), and displays the updated score list (great).  However, when I
click back to the graphics window, the whole thing closes/shuts
down/crashes.  For the life of me I cant figure it out.  I have looked
at the order of my steps, I've tried to follow it through piece by
piece, and like I said I tried to use the debugger to step through it
- but since the game just closes out, it doesnt tell me anything.

1. How can I stop this crash from happening? I have copied and pasted
the "game over" section of my code below, and am attaching the entire
code to the email, in case that would be helpful.

2. I'd REALLY like to make it display the high scores/prompt for user
name on the game/graphics window, not in the text window, anyway - and
that would eliminate the problem it seems to have when using
keystrokes in the text window and then trying to switch back to the
game window. (it's not a click-specific issue, alt-tab does it too).


I apologize for the "newbie" nature of this question to a more
advanced list, but I have tried everything else I can think of and am
at my wits' end.  The 'deadline' (birthday) is in a week and I'm
stuck.  I'd appreciate any comments or suggestions you might have, in
as simple of language as you can offer them, ha ha.  I *am* new to
python, and so my code may not be so elegant. But I hope you can read
it enough to see what I'm doing wrong or possibly just offer
suggestions for displaying it all in the graphics window and avoiding
the problem altogether.

Thank you so much for your time and ideas!

Sincerely,
Denise

            #game over..
            if lives == 0:

                def add_score():
                    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!"

                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
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Copy of play w scorelist2.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20050420/0fdcae9f/attachment.ksh>


More information about the Python-list mailing list