【问题标题】:program only works half the time程序只工作一半的时间
【发布时间】:2021-11-11 23:49:47
【问题描述】:

由于某种原因,我在 youtube 教程上看到的这个 python 程序有时只能工作。每当我运行代码时,程序中都会出现错误,告诉我程序没有响应。但偶尔代码会突然完美运行。

import pygame, sys
from sys import exit

# crosshair class
class Crosshair(pygame.sprite.Sprite):
    def __init__(self, picture_path):
        super().__init__()
        self.image = pygame.image.load(picture_path)
        self.rect = self.image.get_rect()
    def update(self):
            self.rect.center = pygame.mouse.get_pos()


# general setup
pygame.init()
clock = pygame.time.Clock()

# create the screen
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('Runner')
background = pygame.image.load("sprites/graphics/bg.png")
background = pygame.transform.scale(background, (800, 400))


pygame.mouse.set_visible(False)

#crosshair
crosshair = Crosshair('sprites/graphics/crosshair.png')
crosshair_group = pygame.sprite.Group()
crosshair_group.add(crosshair)


# while loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit
            exit()


    screen.blit(background,(0,0))
    crosshair_group.draw(screen)
    crosshair_group.update()
    clock.tick(60)
    pygame.display.update()


 

【问题讨论】:

    标签: pygame


    【解决方案1】:

    您需要使用pygame.quit() 而不是pygame.quit。缺少括号意味着您实际上并没有调用该函数,并且窗口永远不会关闭。

    当您尝试 X 出窗口时,您会收到 not responding 消息,因为正在调用 exit(),这会结束您的程序,包括事件处理循环。该窗口没有程序控制它或使其响应诸如关闭之类的输入,因此您会收到该消息。

    调用pygame.quit() 函数将在程序退出前关闭窗口,因此一切都已处理完毕。

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 1970-01-01
      • 2015-06-07
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多