I want to put a system of collide on my pygame's rpg and my code is crashing with "RecursionError: maximum recursion depth exceeded while calling a Python object". I tried many things but the error always appears.
My code:
import pygame
from pygame.locals import *
from player import *
class Camera:
def __init__(self, widht, height):
self.rect = pygame.Rect(0, 0, widht, height)
self.widht = widht
self.height = height
self.center = list(self.rect.center)
self.x = self.center[0]
self.y = self.center[1]
def apply(self, entity):
return entity.move(self.rect.topleft)
def update(self, target):
y = -target.rect.y + int(769 / 2)
x = -target.rect.x + int(1024 / 2)
self.rect = pygame.Rect(x, y, self.widht, self.height)
class Level(pygame.sprite.Sprite):
def __init__(self):
self.structure = 0
self.all_blocks = pygame.sprite.Group()
def generer(self):
with open("niveau.txt", "r") as fichier:
structure_niveau = []
for ligne in fichier:
ligne_niveau = []
for sprite in ligne:
if sprite != '\n':
ligne_niveau.append(sprite)
structure_niveau.append(ligne_niveau)
self.structure = structure_niveau
def afficher(self, fenetre, x, y, camX, camY, playerX, playerY):
tailleSprite = 64
self.all_blocks.empty()
#Camera.__init__(self, x, y)
cam = Camera(1024, 768)
grass = pygame.image.load("assets/bloc/grass.png").convert_alpha()
tree = pygame.image.load("assets/bloc/tree_grass.png").convert_alpha()
no_texture = pygame.image.load("assets/bloc/no_texture.png").convert_alpha()
num_ligne = 0
for ligne in self.structure:
num_case = 0
for sprite in ligne:
x = num_case * tailleSprite + camX
y = num_ligne * tailleSprite + camY
sprite_rect = pygame.Rect(x, y, 64, 64)
screenRect = pygame.Rect(x, y, 1088, 836)
aroundPlayer = pygame.Rect(playerX, playerY, playerX + 128, playerY + 128)
if sprite == 'G':
if screenRect.contains(sprite_rect):
fenetre.blit(grass, (x, y))
if aroundPlayer.contains(sprite_rect):
self.all_blocks.add(sprite)
elif sprite == 'T':
if screenRect.contains(sprite_rect):
fenetre.blit(tree, (x, y))
#self.all_blocks.add(sprite)
#print(self.x, self.y)
else:
if screenRect.contains(sprite_rect):
fenetre.blit(no_texture, (x, y))
#print(x, y)
num_case += 1
num_ligne += 1
and main.py:
import pygame
from game import Game
from level import *
pygame.init()
lvl = Level()
WIDHT = 768
HEIGHT = 1024
screen = pygame.display.set_mode((1024, 768))
screen_rect = pygame.Rect(0, 0, HEIGHT, WIDHT)
pygame.display.set_caption("RPG")
game = Game()
cam = Camera(1024, 768)
running = True
lvl.generer()
print(game.player.rect)
print(screen_rect)
clock = pygame.time.Clock()
lvl.afficher(screen, 0, 0, 0, 0, game.player.rect.x, game.player.rect.y)
camPos = 0
while running:
lvl.afficher(screen, 0, 0, camPos, 0, game.player.rect.x, game.player.rect.y)
cam.apply(game.player.rect)
cam.update(game.player)
#print(cam.rect.topleft)
if game.pressed.get(pygame.K_RIGHT):
game.player.move_right()
#print(game.player.rect.x)
if not screen_rect.contains(game.player.rect):
game.player.rect.x -= HEIGHT -60
camPos += -HEIGHT
elif game.pressed.get(pygame.K_LEFT):
game.player.move_left()
#print(game.player.rect.x)
if not screen_rect.contains(game.player.rect):
game.player.rect.x += HEIGHT - 60
camPos += HEIGHT
elif game.pressed.get(pygame.K_DOWN):
game.player.move_down()
#print(game.player.rect.y)
if not screen_rect.contains(game.player.rect):
game.player.rect.y -= WIDHT - 80
elif game.pressed.get(pygame.K_UP):
game.player.move_up()
#print(game.player.rect.y)
if not screen_rect.contains(game.player.rect):
game.player.rect.y += WIDHT - 80
#print(cam.rect.x, cam.rect.y)
#print(cam.rect)
screen.blit(game.player.image, game.player.rect)
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
elif event.type == pygame.KEYDOWN:
game.pressed[event.key] = True
elif event.type == pygame.KEYUP:
game.pressed[event.key] = False
Try one of the following option which will help you to get rid of the error
sys.setrecursionlimit(n)
- in python recursion is limited to 999 calls.