Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

python - python pygame暂停功能(python pygame pause function)

I am beginner and have a problem with my code.

(我是新手,我的代码有问题。)

Here you can see a short excerpt of my code.

(在这里,您可以看到我的代码的简短摘录。)

It's a simple snake game I created but I was trying to add a pause.

(这是我创建的一个简单的蛇游戏,但是我试图添加一个暂停。)

I got it but when I start the pause I am not able to close it.

(我知道了,但是当我开始暂停时,我无法关闭它。)

Possibly there is a basic mistake in my code so I couldn't advance.

(我的代码中可能存在一个基本错误,所以我无法前进。)

I hope you can help me.

(我希望你能帮助我。)

Thank you in advance!

(先感谢您!)

def checkquit(e):
    running = True
    pause = False
    for ev in e:
        if ev.type == pygame.QUIT:
            exit(0)
            running = True

        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
            quit(0)
            running = True

        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
            pause = True




    while pause:
        #running = False
        pause = True   
        red = (255,0,0)
        screen = pygame.display.set_mode((800,500))
        screen.fill((0,0,0))
        myfont=pygame.font.SysFont("monospace",50)
        myfonttwo=pygame.font.SysFont("monospace",10)
        text1=myfont.render("Pause!",100,red)
        text2=myfont.render("Please restart the game",100,red)
        screen.blit(text2,(10,200))
        screen.blit(text1,(230,100))
        pygame.display.update()

        for ev in e:
            if ev.type == pygame.QUIT:
                pause = False
            if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
                pause = False      
            if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
                pause = True
  ask by Rezan Yilmaz translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The pause screen is displayed in a separate application loop.

(暂停屏幕显示在单独的应用程序循环中。)

You've to get the events in that loop, too.

(您还必须在该循环中获取事件。)

Note, in your code, the content of e never changes in the "pause" loop:

(注意,在您的代码中, e的内容在“ pause”循环中永远不会改变:)

def checkquit(e):
    global running
    running = True
    pause = False
    for ev in e:
        if ev.type == pygame.QUIT:
            exit(0)
            running = True
        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
            quit(0)
            running = True
        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
            pause = True

    while pause:

        # [...]


        # get the new events
        e = pygame.event.get()

        # handle the events in the loop
        for ev in e:
            if ev.type == pygame.QUIT:
                pause = False
            if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
                pause = False      
            if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
                pause = True

runnung seems to be a variable in global namespace.

(runnung似乎是全局名称空间中的变量。)

You've to use the global statement to change its state.

(您必须使用global语句来更改其状态。)

Furthermore it is superfluous to recreate the window surface in the "pause" loop.

(此外,在“暂停”循环中重新创建窗口表面是多余的。)

screen = pygame.display.set_mode((800,500))


I recommend to change the game process.

(我建议更改游戏过程。)

Use 1 application loop.

(使用1个应用程序循环。)

eg:

(例如:)

myfont=pygame.font.SysFont("monospace",50)
myfonttwo=pygame.font.SysFont("monospace",10)
text1=myfont.render("Pause!",100,red)
text2=myfont.render("Please restart the game",100,red)

def checkquit(e):
    global running, pause
    for ev in e:
        if ev.type == pygame.QUIT:
            exit(0)
            running = True
        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
            if pause:
                pause = False
            else:
                quit(0)
                running = True
        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
            pause = not pause

running, pause = True, False
while running:
    events = pygame.event.get()
    checkquit(events)

    screen.fill((0,0,0))
    if pause:
        # draw pause screen
        screen.blit(text2,(10,200))
        screen.blit(text1,(230,100))

    else:
        # draw game
        # [...]

    pygame.display.update() 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...