On my pygame I have a problem : the collisions are weird and they work half : on the border they work but on the midddle of the map no (sorry for the color) were that work (yes) and were that not work (no)
here is the portion of code for the deplacement :
if game.pressed.get(pygame.K_d):
if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + 1 + camX // 64] is not 'T':
game.player.move_right()
#print(game.player.rect.x)
if not screen_rect.contains(game.player.rect):
game.player.rect.x -= HEIGHT -60
camX += -HEIGHT
elif game.pressed.get(pygame.K_q):
if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
game.player.move_left()
#print(game.player.rect.x)
if not screen_rect.contains(game.player.rect):
game.player.rect.x += HEIGHT - 60
camX += HEIGHT
elif game.pressed.get(pygame.K_s):
print(game.player.rect.y)
if lvl.structure[game.player.rect.y // 64 + 1 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
game.player.move_down()
if not screen_rect.contains(game.player.rect):
game.player.rect.y -= WIDHT - 80
camY -= WIDHT
elif game.pressed.get(pygame.K_z):
if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
game.player.move_up()
#print(game.player.rect.y)
if not screen_rect.contains(game.player.rect):
game.player.rect.y += WIDHT - 80
camY += WIDHT
lvl.afficher(screen, 0, 0, camX, camY, game.player.rect.x, game.player.rect.y)
screen.blit(game.player.image, game.player.rect)
pygame.display.flip()
and for the level :
class Level(pygame.sprite.Sprite):
def __init__(self):
self.structure = 0
self.map = []
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.collide = False
#Camera.__init__(self, x, y)
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, 46, 64)
if sprite == 'G':
if screenRect.contains(sprite_rect):
fenetre.blit(grass, (x, y))
elif sprite == 'T':
if screenRect.contains(sprite_rect):
fenetre.blit(tree, (x, y))
else:
if screenRect.contains(sprite_rect):
fenetre.blit(no_texture, (x, y))
#print(x, y)
num_case += 1
num_ligne += 1
Thank you in advance !!!!!