繁体   English   中英

当我的暂停在pygame中播放我的game_loop函数时,如何阻止我的游戏重启?

[英]How do I stop my game from restarting when my pause plays my game_loop function in pygame?

所以,我在pygame中制作一个简单的视频游戏,它非常接近完成。 我正在我的暂停屏幕上工作,但是当我点击p(我选择的按钮)时它暂停了但是当我点击继续时它重新启动游戏。 我查了一下代码,发现没有错。 然后,我在这里找到了问题论坛并查找了问题,我找到了一些东西,但他们并没有像我一样做代码,最重要的是答案很难理解。 暂停功能paused() ,取消暂停,是因为unpause() 你能试着找出它出错的地方吗? 这是我的源代码:

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 600
dark_blue = (0,178,210)
blue = (109,178,201)
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
brown = (102, 51, 0)
ship_width = 96
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('_Asteroid Belt_')
clock = pygame.time.Clock()
thingImg = pygame.image.load("/home/pi/Desktop/asteroid.png")
shipImg = pygame.image.load('/home/pi/Desktop/spaceship.png')
bg = pygame.image.load("/home/pi/Desktop/py.png")
bgi = pygame.image.load("/home/pi/Desktop/introbg.jpg")
pause = False
def quitgame():
    pygame.quit()
    quit()
def unpause():
    global pause
    pause = False

def paused():


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

        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Paused", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)


        button("Start Flight",100,450,200,100,dark_blue,blue,game_loop)
        button("Land on Desktop",500,450,200,100,dark_blue,blue,quitgame)



        #pygame.draw.rect(gameDisplay,dark_blue,(500,450,200,100))


        pygame.display.update()
        clock.tick(15)
def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        gameDisplay.blit(bgi,(0,0))
        largeText = pygame.font.Font('freesansbold.ttf',115)
        TextSurf, TextRect = text_objects("Asteroid Belt", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)


        button("Start Flight",100,450,200,100,dark_blue,blue,game_loop)
        button("Land on Desktop",500,450,200,100,dark_blue,blue,quitgame)



        #pygame.draw.rect(gameDisplay,dark_blue,(500,450,200,100))


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

def message_display2(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)


def things_dodged(count):
    font = pygame.font.SysFont(None,25)
    text = font.render("Dodged: "+str(count), True, white)
    gameDisplay.blit(text, (0,0))

def things(x,y, thing_width, thing_height,thing_startx,thing_starty):
    gameDisplay.blit(thingImg, (x,y))



def ship(x,y):
    gameDisplay.blit(shipImg,(x,y))
def text_objects(text, font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)

    game_loop()



def crash():
    message_display("You Crashed!")


def button(msg,x,y,w,h,ic,ac,action=None):
     mouse = pygame.mouse.get_pos()
     click = pygame.mouse.get_pressed()

     if x + w>mouse[0]>x and y+h > mouse[1] >y:
        pygame.draw.rect(gameDisplay,ac,(x,y,w,h))
        if click[0] == 1 and action != None:
            if action == game_loop:
                game_loop()
            elif action == quitgame:
                pygame.quit()
                quit()
     else:
         pygame.draw.rect(gameDisplay,ic,(x,y,w,h))

     smallText = pygame.font.Font('freesansbold.ttf',20)
     textSurf, textRect = text_objects(msg,smallText)
     textRect.center = ((x+(w/2)),(y+(h/2)))
     gameDisplay.blit(textSurf, textRect)





def game_loop():
    global pause
    x = (300)
    y = (410)

    x_change=0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 5
    thing_width = 100
    thing_height = 100
    dodged = 0

    gameExit = False

    while not gameExit:

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -20
                if event.key == pygame.K_RIGHT:
                    x_change = 20
                if event.key == pygame.K_p:
                    pause = True
                    paused()



            if event.type == pygame.KEYUP:
                if event .key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0



        x+= x_change

        gameDisplay.blit(bg, (0,0))  
        things(thing_startx,thing_starty,thing_width,thing_height,x,y)
        thing_starty += thing_speed
        thing_height=100
        ship(x,y)
        things_dodged(dodged)

        if x > display_width - ship_width or x<0:
            crash()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0, display_width)
            dodged += 1
            thing_speed+=2

        if y < thing_starty+thing_height:
            cow = 5

            if x > thing_startx and x < thing_startx + thing_width or x+ship_width > thing_startx and x+ship_width< thing_startx +thing_width:

                crash()



        pygame.display.update()
        clock.tick(60)
game_intro()
game_loop()
pygame.quit()
quit()

您似乎没有在pause()函数中取消pause()

你应该添加这样的东西:

def paused():
    while pause:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.K_p:
                unpause()

在我的代码中,我将取消暂停按钮功能设置为game_loop()而不是game_loop() unpause() 因此每次我去取消停止时,我都会重新启动游戏循环! 这是一个示例,您必须注意代码的每个部分,而不仅仅是出错的函数。

暂无
暂无

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

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