Python Forum
I was making code for collision for my character
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I was making code for collision for my character
#1
Star 
So I was making a code for collision and this error poped up:
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)
Reply
#2
You have a typo: colliderect not colliederect
izmamonke likes this post
Reply
#3
(Aug-06-2021, 03:04 PM)deanhystad Wrote: You have a typo: colliderect not colliederect
omg i didnt notice ty so much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] unexpected character after line continuation character paul18fr 4 3,393 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  Making a code.py file to function, does not run hobbyist 6 2,889 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  Making new lines of code AvioxyYT 1 1,801 Jan-22-2021, 07:02 PM
Last Post: buran
  SOLVED - Collision detection - TURTLE OuateDePhoque 9 11,340 Nov-18-2020, 06:29 PM
Last Post: OuateDePhoque
  bouncing ball with variable collision points (in time) Zhaleh 1 2,364 Jul-24-2020, 02:54 PM
Last Post: Marbelous
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,166 Jul-13-2020, 07:05 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,735 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Player object wont recognize collision with other objects. Jan_97 3 2,713 Dec-22-2019, 04:08 PM
Last Post: joe_momma
  making the code easier, list comprehension go127a 2 2,059 May-26-2019, 06:19 PM
Last Post: Gribouillis
  Replace changing string including uppercase character with lowercase character silfer 11 6,192 Mar-25-2019, 12:54 PM
Last Post: silfer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020