简体   繁体   中英

TypeError: 'Text' object is not callable - Python

I'm working on a game and In this game the player wins if its character (a ball) has aw of 34. I made a win screen, but when I tested it out, it was giving me an error message:

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

I function that makes a win screen is under #WIN

Here's my code:

# 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()

win is a text widget (it's also a method, but at the time of the error it's a text widget). You can't call a widget, which you are doing in this statement: win() . Don't use the same name for a global variable and a function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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