2015-11-16 63 views
0

我試圖在我的pong遊戲結束時添加一個def main以使其更易於閱讀,但我遇到了試圖做到這一點的問題。當我添加def main時,我只是得到一個黑屏,但沒有它,我得到整個遊戲。將def main添加到我的pong遊戲代碼末尾

import pygame 


    SCR_WID, SCR_HEI = 640, 480 
    class Player(): 
    def __init__(self): 
      self.x, self.y = 16, SCR_HEI/2 
      self.speed = 3 
      self.padWid, self.padHei = 8, 64 
      self.score = 0 
      self.scoreFont = pygame.font.Font("imagine_font.ttf", 64) 

    def scoring(self): 
      scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255)) 
      screen.blit(scoreBlit, (32, 16)) 
      if self.score == 10: 
        print ("player 1 wins!") 
        exit() 

    def movement(self): 
      keys = pygame.key.get_pressed() 
      if keys[pygame.K_w]: 
        self.y -= self.speed 
      elif keys[pygame.K_s]: 
        self.y += self.speed 

      if self.y <= 0: 
        self.y = 0 
      elif self.y >= SCR_HEI-64: 
        self.y = SCR_HEI-64 

    def draw(self): 
      pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei)) 

    class Enemy(Player): 
    def __init__(self): 
      self.x, self.y = SCR_WID-16, SCR_HEI/2 
      self.speed = 3 
      self.padWid, self.padHei = 8, 64 
      self.score = 0 
      self.scoreFont = pygame.font.Font("imagine_font.ttf", 64) 

    def scoring(self): 
      scoreBlit = self.scoreFont.render(str(self.score), 1, (255, 255, 255)) 
      screen.blit(scoreBlit, (SCR_HEI+92, 16)) 
      if self.score == 10: 
        print ("Player 2 wins!") 
        exit() 

    def movement(self): 
      keys = pygame.key.get_pressed() 
      if keys[pygame.K_UP]: 
        self.y -= self.speed 
      elif keys[pygame.K_DOWN]: 
        self.y += self.speed 

      if self.y <= 0: 
        self.y = 0 
      elif self.y >= SCR_HEI-64: 
        self.y = SCR_HEI-64 

    def draw(self): 
      pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei)) 

    class Ball(): 
    def __init__(self): 
      self.x, self.y = SCR_WID/2, SCR_HEI/2 
      self.speed_x = -3 
      self.speed_y = 3 
      self.size = 8 

    def movement(self): 
      self.x += self.speed_x 
      self.y += self.speed_y 

      #wall col 
      if self.y <= 0: 
        self.speed_y *= -1 
      elif self.y >= SCR_HEI-self.size: 
        self.speed_y *= -1 

      if self.x <= 0: 
        self.__init__() 
        enemy.score += 1 
      elif self.x >= SCR_WID-self.size: 
        self.__init__() 
        self.speed_x = 3 
        player.score += 1 
      ##wall col 
      #paddle col 
      #player 
      for n in range(-self.size, player.padHei): 
        if self.y == player.y + n: 
          if self.x <= player.x + player.padWid: 
            self.speed_x *= -1 
            break 
        n += 1 
      #enemy 
      for n in range(-self.size, enemy.padHei): 
        if self.y == enemy.y + n: 
          if self.x >= enemy.x - enemy.padWid: 
            self.speed_x *= -1 
            break 
        n += 1 
      ##paddle col 

    def draw(self): 
      pygame.draw.rect(screen, (255, 255, 255), (self.x, self.y, 8, 8)) 

    SCR_WID, SCR_HEI = 640, 480 
    screen = pygame.display.set_mode((SCR_WID, SCR_HEI)) 
    pygame.display.set_caption("Pong") 
    pygame.font.init() 
    clock = pygame.time.Clock() 
    FPS = 60 



    def main(): 
     ball = Ball() 
     player = Player() 
     enemy = Enemy() 

     while True: 
     #process 
     for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
          print ("Game exited by user") 
          exit() 



    ##process 
    #logic 
    ball.movement() 
    player.movement() 
    enemy.movement() 
    ##logic 
    #draw 
    screen.fill((0, 0, 0)) 
    ball.draw() 
    player.draw() 
    player.scoring() 
    enemy.draw() 
    enemy.scoring() 
    ##draw 
    #_______ 
    pygame.display.flip() 
    clock.tick(FPS) 
    main() 

回答

2

上月底呼籲main()應該

if __name__ == "__main__": 
    main() 

所以,你可以運行你的代碼,並獲得函數運行。此外,你的縮進似乎有點關閉。你的代碼在空間方面的結構是否正確?

+1

if語句可能有點偏離。如果__name__ ==「__main__」在末尾的代碼中應該添加到哪裏? – ChampIsHere

+0

是的,在最後! – FBidu

0

前面已經回答了,你應該在腳本的末尾插入下面的語句:

if __name__ == "__main__": 
    main() 

這個答案會向您提供有關語句的含義更多信息。

What does if __name__ == "__main__": do?

總之,它會告訴它具有與功能主要做解釋(): 1)自動運行,如果作爲獨立的腳本文件已經被執行。 2)如果文件已從另一個腳本導入爲模塊,則不要運行。 在你的情況下,想要的行爲是頭號。