R
R
Rostislav2020-10-23 22:27:57
Python
Rostislav, 2020-10-23 22:27:57

Error: TypeError: argument 1 must be pygame.Surface, not function. How to fix?

I have searched many sites and have not found the answer.

Full error code:
Traceback (most recent call last):
File "main.py", line 52, in


win.blit (gpic, (gx, gy)) TypeError: argument 1 must be pygame.Surface, not function .py:

import pygame
from GrassLog import grass

DisplayX = 500
DisplayY = 250

pygame.init()
win = pygame.display.set_mode((DisplayX, DisplayY))

pygame.display.set_caption("Tetra")

x = 60
y = 50
widht = 50
height = 50
speed = 5
FPS = 65

person = pygame.image.load('Image\\pers.png')
gpic = grass.pic

gx = grass.X
gy = grass.Y


run = True

while run:
  pygame.time.delay(50)
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False
  
  keys = pygame.key.get_pressed()

  if keys[pygame.K_LEFT] and x > 1:
    x -= speed
  
  if keys[pygame.K_RIGHT] and x < (DisplayX - widht - 1):
    x += speed

  if keys[pygame.K_UP] and y > 1:
    y -= speed

  if keys[pygame.K_DOWN] and y < (DisplayY - height - 5):
    y += speed


  win.fill((0, 0, 0))

  win.blit(person, (x, y))
  win.blit(gpic, (gx, gy))
  
  pygame.display.update()



pygame.quit()


GrassLog.py code:
import pygame


class grass:

  def X(x):
    x = 0

  def Y(y):
    y = 195

  def pic(grasspic):
    grasspic = pygame.image.load('Imaga\\grass.png')

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2020-10-23
@Rostyslav23

Rostislav , it says that the gpic argument in the string win.blit(gpic, (gx, gy))
must be a specific object, not a function.
I guess the grass object's gpic method looks like a function from win.blit's point of view.
gpic = grass.pic
You create a link named gpic to a method of the grass class. (And, by the way, if we look at this method, what does it return? Probably None
, because there is no return in it)

class Grass:
  def __init__(self):
    self.X = 0
    self.Y = 195

  def pic(self):
    return pygame.image.load('Imaga\\grass.png')


gpic = Grass()

And then access its method
win.blit(gpic.pic(), (gpic.X, gpic.Y))
. In general, the class turns out to be rather empty and stupid, and I'm not sure if it's correct - but something like this should be, maybe more complicated (because the constants self.X and self.Y, hardcore inside the class constructor - Well, I don't know...)
Ps I must admit, I'm not a great master, maybe my older comrades will correct me.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question