2017-02-05 28 views
0

私はゲームに取り組んでいます。このゲームでは、キャラクター(ボール)のawが34の場合に勝ちます。それアウト、それは私にエラーメッセージを与えていた。TypeError: 'Text'オブジェクトは呼び出し可能ではありません - Python

Traceback (most recent call last): 
    File "main.py", line 260, in <module> 
    main() 
    File "main.py", line 165, in main 
    gameLoop() 
    File "main.py", line 192, in gameLoop 
    player.end() 
    File "main.py", line 133, in end 
    win() 
TypeError: 'Text' object is not callable 

を、私はそれが勝利画面を作る機能

#WINの下にここに私のコードですされています

# IMPORTS 
import pygame, random 

# GLOBALS 
global screen, displayW, displayH 
global clock, FPS, font 
global background, clouds, lose, win 
global end, food, player, score 

# SETGLOBALVALUES 
def setGlobalValues(): 
    global screen, displayW, displayH 
    global clock, FPS, font 
    global background, clouds, lose, win 
    global end, food, player, score 

    displayW = 800 
    displayH = 600 
    screen = pygame.display.set_mode((displayW, displayH)) 

    clock = pygame.time.Clock() 
    FPS = 60 
    font = pygame.font.SysFont("robotomono", 20) 

    end = False 
    food = Burger() 
    player = Player() 
    score = Text() 
    score.x = 20 
    score.y = 100 
    score.text = "Size: {}".format(str(player.w)) 
    score.color = (0, 0, 0) 

    background = Background() 
    background.img = pygame.image.load("assets/img/background.jpeg") 

    clouds = Background() 
    clouds.img = pygame.image.load("assets/img/clouds.png") 

    lose = Text() 
    lose.x = displayW/2 
    lose.y = displayH/2 
    lose.text = "YOU LOSE!" 
    lose.color = (200, 0, 0) 

    win = Text() 
    win.x = displayW/2 
    win.y = displayH/2 
    win.text = "YOU WIN!" 
    win.color = (0, 200, 0) 

# FOOD 
class Burger(): 
    def __init__(self, img="", x=0, h=0, w=0, velY=0, color=()): 
     self.img = pygame.image.load("assets/img/burger.png") 
     self.w = 30 
     self.h = 30 
     self.x = random.randrange(0, displayW - self.w) 
     self.y = -100 
     self.velY = 3 
     self.color = (255, 0, 0) 

    def draw(self): 
     screen.blit(self.img, (self.x, self.y)) 

    def animate(self): 
     self.y += self.velY 

     if self.velY >= 16: 
      self.velY = 16 

    def collision(self): 
     # collision with player 
     if self.y >= player.y - player.h and self.y <= player.y + player.h and self.x >= player.x - player.w and self.x <= player.x + player.w: 
      player.w += player.change 
      player.h += player.change 

      score.text = "Size: {}".format(str(player.w)) 
      score.draw() 

      self.reset() 
     else: 
      score.text = "Size: {}".format(str(player.w)) 
      score.draw() 

     # collision with bottom wall 
     if self.y >= displayW: 
      if player.w == 20: 
       player.w = 20 
      else: 
       player.w -= 2 
       player.h -= 2 

      self.reset() 

    def reset(self): 
     self.y = -100 
     self.x = random.randrange(0, displayW - self.w) 
     self.velY += 1 
     screen.blit(self.img, (self.x, self.y)) 

# PLAYER 
class Player(): 
    def __init__(self, x=0, y=0, velX=0, velY=0, w=0, h=0, change=0, color=()): 
     self.w = 30 
     self.h = 30 
     self.x = displayW/2 - self.w/2 
     self.y = displayH - 120 
     self.velX = 0 
     self.velY = 0 
     self.change = 2 
     self.color = (0, 0, 0) 

    def draw(self): 
     pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.w, self.h)) 

    def animate(self): 
     self.x += self.velX 
     self.y += self.velY 

    def collision(self): 
     # collision to walls 
     if self.x <= 0: 
      self.velX = 0 
     elif self.x + self.h >= displayW: 
      self.velX = 0 

    def end(self): 
     if self.w <= 20: 
      lose() 

     if self.w >= 34: 
      win() 

# SCORE 
class Text(): 
    def __init__(self, x=0, y=0, text="", color=()): 
     self.x = 0 
     self.y = 0 
     self.text = "" 
     self.color =() 

    def draw(self): 
     render = font.render(self.text, True, self.color) 

     screen.blit(render, (self.x, self.y)) 
     pygame.display.update() 

# BACKGROUND 
class Background(): 
    def __init__(self, x=0, y=0, img=""): 
     self.x = 0 
     self.y = 0 
     self.img = "" 

    def draw(self): 
     screen.blit(self.img, (self.x, self.y)); 

# MAIN 
def main(): 
    pygame.init() 

    setGlobalValues() 
    setup() 
    gameLoop() 
    quitGame() 

# GAMELOOP 
def gameLoop(): 
    global end, player 

    while not end: 
     for event in pygame.event.get(): 
      # ONCLICK QUIT 
      if event.type == pygame.QUIT: 
       end = True 

      # KEYDOWN 
      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_LEFT: 
        player.velX -= 10 
       if event.key == pygame.K_RIGHT: 
        player.velX += 10 

      # KEYUP 
      if event.type == pygame.KEYUP: 
       if event.key == pygame.K_LEFT: 
        player.velX = 0 
       if event.key == pygame.K_RIGHT: 
        player.velX = 0 

     player.end() 
     draw() 
     animate() 
     collision() 
     setFPS() 


# DRAW 
def draw(): 
    global screen 

    # fill background 
    screen.fill((255, 255, 255)) 

    background.draw() 
    food.draw() 
    clouds.draw() 
    player.draw() 
    score.draw() 

    # update 
    pygame.display.update() 

# ANIMATE 
def animate(): 
    global food, player 

    food.animate() 
    player.animate() 

# COLLISION 
def collision(): 
    global player, food 

    player.collision() 
    food.collision() 

# SETFPS 
def setFPS(): 
    global clock, FPS 

    clock.tick(FPS) 

# SETUP 
def setup(): 
    pygame.display.set_caption("Burger Muncher") 

# WIN 
def win(): 
    global screen 

    screen.fill(255, 255, 255) 
    win.draw() 

# WIN 
def lose(): 
    global screen 

    screen.fill(255, 255, 255) 
    lose.draw() 

# QUIT GAME 
def quitGame(): 
    pygame.quit() 
    quit() 

# CALL MAIN 
if __name__ == "__main__": 
    main() 
+0

'Text'クラスは同じファイルにあります –

+1

あなたは' win'という関数と 'win'という変数を持っています。それらの名前を変更して – user2896976

+0

とすると、 'config'クラスや辞書を作成して、' global' – furas

答えて

1

winは、テキストでありますウィジェット(これも方法ですが、エラー時にはテキストウィジェットです)。このステートメントでやっているウィジェットを呼び出すことはできません:win()。グローバル変数と関数に同じ名前を使用しないでください

関連する問題