【问题标题】:Run pygame codes and it shows 'Module 'pygame' has no 'init' member' and white screen window运行 pygame 代码,它显示 'Module 'pygame' has no 'init' member' 和白屏窗口
【发布时间】:2018-02-18 12:52:37
【问题描述】:

我是学习Python的初学者,在调试从书中复制的一系列代码时遇到了一个问题,我得到一个白屏窗口而不是一个带图案的窗口,代码如下:

import pygame, sys, random
import pygame.locals as GAME_GLOBALS
import pygame.event as GAME_EVENTS
pygame.init()
windowWidth = 640
windowHeight = 480
surface = pygame.display.set_mode((windowWidth, windowHeight))
pygame.display.set_caption('Pygame Shapes!')

while True:
    surface.fill((200, 0, 0))
    pygame.draw.rect(surface, (255, 0, 0), (random.randint(0, windowWidth), random.randint(0, windowHeight), 10, 10))

greenSquareX = windowWidth / 2
greenSquareY = windowHeight / 2

while True:
    surface.fill((0, 0, 0))
    pygame.draw.rect(surface, (0, 255, 0),
     (greenSquareX, greenSquareY, 10, 10))
    greenSquareX += 1
    # greenSquareY += 1
    pygame.draw.rect(surface, (0, 0, 255),
       (blueSquareX, blueSquareY, 10, 10))

    blueSquareX = 0.0
    blueSquareY = 0.0
    blueSquareVX = 1
    blueSquareVy = 1

while True:
    surface.fill((0, 0, 0))
    pygame.draw.rect(surface, (0, 0, 255),
       (blueSquareX, blueSquareY, 10, 10))
    blueSquareX += blueSquareVX
    blueSquareY += blueSquareVY
    blueSquareVX += 0.1
    blueSquareVY += 0.1

    for event in GAME_EVENTS.GET():
        if event.type == GAME_GLOBALS.QUIT:
            pygame.quit()
            sys.exit()
    pygame.dispaly.update()

我得到的第一个错误是:E1101:Module 'pygame' has no 'init' member'(4 ,1)。我之前用pygame.init() 运行过其他Python 代码,结果很好;但这次我得到一个白屏窗口。我的代码有什么问题?感谢您的帮助!

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    这花了我大量的调试来修复,哈哈。

    首先,您没有在任何while 循环中转义或更新。一旦代码进入您的第一个 while 循环,此后将不会更新任何内容(pygame.display) - 导致您的白屏综合症。

    此外,请统一命名变量并检查代码中的拼写错误。您创建了一个blueSquareVy 变量只是为了稍后尝试将其称为blueSquareVY,并且您在代码末尾拼错了pygame.display.update()。大写很重要——这只是一些错别字!

    代码中也存在逻辑错误。在将图形绘制到屏幕上之后,您应该填充窗口表面。查看您的变量,您似乎希望小方块移动。您将在 while 循环之外创建位置变量,就像您在循环中创建它们一样,它们在每次循环迭代时都会以其初始值重新创建。

    带注释和错误修正的代码:

    import pygame, sys, random
    import pygame.locals as GAME_GLOBALS
    import pygame.event as GAME_EVENTS
    pygame.init()
    windowWidth = 640
    windowHeight = 480
    surface = pygame.display.set_mode((windowWidth, windowHeight))
    pygame.display.set_caption('Pygame Shapes!')
    
    # Renamed variables to be consistent
    # Moved variables out of the while loop
    greenSquareX = windowWidth / 2
    greenSquareY = windowHeight / 2
    blueSquareX = 0.0
    blueSquareY = 0.0
    blueSquareVX = 1
    blueSquareVY = 1
    
    # Joined the two while loops into one
    while True:
        surface.fill((200, 0, 0))
        pygame.draw.rect(surface, (255, 0, 0), (random.randint(0, windowWidth), random.randint(0, windowHeight), 10, 10))
    
        surface.fill((0, 0, 0))
        pygame.draw.rect(surface, (0, 255, 0),
         (greenSquareX, greenSquareY, 10, 10))
        greenSquareX += 1
        greenSquareY += 1
    
        pygame.draw.rect(surface, (0, 0, 255),
           (blueSquareX, blueSquareY, 10, 10))
        blueSquareX += blueSquareVX
        blueSquareY += blueSquareVY
        blueSquareVX += 0.1
        blueSquareVY += 0.1
    
        # Do not capitalize the .get() method for pygame.event class
        for event in GAME_EVENTS.get():
            if event.type == GAME_GLOBALS.QUIT:
                pygame.quit()
                sys.exit()
    
        # Misspelled pygame.display
        pygame.display.update()
    

    希望这有帮助!

    【讨论】:

    • 哦,谢谢!XXXD 你检查我的代码真是太好了,我从中学到了很多东西!原始代码来自 Make Games with Python。 2-Raspberry(2016),第 22 页,再次抱歉我的错别字,我应该在粘贴前仔细检查。
    • @ruihuiliu 很高兴我能帮上忙!前段时间我也看过你上面提到的那本书。如果它解决了您的问题,请不要忘记接受答案。
    • 当然,我会的!这是我第一次使用这个网站,所以我花了几分钟才Bing out(google被禁止)如何在stackoverflow上接受答案,哈哈,再次感谢!
    • 您好,对于互操作性很抱歉,您想看看我从Make Games with Python 中得到的另一个问题。覆盆子(2016)如果你有时间?恐怕这本书没有提供正确的代码示例,这是我的问题stackoverflow.com/questions/48887664/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2021-03-13
    • 2022-12-23
    • 1970-01-01
    相关资源
    最近更新 更多