pygame没有定义,现在只是一个语法错误

时间:2014-12-12 15:55:01

标签: pygame nameerror

希望你们能提供帮助。最初由shell引发的错误是' nameError:' pygame'没有定义',这似乎很奇怪,因为没有太大的改变我的代码以前运行良好。现在摆弄代码 - 注释掉行,没有什么太激烈 - 从python空闲执行它会引发一个基本的语法错误。违规代码:

pygame.display.update()

如果此行已注释掉,则会在下一行引发新的语法错误;违规代码:

if isItSnap(middleDeck):

我查看了这篇文章 - 'pygame' is not defined - 但答案并不适用。

如果我要发布整个节目,请告诉我。 pygame当然是导入并初始化的。

#loop for actual gameplay
while True:
    for event in pygame.event.get():
        #event.type, check identifier the 'type' of event it is
        #pygame.QUIT, all 'type's that an event can be are stored as constants
        #in pygame.locals module in fact the 'pygame' preceeding QUIT is unnecessary
        #QUIT is contant variable in pygame.locals module. now in our global namespace
        #finally to generate an event with type 'QUIT' user clicks red boxed cross on window
        if event.type == pygame.QUIT:
            terminate()
        if event.type == pygame.KEYDOWN and event.key == K_ESCAPE:
            terminate()
        if event.type == pygame.KEYDOWN:#check
            if event.key == pygame.turnCardButton:#anyone can turn card over
                #lets us know user wants to flip card
                turnCard = True

    #turn card, only for computer atm
    if turnCard:
        turnCard(currentPlayer, middleDeck)
        turnCard = False

    #draw everything to screen, only need to blit latest card, no need whole screen
    #or draw everything end prog just draw the latest card here???
    windowSurface.fill(GREEN)#clear screen
    displayRect = windowSurface.get_rect()
    for cards in middleDeck:
        card = pygame.image.load(imageLocator[cards])
        #atm there is no rotation, all cards aligned perfectly
        windowSurface.blit(card, (displayRect.centerx - CARDX, displayRect.centery - CARDY)

    #update screen for user, this when they will try call snap
    pygame.display.update()

    if isItSnap(middleDeck):
        #need users to see card before we enter into loop
        #check rules snap, maybe need to timer if noone called can continue turning cards
        #clear event list as we want to find player with the QUICK DRAW YEEHAA
        pygame.event.clear()
        while True:
            #CURRENTLY NO SCORE CALLING SLOW IN PLAY
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    #who has one this round!?
                    if event.key == playerOneSnapButton:
                        #player one takes all the cards
                        player1 = middleDeck + player1
                        middleDeck = []
                        #just for now UNTIL ALL DRAWING HAS BEEN OPTIMIZED
                        windowSurface.fill(GREEN)
                        pygame.display.update()
                        break

                    elif event.key == playerTwoSnapButton:
                        player2 = middleDeck + player2
                        middleDeck = []
                        windowSurface.fill(GREEN)
                        pygame.display.update()
                        break                               
    else:
        time.sleep(playSpeed)#to slow game a little

1 个答案:

答案 0 :(得分:0)

该问题是由拼写错误引起的。

之后缺少右括号

windowSurface.blit(card, (displayRect.centerx - CARDX, displayRect.centery - CARDY)

windowSurface.blit(card, (displayRect.centerx - CARDX, displayRect.centery - CARDY))