简体   繁体   English

Pygame Window 打开后关闭

[英]Pygame Window Closes after Being Opened

I'm making a article generator, however, when I run the code, the Pygame Window opens, the closes before anything happens, Ive tried adding input() but no text shows up anyways.我正在制作一个文章生成器,但是,当我运行代码时, 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()

I tried out your code on my Linux virtual machine and for me the window stayed open.我在我的 Linux 虚拟机上试用了你的代码,对我来说 window 保持打开状态。 But I was not getting any of the prompting in my terminal for a password until I did close the window.但是在我关闭 window 之前,我的终端中没有任何提示输入密码。 With that I did shuffle some bits around to prompt for a password and then display the "pygame" window.有了这个,我确实打乱了一些位以提示输入密码,然后显示“pygame”window。 Following is your code with some rearrangements.以下是您的代码,并进行了一些重新排列。

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

The result was a display window.结果是显示 window。

样品窗口

I am not sure if this program will evolve so that the password and other bits currently being entered in the terminal ultimately are displayed in the window instead, but give those changes a try.我不确定该程序是否会发展,以便当前在终端中输入的密码和其他位最终显示在 window 中,但请尝试这些更改。

You could also try taking a look at rubato .您也可以尝试查看rubato This is a python game engine targeted toward ease of use.这是一款旨在易于使用的 python 游戏引擎。 Creating a window only takes 3 lines of code:创建 window 只需要 3 行代码:

import rubato

rubato.init()

rubato.begin()

Their documentation is also super detailed!他们的文档也非常详细!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM