So I was making a code for collision and this error poped up:
Can someone help me or explain me what is wrong with my code?
Error:Traceback (most recent call last):
File "X", line 111, in <module>
plyrrect, collisions = move(plyrrect, player_movement, tile_rects)
File "X", line 52, in move
hit_list = collision_test(rect, tiles)
File "X", line 45, in collision_test
if rect.colliederect(tile):
AttributeError: 'pygame.Rect' object has no attribute 'colliederect'
I tried to fix it, my first thought was that I made a typo, I've found some errors like writing "tile" instead of "tiles" (lines 52 and 61), but I could'nt find anything more. Can someone help me or explain me what is wrong with my code?
import pygame import sys from pygame import * pygame.init() # Fps clock = pygame.time.Clock() # Title pygame.display.set_caption('HandsomeBeta') # Screen window = (1280, 720) screen = pygame.display.set_mode(window) display = pygame.Surface((1280, 720)) # player image player = pygame.image.load('assets4plyr/Handsome1.png') handsomeman = pygame.transform.scale(player, (55, 55)) handsomeman.set_colorkey((255, 255, 255)) grassimg = pygame.image.load('assets4game/handsomegrass.png') tile_size = grassimg.get_height() dirtimg = pygame.image.load('assets4game/handsomedirt.png') game_map = [['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','2','2','2','2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'], ['2','2','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','2','2','2','2','2','2','2','2','2','2','2','2','2'], ['1','1','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','1','1','1','1','1','1','1','1','1','1','1','1','1'], ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'], ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'], ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'], ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'], ['1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1']] def collision_test(rect, tiles): hit_list = [] for tile in tiles: if rect.colliederect(tile): hit_list.append(tile) return hit_list def move(rect, movement, tiles): collision_types = {'top': False, 'bottom': False, 'right': False, 'left': False} rect.x += movement[0] hit_list = collision_test(rect, tiles) for tile in hit_list: if movement[0] > 0: rect.right = tile.left collision_types['right'] = True elif movement[0] < 0: rect.left = tile.right collision_types['left'] = True rect.y += movement[1] hit_list = collision_test(rect, tiles) for tile in hit_list: if movement[1] > 0: rect.bottom = tile.top collision_types['bottom'] = True elif movement[1] < 0: rect.top = tile.bottom collision_types['top'] = True return rect, collision_types moving_right = False moving_left = False playerpos = [640, 200] playerY_momentum = 0 plyrrect = pygame.Rect(50, 50, handsomeman.get_width(), handsomeman.get_height()) test_rect = pygame.Rect(640, 360, 100, 50) # GameLoop while True: display.fill((146, 244, 255)) tile_rects = [] y = 0 for row in game_map: x = 0 for tile in row: if tile == '1': display.blit(dirtimg, (x * tile_size, y * tile_size)) if tile == '2': display.blit(grassimg, (x * tile_size, y * tile_size)) if tile == '0': tile_rects.append(pygame.Rect(x * tile_size, y * tile_size, tile_size, tile_size)) x += 1 y += 1 player_movement = [0, 0] if moving_right: player_movement[0] += 2 if moving_left: player_movement[0] -= 2 player_movement[1] += playerY_momentum playerY_momentum += 0.2 if playerY_momentum > 3: playerY_momentum = 3 plyrrect, collisions = move(plyrrect, player_movement, tile_rects) display.blit(handsomeman, (plyrrect.x, plyrrect.y)) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_RIGHT: moving_right = True if event.key == K_LEFT: moving_left = True if event.type == KEYUP: if event.key == K_RIGHT: moving_right = False if event.key == K_LEFT: moving_left = False surf = pygame.transform.scale(display, window) screen.blit(surf, (0, 0)) pygame.display.update() clock.tick(60)