[英]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.