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()

这里有两个需要修改的地方:

  1. 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.displaySurface = pygame.display.set_mode(self.size)中使用self.size)

  1. 你需要使用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
  1. 要执行类的__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, self.height) = 1920, 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

最新更新