蓝大伟 发表于 2020-8-21 22:50:21

新手问题

为什么exit()前面要加pygame.quit() ,还有正常运行但提示warning怎么回事,
import pygame

pygame.init()

screen = pygame.display.set_mode((480,700))

bg = pygame.image.load("./images/background.png")

screen.blit(bg,(0,0))
#pygame.display.update()

f = pygame.image.load("./images/me1.png")
screen.blit(f, (200, 500))
pygame.display.update()

clock = pygame.time.Clock()
hero_rect = pygame.Rect(150,300,102,126)
while True:
    clock.tick(30)
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            print("游戏结束。。。")
          pygame.quit()
            exit()

    hero_rect.y -= 5
    if hero_rect.y <= -100:
      hero_rect.y = 700
    screen.blit(bg,(0,0))
    screen.blit(f,hero_rect)
    pygame.display.update()

C:\Users\tlan2\Desktop\真·飞机大战\venv\Scripts\python.exe C:/Users/tlan2/Desktop/真·飞机大战/01.py
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
libpng warning: iCCP: known incorrect sRGB profile
游戏结束。。。

昨非 发表于 2020-8-23 21:56:26

pygame.quit()和pygame.init()函数成对出现,它运行的代码会使得Pygame库停止工作。在终止程序之前,总是应该先调用pygame.quit()。一般情况下程序退出之前,Python总是会关闭pygame,这不会真的有什么问题。但是,在IDLE中,如果一个Pygame程序在调用pygame.quit()之前就终止了,将会导致IDLE挂起。
页: [1]
查看完整版本: 新手问题