如何让一个函数中的变量交叉到另一个函数

How do I get a variable in one function to cross over to another function

def StartScreen():

    import pygame

    pygame.init()

    white = 230,230,230
    blue = 0,0,200
    water = 0,255,255
    red = 255,0,0

    WaterLevel = 400
    Depth = 150

    Move = True

    Location = 825

    TextTop = 350

    rise = True
    TextSink = False

    score = 0

    CLOCK = pygame.time.Clock()
    FPS = 60

    gameDisplay = pygame.display.set_mode((272,552))
    pygame.display.set_caption('Boat Game')

    BoatFloat = pygame.image.load("BoatFloatCut.png").convert_alpha()
    BoatFloat = pygame.transform.scale(BoatFloat, (220,50))

    boat = pygame.image.load("BoatLogoCut.png").convert_alpha()
    boat = pygame.transform.scale(boat, (140,100))

    stop = False

    while not stop:

###### Screen Setup ##############################################

        gameDisplay.fill(white)

###### Rising Water ##############################################

        pygame.draw.rect(gameDisplay,water,(0,WaterLevel,272,Depth))

###### Button ####################################################

        pygame.draw.rect(gameDisplay,water,(55,Location-10,160,120),2)

###### Score #####################################################

        ScoreFont = pygame.font.SysFont("monospace", 25)

        ScoreText = ScoreFont.render(str(score), 5, (red))
        gameDisplay.blit(ScoreText, (20, 30))

###### Text ######################################################

        gameDisplay.blit(BoatFloat, (35,TextTop))

###### Movement ##################################################
        if rise:
            TextTop -= 5

        WaterLevel -= 5
        Depth += 5

        if TextTop == 0:
            rise = False

        if rise == False:
            TextSink = True

        if TextSink == True:
            TextTop += 2            


###### Float Down ################################################

        gameDisplay.blit(boat,(60,Location))

        if Move: 
            Location -= 5

        if Location == 275:
            Move = False

###### Start Check 3##############################################

        for event in pygame.event.get():

            if event.type == pygame.MOUSEBUTTONDOWN:
                is_inside = pygame.Rect(55,Location-10,160,120).collidepoint(pygame.mouse.get_pos())

                if is_inside == 1:
                    MainGame()

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        CLOCK.tick(FPS)

        pygame.display.update()


def MainGame():

    import pygame
    import random
    import time

    pygame.init()

    blue = 0,255,255
    red = 255,0,0
    white = 255,255,255

    Location = 136
    Level = 382

    gate = 100

    gate2 = 100

    space1 = -30
    space1 = -30

    space2 = space1 - 200
    space2 = space1 -200

    WaterLevel = 402

    score = 0

    space1speed = 1
    space2speed = 1

    Left_Rect = pygame.Rect(0,402,185,150)
    Right_Rect = pygame.Rect(137,402,185,150)

    CLOCK = pygame.time.Clock()
    FPS = 80

    gate = random.randrange(20,222)
    gate2 = random.randrange(20,222)

    gameDisplay = pygame.display.set_mode((272,552))
    pygame.display.set_caption('Boat Game')

    boat = pygame.image.load("FinalBoat.png").convert_alpha()
    boat = pygame.transform.scale(boat, (40,25))

    stop = False
    sink = False
    gameOver = False

    is_Left = False
    is_Right = False

    while not stop:

####### Background ###############################################

        gameDisplay.fill(white)

###### Score #####################################################

        ScoreFont = pygame.font.SysFont("monospace", 25)

        ScoreText = ScoreFont.render(str(score), 5, (blue))
        gameDisplay.blit(ScoreText, (20, 30))

        score += 1

####### Barriers One  ############################################

        pygame.draw.line(gameDisplay,white,(gate,space1),(gate + 60,space1),2)

        pygame.draw.rect(gameDisplay,red,(0,space1,gate,25))

        pygame.draw.rect(gameDisplay,red,(gate + 60,space1,272 - gate + 60,25))

        space1 += space1speed

        if space1 == 402:
            space1 = -30
            gate = random.randrange(20,222)

###### Barriers two ##############################################

        pygame.draw.line(gameDisplay,white,(gate2,space2),(gate2 + 60,space2),2)

        pygame.draw.rect(gameDisplay,red,(0,space2,gate2,25))

        pygame.draw.rect(gameDisplay,red,(gate2 + 60,space2,272 - gate2 + 60,25))

        space2 += space2speed

        if space2 == 402:
            space2 = space1 - 200
            gate2 = random.randrange(20,222)

