I am just starting off with pygame and python so no judgement (thanks:)) but I have been trying to use colliderect()
to check for collision of two rect
(snake, food; I think). I keep getting this error. AttributeError: 'Food' object has no attribute 'colliderect' What am I doing wrong?
# main function
def main():
clock = pygame.time.Clock()
FPS = 17
run = True
snake = Snake(random.randint(0, 400), random.randint(0, 400), 10, 10)
food = Food(random.randint(0, 400), random.randint(0, 400), 10, 10)
snakeVelocity_X = 0
snakeVelocity_Y = 0
lost = False
# snake object
# Rect object: Rect(x, y, width, height)
class Snake():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self, gameScreen):
pygame.draw.rect(gameScreen, colors.BLACK, (self.x, self.y, self.width, self.height))
# def get_head_position(self):
# return self.x, self.y, self.width, self.height
# food object
class Food():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.collide = False
def draw(self, gameScreen):
pygame.draw.rect(gameScreen, colors.YELLOW, (self.x, self.y, self.width, self.height))
# check for collision
if food.colliderect(snake):
print("collision")
Above is the code I think is sufficient to get my point across.
Ps. I am new to stackoverflow too any do/don'ts.