hmood
hmood

Reputation: 603

TypeError: text must be unicode or bytes & how to make a working bullet in pygame

I am making Space Invaders. Here is my code so far:

# importing packages
import pygame
# Initializing Pygame
pygame.init()

# Setting a display caption
pygame.display.set_caption("Space Invaders")

spaceship = pygame.image.load("spaceship2.png")
blue_enemy = pygame.image.load("blue_enemy.png")
green_enemy = pygame.image.load("green_enemy.png")
orange_enemy = pygame.image.load("orange_enemy.png")
pink_enemy = pygame.image.load("pink_enemy.png")
yellow_enemy = pygame.image.load("yellow_enemy.png")

# Creating a font
pygame.font.init()
font = pygame.font.SysFont("consolas", 30)

# Setting a display width and height and then creating it
display_width = 700
display_height = 500
display_size = [display_width, display_height]
game_display = pygame.display.set_mode(display_size)


# Creating a way to add text to the screen
def message(sentence, color, x, y, font_type, display):
    sentence = font_type.render(sentence, True, color)
    display.blit(sentence, [x, y])


def main():
    # Spaceship coordinates
    spaceship_x = 300
    spaceship_y = 375
    spaceship_x_change = 0
    # Enemy health
    blue_enemy_health = 5
    green_enemy_health = 5
    orange_enemy_health = 5
    pink_enemy_health = 5
    yellow_enemy_health = 5

    # Initializing pygame
    pygame.init()

    # Creating colors
    black = (0, 0, 0)
    red = (0, 0, 0)

    done = False

    # Creating a loop to keep the program running
    while not done:

        # --- Event Processing and controls
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    spaceship_x_change = 7
                elif event.key == pygame.K_LEFT:
                    spaceship_x_change = -7
                elif event.key == pygame.K_r:
                    red = (255, 0, 0)
            elif event.type == pygame.KEYUP:
                spaceship_x_change = 0
                red = (0, 0, 0)

        spaceship_x += spaceship_x_change

        # Preventing the spaceship from going off the screen
        if spaceship_x > display_width - 140:
            spaceship_x -= 10
        if spaceship_x < 1:
            spaceship_x += 10

        # Setting Display color
        game_display.fill(black)

        # Creating a spaceship, bullets, and enemies
        pygame.draw.rect(game_display, red, [spaceship_x + 69, 70, 4, 310])
        game_display.blit(spaceship, (spaceship_x, spaceship_y))
        message(blue_enemy_health, (255, 255, 255), 350, 250, font, game_display)
        game_display.blit(blue_enemy, (20, 25))
        game_display.blit(green_enemy, (160, 25))
        game_display.blit(orange_enemy, (300, 25))
        game_display.blit(pink_enemy, (440, 25))
        game_display.blit(yellow_enemy, (580, 25))

        # Updating Screen so changes take places
        pygame.display.update()

        # Setting FPS
        FPS = pygame.time.Clock()
        FPS.tick(60)


# Executing the function
if __name__ == "__main__":
    main()

However, when I want to display my enemy health, it shows me this error. I don't know what to do about it, please help out. Also i am using an awkward "laser" that the spaceship shoots. I chose this as a shortcut around making bullets as I am new to pygame. If you can, please help with that too.

The error:

Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/ProgrammingLearn/SpaceInvaders.py", line 103, in <module>
    main()
  File "C:/Users/user/PycharmProjects/ProgrammingLearn/SpaceInvaders.py", line 86, in main
    message(blue_enemy_health, (255, 255, 255), 350, 250, font, game_display)
  File "C:/Users/user/PycharmProjects/ProgrammingLearn/SpaceInvaders.py", line 30, in message
    sentence = font_type.render(sentence, True, color)
TypeError: text must be a unicode or bytes

Upvotes: 0

Views: 464

Answers (1)

mazore
mazore

Reputation: 1024

The first argument of font_type.render needs to be a string, and you are passing an int into the sentence parameter (you are passing blue_enemy_health integer). You need to convert it into a string before rendering it with the font by str(blue_enemy_health) when you pass it into the message function.

Upvotes: 1

Related Questions