0

First, let me to preface this by saying I am a beginner with Python, only made a few programs, so this code probably looks very messy. I am trying to familiarize with pygame by making a small pacman game as a gift for a family member with my dogs as the player and ghosts. I am having trouble blitting the image onto the rect called by rocky.draw() in the main loop (my dogs name is rocky). I have tried multiple solutions. The first being removing the draw method under the Rocky class and using get.rect() instead to create the rect. However this just leads to no rect showing and just a giant blown up version of the image showing over most of the screen. So, I moved onto using rocky.draw() again and trying to blit the image onto the rect. But, I keep getting type error: invalid destination position for blit. I know I am most likely missing something silly or misunderstanding something. Any and all input is greatly appreciated. Thank you!

Here is the code:

    import pygame

    pygame.init()

    x = 50
    y = 50
    width = 40
    height = 60
    vel = 5

    pygame.display.set_caption("Rock-Man")

    clock = pygame.time.Clock()

    surface = pygame.display.set_mode((500, 500))

    color = (255, 0, 0)

    rockimg = pygame.image.load('rocky1.png')

    rockimg.convert()


    class Entity:

       def __init__(self, x, y):
            self.x = x
            self.y = y


    class Rocky(Entity):
        def __init__(self):
            Entity.__init__(self, x, y)
            self.x = 0
            self.y = 0
            self.tile_size = 30

        def draw(self):
            pygame.draw.rect(surface, color, (x, y, width, height))


    rocky = Rocky()

    run = True
    while run:
        pygame.time.delay(100)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            x -= vel
        if keys[pygame.K_RIGHT]:
            x += vel
        if keys[pygame.K_UP]:
            y -= vel
        if keys[pygame.K_DOWN]:
            y += vel

        surface.fill((0, 0, 0))
        rocky.draw()
        surface.blit(rockimg, rocky)

        pygame.display.flip()
        pygame.display.update()

    pygame.quit()
Mudalin
  • 23
  • 3

1 Answers1

2

You need to give surface.blit a destination on the screen. The arguments passed for the blit function are:

blit(source, dest, area=None, special_flags=0)

You are trying to give a type Rocky() as the argument dest (tuple destination).

I am unsure of your intent, but to draw rockimg you need to give it coordinates.

Here is an example for reference:

surface.blit(rockimg, (250, 250))
Nolan Dagum
  • 43
  • 1
  • 7