####### Controles ################################################

        pygame.draw.rect(gameDisplay, red, Left_Rect)

        pygame.draw.rect(gameDisplay, red, Right_Rect)

####### Boat #####################################################

        gameDisplay.blit(boat, (Location, Level))
        ####### Barrier 1 Effects ################################

        if space1 == Level - 25 and gate >= Location:
            sink = True

        if space1 == Level - 25 and gate + 50 <= Location + 25:
            sink = True

        if sink == True:
            Level += 1

        if Level == 402:
            stop = True

        ####### Barrier 2 Effects ################################
        if space2 == Level - 25 and gate2 >= Location:
            sink = True

        if space2 == Level - 25 and gate2 + 50 <= Location + 25:
            sink = True

        if sink == True:
            Level += 1

        if Level == 402:
            stop = True


####### Water #####################################################

        pygame.draw.rect(gameDisplay,blue,(0,WaterLevel,272,150))

####### Movement ##################################################


        for event in pygame.event.get():

            if event.type == pygame.MOUSEBUTTONDOWN:
                is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos())
                is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos())

            if event.type == pygame.MOUSEBUTTONUP:
                is_Left = False
                is_Right = False

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        if is_Left:
            Location -= 5

        elif is_Right:
            Location += 5

        CLOCK.tick(FPS)

        pygame.display.update()

StartScreen()

'score'是我要交叉的变量。 score + 1 每秒发生 80 次,而 MainGame() 为 运行。在游戏已经玩完之后,我想将分数 blit 到 StartScreen()。基本上我想在开场画面上看到球员得分。

您必须在任何方法定义之外对变量 score 进行初始化。要访问变量并对其进行编辑,您可以使用关键字 global。这告诉 python 您要访问的变量不在函数的局部范围内。阅读此处了解有关正确使用全局变量的更多信息。 Using global variables in a function other than the one that created them

这是一个使用您的代码的示例,

import pygame
import random
import time
score = 0
def StartScreen():
    global score

    pygame.init()

    white = 230,230,230
    blue = 0,0,200
    water = 0,255,255
    red = 255,0,0

    WaterLevel = 400
    Depth = 150

    Move = True

    Location = 825

    TextTop = 350

    rise = True
    TextSink = False

    CLOCK = pygame.time.Clock()
    FPS = 60

    gameDisplay = pygame.display.set_mode((272,552))
    pygame.display.set_caption('Boat Game')

    BoatFloat = pygame.image.load("BoatFloatCut.png").convert_alpha()
    BoatFloat = pygame.transform.scale(BoatFloat, (220,50))

    boat = pygame.image.load("BoatLogoCut.png").convert_alpha()
    boat = pygame.transform.scale(boat, (140,100))

    stop = False

    while not stop:

###### Screen Setup ##############################################

        gameDisplay.fill(white)

###### Rising Water ##############################################

        pygame.draw.rect(gameDisplay,water,(0,WaterLevel,272,Depth))

###### Button ####################################################

        pygame.draw.rect(gameDisplay,water,(55,Location-10,160,120),2)

###### Score #####################################################

        ScoreFont = pygame.font.SysFont("monospace", 25)

        ScoreText = ScoreFont.render(str(score), 5, (red))
        gameDisplay.blit(ScoreText, (20, 30))

###### Text ######################################################

        gameDisplay.blit(BoatFloat, (35,TextTop))

###### Movement ##################################################
        if rise:
            TextTop -= 5

        WaterLevel -= 5
        Depth += 5

        if TextTop == 0:
            rise = False

        if rise == False:
            TextSink = True

        if TextSink == True:
            TextTop += 2            


###### Float Down ################################################

        gameDisplay.blit(boat,(60,Location))

        if Move: 
            Location -= 5

        if Location == 275:
            Move = False

###### Start Check 3##############################################

        for event in pygame.event.get():

            if event.type == pygame.MOUSEBUTTONDOWN:
                is_inside = pygame.Rect(55,Location-10,160,120).collidepoint(pygame.mouse.get_pos())

                if is_inside == 1:
                    MainGame()

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        CLOCK.tick(FPS)

        pygame.display.update()


