Pygame window 在 运行 崩溃

Pygame window crashing on run

我正在尝试制作一个带有 window 的刽子手游戏,它会显示每次你猜错时都会绘制刽子手。我正在为它使用 pygame 库,但是当我运行 程序打开 window 然后崩溃。如何修复?

代码如下:

import pygame

def wait():
    result=None
    import msvcrt 
    result=msvcrt.getch()
    return result

window=pygame.display.set_mode((600, 600))
window.fill((0, 0, 0))

running=True
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
    if running==False:
        pygame.quit()

    print ("Press any key to start a game")
    wait()

    Wrong=0
    Word=list(input("Give the hidden word"))
    Hidden=[]
    Guess=input("Guess a letter or the word")

    for j in range(len(Word)):
        Hidden.append("_")

    if Guess in Word:
        for i in range(len(Word)):
            if Guess==Word[i]:
                Hidden.pop(i)
                Hidden.insert(i,Guess)
                print (Hidden)
    else:
        Wrong+=1

    if Wrong==1:
        White=pygame.draw.circle(window, (255, 255, 255),[300, 80], 50, 3)
        pygame.display.update()
    elif Wrong==2:
        Blue=pygame.draw.line(window, (0, 0, 255),[300, 130],[300, 350], 3)
        pygame.display.update()
    elif Wrong==3:
        Green=pygame.draw.line(window, (0, 255, 0),[300, 200],[400, 300], 3)
        pygame.display.update()
    elif Wrong==4:
        Red=pygame.draw.line(window, (255, 0, 0),[300, 200],[200, 300], 3)
        pygame.display.update()
    elif Wrong==5:
        Cyan=pygame.draw.line(window, (0, 255, 255),[300, 350],[200, 500], 3)
        pygame.display.update()
    elif Wrong==6:
        Yellow=pygame.draw.line(window, (0, 255, 255),[300, 350],[400, 500], 3)
        pygame.display.update()
    elif Wrong==7:
        print ("You Lost")
  1. 每个pygame程序在开始时都需要pygame.init()
  2. 在程序结束时只使用一次pygame.display.update()
  3. 你的程序死机了,因为 input() 打断了你程序的主循环

Pygame 起始文件:

import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

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

    #Your code...

    pygame.display.update()
    clock.tick(FPS)

你可以通过在 pygame window 中添加一个输入框来解决这个问题:

希望对你有所帮助,亚当