当前位置: 首页 > 知识库问答 >
问题:

Pyplay:窗口没有响应

王飞英
2023-03-14

我是一个ameatur编码器,我最近开始使用pyplay,我一直在编码一些例子,我刚刚找到一个执行时无法正常加载的例子。我很确定我写对了,但我不知道为什么它不会运行。我运行Python 3.6和Pygame1.9.3。**

*我知道pygame的版本是1.9,但我不知道最后一个数字。

这是代码:

# This just imports all the Pygame modules
import pygame

# This MUST BE the first thing you put into a program!
pygame.init()
# This tells pygame to open a windo 640 pixels by 480
screen = pygame.display.set_mode((640, 480))

# This is called a while loop
# This is the simplest form of an event loop
# This line procceses the window: Such as telling it to close when the user     hits the Escape Key
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
# Y = DOWN
class Game(object):
    def main(self, screen):
        image = pygame.image.load('player.png')

        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key ==     pygame.K_ESCAPE:
                    return

            screen.fill((200, 200, 200))
            screen.blit(image, (320, 240))
            pygame.display.flip()

    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((640, 480))
        Game().main(screen)

共有1个答案

诸葛乐逸
2023-03-14

我已经修改了你的代码,所以它现在可以运行了。我发现的主要问题是,当代码运行时,您不能确切地理解代码在做什么。

问题固定:

>

取消插入如果uuuu name_uuuu==“\uuuuu main\uuuuu”:分支。这个分支从来不是类的一部分。在类中有方法和变量。就是这样。

除了这些问题之外,代码还可以,但是在继续之前,请确保您理解它的作用。

我希望这个答案对你有所帮助,如果你还有任何问题,请随时在下面发表评论!

修订后的代码示例:

# This just imports all the Pygame modules
import pygame

class Game(object):
    def main(self, screen):
        image = pygame.image.load('player.png')

        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return

            screen.fill((200, 200, 200))
            screen.blit(image, (320, 240))
            pygame.display.flip()



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    Game().main(screen)
 类似资料:
  • 我在youtube上的视频中找到了一个教程,那个家伙正在运行以下代码: 如果event.type==pyplay。退出: 当我写“pygame.quit:”而不是“pygame.quit:”(在for循环中)时,窗口不会关闭。我完全是个初学者。这是我们必须大写的命令吗?有人能解释一下原因吗?

  • 这段简单的代码在几秒钟(大约 5 秒)后崩溃(窗口没有响应)。 如果我取消注释注释的行,我可以清楚地看到程序在显示 47 到 50 之间的值时失控。 我使用 python 2.7 和 pygame 1.9.2、Windows 8(64 位)和 Eclipse PyDev。

  • 这是完整的代码,这是一个滚动程序的原型,它被打破了(尽管如此),但是每当我试图点击它时,我都会看到没有响应的窗口

  • 是否可以创建没有背景或窗口的ImGui屏幕?它只是可以在屏幕上拖动的ImGui窗口。它看起来像这样,但不是蓝色背景,您只会看到它下面的应用程序。

  • 我是一个对使用python相当陌生的爱好者。自从我安装 Pygame 以来已经一周了,经过多次尝试,当我运行任何程序时,我仍然无法启动或打开 Pygame 窗口。我已经看了尽可能多的教程,并阅读了我能找到的关于类似问题的所有文章。我已经复制了在其他网站上找到的所有解决方案,但问题仍然存在。然而,更奇怪的是,我正在使用的IDE(Pycharm)很少输出错误消息,而是简单地继续运行,但从未启动Pyga

  • 我运行以下代码: 当我运行代码时,pygame窗口打开,但它是一个空白(黑色)屏幕。我还收到以下错误消息:Traceback(最近一次呼叫last): 文件"C:/用户/Draco/OneDrive/文档/编程/graphics.py",第13行,screen.blit(img(0,0))TypeError:'pyplay.Surface'对象不可调用 我试图打开的图像保存为JPG文件。图像保存在