def MainGame():
    global score

    pygame.init()

    blue = 0,255,255
    red = 255,0,0
    white = 255,255,255

    Location = 136
    Level = 382

    gate = 100

    gate2 = 100

    space1 = -30
    space1 = -30

    space2 = space1 - 200
    space2 = space1 -200

    WaterLevel = 402

    space1speed = 1
    space2speed = 1

    Left_Rect = pygame.Rect(0,402,185,150)
    Right_Rect = pygame.Rect(137,402,185,150)

    CLOCK = pygame.time.Clock()
    FPS = 80

    gate = random.randrange(20,222)
    gate2 = random.randrange(20,222)

    gameDisplay = pygame.display.set_mode((272,552))
    pygame.display.set_caption('Boat Game')

    boat = pygame.image.load("FinalBoat.png").convert_alpha()
    boat = pygame.transform.scale(boat, (40,25))

    stop = False
    sink = False
    gameOver = False

    is_Left = False
    is_Right = False

    while not stop:

####### Background ###############################################

        gameDisplay.fill(white)

###### Score #####################################################

        ScoreFont = pygame.font.SysFont("monospace", 25)

        ScoreText = ScoreFont.render(str(score), 5, (blue))
        gameDisplay.blit(ScoreText, (20, 30))

        score += 1

####### Barriers One  ############################################

        pygame.draw.line(gameDisplay,white,(gate,space1),(gate + 60,space1),2)

        pygame.draw.rect(gameDisplay,red,(0,space1,gate,25))

        pygame.draw.rect(gameDisplay,red,(gate + 60,space1,272 - gate + 60,25))

        space1 += space1speed

        if space1 == 402:
            space1 = -30
            gate = random.randrange(20,222)

###### Barriers two ##############################################

        pygame.draw.line(gameDisplay,white,(gate2,space2),(gate2 + 60,space2),2)

        pygame.draw.rect(gameDisplay,red,(0,space2,gate2,25))

        pygame.draw.rect(gameDisplay,red,(gate2 + 60,space2,272 - gate2 + 60,25))

        space2 += space2speed

        if space2 == 402:
            space2 = space1 - 200
            gate2 = random.randrange(20,222)

####### Controles ################################################

        pygame.draw.rect(gameDisplay, red, Left_Rect)

        pygame.draw.rect(gameDisplay, red, Right_Rect)

####### Boat #####################################################

        gameDisplay.blit(boat, (Location, Level))
        ####### Barrier 1 Effects ################################

        if space1 == Level - 25 and gate >= Location:
            sink = True

        if space1 == Level - 25 and gate + 50 <= Location + 25:
            sink = True

        if sink == True:
            Level += 1

        if Level == 402:
            stop = True

        ####### Barrier 2 Effects ################################
        if space2 == Level - 25 and gate2 >= Location:
            sink = True

        if space2 == Level - 25 and gate2 + 50 <= Location + 25:
            sink = True

        if sink == True:
            Level += 1

        if Level == 402:
            stop = True


####### Water #####################################################

        pygame.draw.rect(gameDisplay,blue,(0,WaterLevel,272,150))

####### Movement ##################################################


        for event in pygame.event.get():

            if event.type == pygame.MOUSEBUTTONDOWN:
                is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos())
                is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos())

            if event.type == pygame.MOUSEBUTTONUP:
                is_Left = False
                is_Right = False

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        if is_Left:
            Location -= 5

        elif is_Right:
            Location += 5

        CLOCK.tick(FPS)

        pygame.display.update()

StartScreen()

我不确定我是否捕捉到所有得分实例,但如果我捕捉到了,这应该可行。分数在代码的顶部定义。在您的两种方法中,我都在顶部键入了全局分数。这让函数知道在它的范围之外寻找变量 score 。此外,导入应放在文件的顶部。无需始终如一地导入相同的模块。

您可以做的是将变量完全放在函数外部,而不是将其保留在函数内部。这样,您拥有的所有功能都可以识别它。但是,如果您想从另一个函数编辑 score,只需简单地将单词 global score 放在顶部,表示您想要编辑 score 的全局版本。

使用全局变量就可以了。另一种选择是将分数传递给所有分数更改函数和return(至少)分数。

def some_func (score, otherstuff):
    # do something with the inputs.
    score += 1
    return score, otherstuff.

另一种管理分数的方法是为您的游戏创建一个 class,前提是使 class 的所有分数更改函数成员合理。这可能是一个好的设计,也可能是一个糟糕的设计。如果采用这种方法,__init__ 函数应将 self.score 初始化为零。得分改变函数应该只更新 self-score.

的值