2015-04-07 140 views
0
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'是我想要跨越的變量。 MainGame()正在運行時,得分+每秒發生80次。在比賽結束後我想將比分送到StartScreen()。基本上我希望看​​到球員在開幕式上得分。如何在一個函數中獲取變量以跨越另一個函數

+1

您是否嘗試過在函數scoop/slash之外創建varibme使其成爲全局 – reticentroot

+0

編寫一個函數,它返回您需要的變量並將其傳遞給您希望它轉到的函數? 「f o g」它被稱爲構圖。我不認爲你可以在函數內部創建一個全局變量。這將超出範圍。 – lciamp

回答

2

您必須在任何方法定義之外初始化變量分數。要訪問變量並編輯它,請使用關鍵字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() 

我不知道如果我抓住得分的所有實例,但如果我這樣做應該工作。分數定義在代碼的頂部。在你的兩種方法中,我在頂部鍵入全局分數。這使得函數知道查找範圍之外的變量分數。另外,導入應該放在文件的頂部。沒有必要持續導入相同的模塊。

+0

謝謝,問題解決了 –

0

你可以做的是將變量完全放在函數之外,而不是將它放在函數內部。這樣,您擁有的所有功能都可以識別它。但是,如果您想從其他功能編輯score,只需簡單地將文字global score置頂,表示您想要編輯score的全球版本。

1

使用全局變量可以工作。另一種選擇是將分數傳遞給所有改變分數的函數,並返回(至少)分數。

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

另一種方式來管理的比分是創建一個類爲您的遊戲,如果是合理的,使類的所有成績,改變功能的部件。這可能是一個好設計或一個壞設計。如果採取這種方法,__init__函數應該初始化爲self.score爲零。分數改變功能應該只更新self-score的值。

相關問題