[英]Pygame Window Closes after Being Opened
我正在制作一个文章生成器,但是,当我运行代码时, Pygame Window 打开,在任何事情发生之前关闭,我尝试添加 input() 但无论如何没有文本显示。
import pygame
import random
def main():
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("hello")
clock = pygame.time.Clock()
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
return
win.fill((30, 30, 30))
pygame.display.update()
clock.tick(60)
if __name__ == '__main__':
main()
pygame.quit()
pygame.display.set_caption ('Article Labs BETA')
icon = pygame.image.load ('icon book.png')
pygame.display.set_icon (icon)
space_articles = ['https://www.theguardian.com/science/2022/aug/15/starwatch-saturn-makes-closest-approach-earth']
biology_articles = []
immunotheraputic_articles = []
suprise_me = []
article = random.choice(space_articles)
float;password = 'dumbledoors2science'
print('Article Labs BETA, Made Using Python')
print('Please enter the password!')
answer = input()
if answer == 'dumbledoors2science':
print('Welcome Vesriram, Here are your articles')
print(article)
input()
else:
print('Goodbye, looks like the password was incorrect')
pygame.quit()
input()
我在我的 Linux 虚拟机上试用了你的代码,对我来说 window 保持打开状态。 但是在我关闭 window 之前,我的终端中没有任何提示输入密码。 有了这个,我确实打乱了一些位以提示输入密码,然后显示“pygame”window。 以下是您的代码,并进行了一些重新排列。
import pygame
import random
import sys
def main():
pygame.init()
win = pygame.display.set_mode((500, 500))
#pygame.display.set_caption("Hello")
pygame.display.set_caption ('Article Labs BETA')
icon = pygame.image.load ('icon book.png')
pygame.display.set_icon (icon)
clock = pygame.time.Clock()
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
return
win.fill((130,130, 130)) # Made the window gray
pygame.display.update()
clock.tick(60)
space_articles = ['https://www.theguardian.com/science/2022/aug/15/starwatch-saturn-makes-closest-approach-earth']
biology_articles = []
immunotheraputic_articles = []
suprise_me = []
article = random.choice(space_articles)
float;password = 'dumbledoors2science'
print('Article Labs BETA, Made Using Python')
print()
answer = input('Please enter the password! ')
if answer == 'dumbledoors2science':
print('Welcome Vesriram, Here are your articles')
print(article)
else:
print('Goodbye, looks like the password was incorrect')
pygame.quit()
sys.exit()
if __name__ == '__main__':
main()
pygame.quit()
结果是显示 window。
我不确定该程序是否会发展,以便当前在终端中输入的密码和其他位最终显示在 window 中,但请尝试这些更改。
您也可以尝试查看rubato 。 这是一款旨在易于使用的 python 游戏引擎。 创建 window 只需要 3 行代码:
import rubato
rubato.init()
rubato.begin()
他们的文档也非常详细!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.