[Tutor] Type error when doing Pygame

Mats Wichmann mats at wichmann.us
Sat Sep 28 10:14:04 EDT 2019


On 9/28/19 5:53 AM, Cravan wrote:
> Dear Alan and Cameron,
> 	Thank you for your input. However, after removing the first "screen" argument, I receive another error:
> ###################
> Traceback (most recent call last):
>    File "final_project.py", line 134, in <module>
>      g.run()
>    File "final_project.py", line 37, in run
>      self.draw()
>    File "final_project.py", line 64, in draw
>      self.food_food(5, 5, self.food_bar)
>    File "final_project.py", line 50, in food_food
>      pygame.draw.rect(self, GREEN, fill_rect)
> TypeError: argument 1 must be pygame.Surface, not Game
> 
> I apologise if this seems like a really silly question, but I don’t really code much and am not really proficient in coding. May I know how to rectify this?

It's not silly, but you do figure out how Pygame's methods need to be 
called.  Clipped from their docs:

  pygame.draw.rect()
     draw a rectangle
     rect(surface, color, rect) -> Rect
     rect(surface, color, rect, width=0) -> Rect

     Draws a rectangle on the given surface.
     Parameters:	

         surface (Surface) -- surface to draw on


that makes sense... you need something to draw on.

you've passed it "self", which is a Game instance, and it doesn't know 
what to do with that.  You need to pass it a pygame.Surface instance, as 
the exception message says.  Presumably that would be something that has 
been previously set up, then stored as an attribute in "self" so that 
you can access it as needed.  That has happened in your initialization 
method, where you create a Surface by calling pygame.display.set_mode() 
and store the result in self.screen.   So almost certainly your call to 
draw_rect should be passing self.screen as the first aregument, rather 
than self.

Does that make sense?


More information about the Tutor mailing list