[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] program crashes



Can someone tell me what to do (or what I need to get) to get this program to work properly?
When I run the code(straight out of the book), I get this:

Traceback (most recent call last):
  File "/Users/kevinhayes/Desktop/code/ch05/paint.py", line 107, in <module>
    main()
  File "/Users/kevinhayes/Desktop/code/ch05/paint.py", line 102, in main
    myLabel = showStats(drawColor, lineWidth)
  File "/Users/kevinhayes/Desktop/code/ch05/paint.py", line 67, in showStats
    myFont = pygame.font.SysFont("None", 20)
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pygame/sysfont.py", line 555, in SysFont
    initsysfonts()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pygame/sysfont.py", line 522, in initsysfonts
    fonts = initsysfonts_darwin()
  File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pygame/sysfont.py", line 422, in initsysfonts_darwin
    _search_osx_font_paths(fonts)
UnboundLocalError: local variable 'fonts' referenced before assignment



********************************

here is the code:

""" paint.py 
    a simple paint program"""

import pygame
    
def checkKeys(myData):
    """test for various keyboard inputs"""
    
    #extract the data
    (event, background, drawColor, lineWidth, keepGoing) = myData
    #print myData
    
    if event.key == pygame.K_q:
        #quit    
        keepGoing = False
    elif event.key == pygame.K_c:
        #clear screen
        background.fill((255, 255, 255))
    elif event.key == pygame.K_s:
        #save picture
        pygame.image.save(background, "painting.bmp")
    elif event.key == pygame.K_l:
        #load picture
        background = "">
    elif event.key == pygame.K_r:
        #red
        drawColor = (255, 0, 0)
    elif event.key == pygame.K_g:
        #green
        drawColor = (0, 255, 0)
    elif event.key == pygame.K_w:
        #white
        drawColor = (255, 255, 255)
    elif event.key == pygame.K_b:
        #blue
        drawColor = (0, 0, 255)
    elif event.key == pygame.K_k:
        #black
        drawColor = (0, 0, 0)

    #line widths
    elif event.key == pygame.K_1:
        lineWidth = 1
    elif event.key == pygame.K_2:
        lineWidth = 2
    elif event.key == pygame.K_3:
        lineWidth = 3
    elif event.key == pygame.K_4:
        lineWidth = 4
    elif event.key == pygame.K_5:
        lineWidth = 5
    elif event.key == pygame.K_6:
        lineWidth = 6
    elif event.key == pygame.K_7:
        lineWidth = 7
    elif event.key == pygame.K_8:
        lineWidth = 8
    elif event.key == pygame.K_9:
        lineWidth = 9

    #return all values 
    myData = (event, background, drawColor, lineWidth, keepGoing)
    return myData

def showStats(drawColor, lineWidth):
    """ shows the current statistics """
    myFont = pygame.font.SysFont("None", 20)
    stats = "color: %s, width: %d" % (drawColor, lineWidth)
    statSurf = myFont.render(stats, 1, (drawColor))
    return statSurf

def main():
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    pygame.display.set_caption("Paint:  (r)ed, (g)reen, (b)lue, (w)hite, blac(k), (1-9) width, (c)lear, (s)ave, (l)oad, (q)uit")
    
    background = "">
    background.fill((255, 255, 255))
    
    clock = pygame.time.Clock()
    keepGoing = True
    lineStart = (0, 0)
    drawColor = (0, 0, 0)
    lineWidth = 3
    
    while keepGoing:
        clock.tick(30)
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False
            elif event.type == pygame.MOUSEMOTION:
                lineEnd = pygame.mouse.get_pos()
                if pygame.mouse.get_pressed() == (1, 0, 0):
                    pygame.draw.line(background, drawColor, lineStart, lineEnd, lineWidth)
                lineStart = lineEnd
            elif event.type == pygame.KEYDOWN:
                myData = (event, background, drawColor, lineWidth, keepGoing)
                myData = checkKeys(myData)
                (event, background, drawColor, lineWidth, keepGoing) = myData
        screen.blit(background, (0, 0))
        myLabel = showStats(drawColor, lineWidth)
        screen.blit(myLabel, (450, 450))
        pygame.display.flip()

if __name__ == "__main__":
    main()

***********************************************************************************

I'm using python 2.5 and pygame-1.9.1release-py2.5 on Mac osx 10.4.11...any help would be appreciated.  Thanks. Kevin