1

I am making a snake game in pygame. However, I ran into a TypeError problem inside my Snake class object. To be specific it threw an error inside the draw() (class) and blit() (non-class) functions, telling me that "an integer is required" (got type tuple).

import pygame
import random

pygame.init()

screen = pygame.display.set_mode((640, 480))

class Snake(object):
    def __init__(self, x, y, height, width, color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.vel = 3

    def draw(self): # The problem happened here
        pygame.draw.rect(screen, self.color, (self.x, self.y), (self.height, self.width))

    def auto_move(self):
        pass


def blit(): # The problem also happened here
    snake.draw()

snake = Snake(300, 300, 64, 64, (0, 255, 0))

run = True
while run:
    screen.fill((0, 0, 0))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    blit()
    pygame.display.update()

The game is far from finish as I quickly ran to said error. Thanks in advance.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
colappse
  • 71
  • 5
  • 3
    `pygame.draw.rect(screen, self.color, (self.x, self.y, self.height, self.width))`. Rect argument need to be a single tuple of `(x, y, w, h)` rather than `(x, y)` and `(w, h)` –  Feb 26 '21 at 13:25
  • 1
    `(self.height, self.width)` is what you are providing to the `radius` argument per the docs, however the docs say it only accepts an `int or float` – gold_cy Feb 26 '21 at 13:28
  • 2
    The error message should give you a line number. Please identify the affected line. You also want to create a [mre], that is: create a copy and remove everything that is not required to trigger the same error. – Thomas Weller Feb 26 '21 at 13:39
  • Sum up: `pygame.draw.rect(screen, self.color, (self.x, self.y), (self.height, self.width))` needs to be `pygame.draw.rect(screen, self.color, (self.x, self.y, self.height, self.width))` and `snack` is not yet defined. – Rabbid76 Feb 26 '21 at 14:28

1 Answers1

0

To use pygame.draw.rect, the documentation says that you must pass as arguments:

  • surface: in your case, surface is screen.

  • color: pygame.Color, int or tuple of length 3. In your case self.color.

  • rect: pygame.Rect()

The rect argument can be as following:

  1. pygame.Rect(left, top, width, height)
  2. pygame.Rect((left, top), (width, height))
  3. pygame.Rect(object)

So, you must type

    def draw(self, screen): # The problem no longer happen here
        rect = pygame.Rect(self.x, self.y, self.height, self.width)
        pygame.draw.rect(screen, self.color, rect)

Second error

def blit(): # The problem also happened here

snack.draw(screen) >>> ?????

Of course! Why are you typing this line ? There is no "snack" class...

-- Edit --

Your question has been edited to do a minimal reproducible example and the person who edited didn't remove this line - I'll do it

D_00
  • 1,440
  • 2
  • 13
  • 32