"argument 1 must be pygame.Surface, not pygame.Rect" How do I fix this, what is it saying?

Question:

Im trying to make a game similar to cookie clicker, but with pencils. Using pygame.

class pencilsDisplay():
  def __init__(self, x, y):
    self.x = x
    self.y = y
    self.height = 100
    self.length = 100

  def draw(self):
    font = pygame.font.Font('font/Gidole-Regular.ttf', 24)
    small_font = pygame.font.Font('font/Gidole-Regular.ttf', 24)

    PENCILS = font.render('{} Pencils'.format( int(user.pencils) ), True, WHITE)
    PENCILSPERSECOND = font.render('Per Second: {}'.format( int(user.pencils) ), True, WHITE)
    screen.blit(PENCILS.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))
    screen.blit(PENCILSPERSECOND.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))

pencil = MainPencil(100,100)
pencil_display = pencilsDisplay(100,0)

class Player:
  def __init__(self):
    self.pencils = 0
    self.pencilspersecond = 0

user = Player()

def draw():
  pencil.draw()
  pencil_display.draw()

running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
      mouse_pos = event.pos
      if pencil.collidepoint(mouse_pos):
        user.score += 1
        pencil.animation_state = 1

    
    if event.type == pygame.QUIT:
      running = False

  draw()

I have the code that loads the window in above this, but this is the area the error is referring to.
Nothing shows up in the window, and I get this.

  File "main.py", line 82, in <module>
    draw()
  File "main.py", line 67, in draw
    pencil_display.draw()
  File "main.py", line 52, in draw
    screen.blit(PENCILS.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))
TypeError: argument 1 must be pygame.Surface, not pygame.Rect
exit status 1

What have I done wrong?
Im fairly new to pygame, so any explanation I would appreciate.

Asked By: Jack Doherty

||

Answers:

The 1st argument of pygame.Surface.blit is the source Surface:

screen.blit(PENCILS.get_rect( center=( int(self.x + self.length/2),int(self.y + self.height/2) )))

screen.blit(PENCILS, PENCILS.get_rect(center = (int(self.x + self.length/2), int(self.y + self.height/2))))
Answered By: Rabbid76
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.