Problems with Hangman in Pygame
Question: I'm trying to find out what is wrong with the folowing code:

    
    
    import pygame, sys, random, linecache
    from pygame.locals import *
    
    #Start Pygame, define Pygame objects
    pygame.init()
    hangmanSurfaceObj = pygame.display.set_mode((640,480), 0)
    clock = pygame.time.Clock()
    
    #Define colors
    redColor = pygame.Color(255,0,0)
    greenColor = pygame.Color(0,255,0)
    blueColor = pygame.Color(0,0,255)
    whiteColor = pygame.Color(255,255,255)
    blackColor = pygame.Color(0,0,0)
    tardisBlueColor = pygame.Color(16,35,114)
    bgColor = whiteColor
    
    #Import Images
    hangmanImage = pygame.image.load('hangmanResources/hangman.png')
    
    #Import sounds
    sadTromboneSound = pygame.mixer.music.load('hangmanResources/sadTrombone.mp3')
    
    #Import Font
    fontObj = pygame.font.Font('hangmanResources/Avenir_95_Black.ttf', 18)
    
    #Define global variables
    currentWord = 0
    usrWord = ''
    screenWord = []
    i = 0
    currentChar = ''
    guesses = 0
    
    def terminate():
        pygame.quit()
        sys.exit()
    
    def drawRects(tries):
    
        if tries<=0:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(242,65,65,65))
        if tries<=1:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(257,90,35,4))
        if tries<=2:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(261,110,27,1))
        if tries<=3:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(274,130,1,114))
        if tries<=4:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(198,160,76,1))
        if tries<=5:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(275,160,75,1))
        if tries<=6:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(210,244,64,85))
        if tries<=7:
            hangmanSurfaceObj.fill(bgColor, pygame.Rect(274,244,51,87))
    
    
    def newGame(players):
        if players == 1:
            line_number = random.randint(0, 2283)
            usrWord = linecache.getline('hangmanResources/words.txt', line_number)
            usrWord = list(usrWord)
            del usrWord[-1]
            print(usrWord)
            screenWord = ['_']*len(usrWord)
            print(screenWord)
    
    def checkChar(usrWord):
        print('hi')
        i=0
        addGuess = 1
        print(len(usrWord))
        while i <= (len(usrWord)-1):
            print('hi2')
            if currentChar.lower == usrWord[i]:
                print('hi3')
                screenWord[i] = currentChar
                addGuess = 0
    
        return addGuess
    
    
    newGame(1)
    
    while True:
        gameRunning = 1
        while gameRunning==1:
    
    
            for event in pygame.event.get(QUIT):
                terminate()
    
            for event in pygame.event.get(KEYDOWN):
                if event.key==K_ESCAPE:
                    terminate()
                else:
                    currentChar = event.unicode
                    guesses +=checkChar(usrWord)
                    print(currentChar)
                    print(screenWord)
                    msg = ''.join(screenWord)
    
                    msgSurfaceObj = fontObj.render(msg, False, blackColor)
                    msgRectObj = msgSurfaceObj.get_rect()
                    msgRectObj.topleft = (10, 20)
                    hangmanSurfaceObj.blit(msgSurfaceObj, msgRectObj)
    
    
            hangmanSurfaceObj.fill(bgColor)
            hangmanSurfaceObj.fill(blueColor, pygame.Rect(400,0,640,480))
    
            hangmanSurfaceObj.blit(hangmanImage, (0,0))
    
            if guesses<=7:
                drawRects(guesses)
    
            else:
                won=0
                gameRunning = 0
    
    
            pygame.display.update()
            clock.tick(30)
    
        newGame(1)
    

It is supposed to be a hangman game with a hanging post showing, letter blanks
showing, and a blue rectangle on the right (where game controls will
eventually go).

When I go to run it the hanging post appears, the blue rectangle on the left
appears, and although no errors pop up, the letter blanks don't show up, and
there is a strange blue box connected to the corner of the blue rectangle.
More importantly, though, the problem that I'm having is that once you enter a
character, the string which holds the data for the eventual on-screen output
doesn't change. For instance, if the guess word is "Turtle" and I enter "t",
then it should change from `['_','_','_','_','_','_']` to
`['t','_','_','t','_','_']`, but it doesn't.

I used Python 3.1.3 and Pygame 1.9.1.

I searched the Python as well as Pygame documentations for the functions that
I am using, but unfortunately I could find no recourse.

The original file, as well as the resources, can be found
[here](http://sourceforge.net/projects/hangman4pygame/files/ "Hangman for
Pygame on SourceForge").

Thanks so much!


Answer: You are blitting the phrase only during keydown then in every frame you clean
the screen. At 30 fps you are lucky if you see a blink of the phrase. You
should check the frase at key down but blit the frase on every frame.

    
    
         ...
         for event in pygame.event.get(KEYDOWN):
            if event.key==K_ESCAPE:
                terminate()
            else:
                currentChar = event.unicode
                guesses +=checkChar(usrWord)
                print(currentChar)
                print(screenWord)
                msg = ''.join(screenWord)
    
        # Clean background
        hangmanSurfaceObj.fill(bgColor)
    
        # Then blit message
        msgSurfaceObj = fontObj.render(msg, False, blackColor)
        msgRectObj = msgSurfaceObj.get_rect()
        msgRectObj.topleft = (10, 20)
        hangmanSurfaceObj.blit(msgSurfaceObj, msgRectObj)
    
        ...
    

**Edit**

About the blue rectangle attached to the control box, it's because you are
making the control box 200px wide and then overwriting the first 100px with
your drawRects() rountine.

You can either make the control box 100px wide:

    
    
    hangmanSurfaceObj.fill(blueColor, pygame.Rect(400,0,640,480))
    

or modify drawRects() routine so the rects don't overlap the control box.

