如何解决Pygame 窗口未加载
当我编译我的代码时,没有错误,但即使代码编译,pygame 窗口也不会弹出。我只是不断收到“来自 pygame 社区的你好”消息。我尝试运行其他程序,但我的 pygame 版本仍然有效。
import pygame
import os
class Application:
def __init__(self):
self.isRunning = True
self.displaySurface = None
self.fpsClock = None
self.attractors = []
self.size = self.width,self.height = 1920,1080
pygame.quit()
解决方法
需要进行一些更改:
-
self.displaySurface
应该等于pygame.display.set_mode(self.size)
self.displaySurface = pygame.display.set_mode(self.size) #Making the screen
(注意需要将self.size = (self.width,self.height) = 1920,1080
行移到self.displaySurface = pygame.display.set_mode(self.size)
上方,以便可以在self.size
中使用self.displaySurface = pygame.display.set_mode(self.size)
)
- 您需要使用 while 循环来检查游戏屏幕上发生的所有事件并在每一帧更新屏幕
while self.isRunning:
for event in pygame.event.get():#Get all the events
if event.type == pygame.QUIT:#If the users closes the program by clicking the 'X' button
pygame.quit()#De-initialise pygame
sys.exit()#Quit the app
pygame.display.update() #Update the screen
- 要执行类的
__init__
方法,您需要创建一个对象
app = Application()#Creating an object
所以最终的代码应该看起来像:
import pygame
import os
import sys
class Application:
def __init__(self):
pygame.init()
self.isRunning = True
self.size = (self.width,1080 #A tuple
self.displaySurface = pygame.display.set_mode(self.size) #Making the screen
self.fpsClock = None
self.attractors = []
while True:
for event in pygame.event.get():#Get all the events
if event.type == pygame.QUIT:#If the user closes the program by clicking the 'X' button
pygame.quit()#De-initialise pygame
sys.exit()#Quit the app
pygame.display.update() #Update the screen
app = Application()#Creating an object
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。