>> pygame.ver '1.9.1release'. Oct 23, 2015 The event variable is not initialized in the line if event.type == COOKIEVENT: You may need to indent that part of the code so that it goes inside the loop for event in pygame.event.get(): . if event.type == COOKIEVENT:`.">
Skip to content

Pygame Referenced Before Assignment Python

I have installed python27 and py27-game via MacPorts. This seems to be the best route for success with MacOSX Yosemite 10.10.3, and yet I'm still not quite there.

The error sequence happens like so:

Python 2.7.10 (default, May 252015, 13:06:17)[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license"for more information. >>> import pygame >>> pygame.init()(6, 0) >>> pygame.font.get_fonts() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/sysfont.py", line 596, in get_fonts initsysfonts() File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/sysfont.py", line 522, in initsysfonts fonts= initsysfonts_darwin() File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/sysfont.py", line 422, in initsysfonts_darwin _search_osx_font_paths(fonts) UnboundLocalError: local variable 'fonts' referenced before assignment >>> pygame.ver '1.9.1release' >>>

Is there a better install I should make? I tried homebrew and then it works but has only one font available, a very crunched-up square-ish all-capitals rounded font which I forget the name of.

I suspect there is simply something preventing the Python/PyGame/Freetype subsystem(s) from successfully enumerating my fonts, since every other app on the system can find fonts just fine.

import pygame as py,sys,random
from pygame.locals import *


py.init()
clock=py.time.Clock()

def game():#游戏界面
    global cc,dd,ee,ff,gg,aa,bb,bgmname,lines
    while True:

        for event in py.event.get():
            if event.type==QUIT:
                py.quit()
                sys.exit()

            if event.type==MOUSEBUTTONDOWN:

                lines.append(line())



        for line in lines:
            line.update ()

        py.display.update()


class line(object):

    def __init__(self):
        self.y=0
        self.num=3
        self.speed=3
        self.rec=py.draw.rect(bg,(255,255,255),Rect(800/28,0,800/14,10))
    def update(self):

        self.rec=py.draw.rect(bg,(255,255,255),Rect(800/28,self.y,800/14,10))
        self.y+=self.speed
        if y>400:
            gameover()
        return 




while True:

    bg=py.display.set_mode((800,600))

    bg.fill((255,255,255))
    py.display.set_caption("沉默而跳动的线")
    for event in py.event.get():
        if event.type==QUIT:
            py.quit()
            sys.exit()



    lines=[]
    game()