【问题标题】:pygame is not defined, now just a syntax errorpygame 没有定义,现在只是一个语法错误
【发布时间】:2014-12-12 15:55:01
【问题描述】:

希望大家能帮忙。最初由 shell 引发的错误是'nameError: 'pygame' is not defined',这看起来很奇怪,因为没有太大的改变,我的代码之前运行良好。现在在摆弄代码之后——注释掉行,没什么太激烈的——从 python idle 执行它会引发一个基本的语法错误。违规代码:

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

【问题讨论】:

  • 您在上一行中缺少) windowSurface.blit(card, (displayRect.centerx - CARDX, displayRect.centery - CARDY)
  • 哦,是的,谢谢你好心的陌生人。

标签: pygame nameerror


【解决方案1】:

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

后缺少右括号

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    相关资源
    最近更新 更多