Home > other >  TypeError: descriptor 'collidelist' for 'pygame.Rect' objects doesn't apply
TypeError: descriptor 'collidelist' for 'pygame.Rect' objects doesn't apply

Time:11-27

Trying to set up a system to earn score points by killing enemies. but i keep getting: TypeError: descriptor 'collidelist' for 'pygame.Rect' objects doesn't apply to a 'list' object. But it worked one line before.

This is the first program that ive trying to write on my own. im still very new to this so any help would be appreciated.

code:

import pygame
from random import randint
from sys import exit
pygame.init()

def star_movement(stars):
    if stars:
        for star_rect in stars:
            star_rect.x -= 5

            if star_rect.x >= 0: screen.blit(star_surf,star_rect)
        star_list = [star for star in stars if star.x > -100]
        return star_list
    else: return []

def fireb_movement(fire_ball):
    if fire_ball:
        for fire_rect in fire_ball:
            fire_rect.x  = 20

            if fire_rect.x >= 0: screen.blit(Fire_ball,fire_rect)
        fireB_list = [fireB for fireB in fire_ball if fireB.x > -100]
        return fireB_list
    else: return []

def enemy_movement(enemy_rect_list):
    if enemy_rect_list:
        for enemy_rect in enemy_rect_list:
            enemy_rect.x -= 5

            if enemy_rect.x >= 0: screen.blit(enemy_surf,enemy_rect)
        enemy_list = [enemy for enemy in enemy_rect_list if enemy.x > -100]
        return enemy_list
    else: return []

game_active = False

score = 0

stars = []
enemy_rect_list = []
fire_ball = []

time = float(pygame.time.get_ticks()/1000)

screen = pygame.display.set_mode((800,400))
game_icon = pygame.image.load("Graphics/pixels.png").convert_alpha()
pygame.display.set_icon(game_icon)
pygame.display.set_caption("Quick Dash")
clock = pygame.time.Clock()

player_surf = pygame.transform.rotate(pygame.image.load("Graphics/ships/pix_ship.png").convert_alpha(), -90)
player_rect = player_surf.get_rect(center = (100, 200))

enemy_surf = pygame.transform.rotate(pygame.image.load("Graphics/ships/pix_ship2.png").convert_alpha(), 90)

Fire_ball = pygame.transform.rotate(pygame.image.load("Graphics/ships/fireball.png").convert_alpha(), -90)

background_surf = pygame.image.load("Graphics/space.png").convert_alpha()
background_rect = background_surf.get_rect(center = (400,200))

homepage_surf = pygame.image.load("Graphics/homespace.png").convert_alpha()
homepage_rect = homepage_surf.get_rect(topleft = (0,0))

star_surf = pygame.image.load("Graphics/star.png").convert_alpha()

star_a = pygame.USEREVENT   2
pygame.time.set_timer(star_a, 200)

enemy_wave = pygame.USEREVENT   3
pygame.time.set_timer(enemy_wave, 1500)

font = pygame.font.Font("fonts/PublicPixel-z84yD.ttf", 20)


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.QUIT
            exit()
        if game_active:
            if event.type == star_a:
                stars.append(star_surf.get_rect(center=(randint(800, 1600), randint(0, 400))))
            if event.type == enemy_wave:
                enemy_rect_list.append(enemy_surf.get_rect(center=(randint(800, 1600), randint(0, 400))))
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    player_rect.y  = -50
                if event.key == pygame.K_DOWN:
                    player_rect.y  = 50
            if player_rect.collidepoint((100, 400)):
                player_rect.y = 300
            if player_rect.collidepoint((100, -55)):
                player_rect.y = -15
            if pygame.Rect.collidelist(player_rect, enemy_rect_list) != -1:
                game_active = False
            if pygame.Rect.collidelist(fire_ball, enemy_rect_list) != -1:
                score  = 1
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                fire_ball.append(Fire_ball.get_rect(center =(110,(player_rect.y   60))))
        else:
            if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                game_active = True
    if game_active:
        screen.blit(background_surf, background_rect)
        stars = star_movement(stars)
        enemy_rect_list = enemy_movement(enemy_rect_list)
        fire_ball = fireb_movement(fire_ball)
        screen.blit(player_surf, player_rect)
        main_message2 = font.render(score, False, "#11339c")
        main_message_rect = main_message2.get_rect(center=(400, 100))
    else:
        main_message = font.render(f"Press SPACE to start!", False, "#11339c")
        main_message_rect = main_message.get_rect(center=(400, 100))
        screen.blit(homepage_surf, homepage_rect)
        screen.blit(main_message, main_message_rect)
    pygame.display.update()
    clock.tick(60)

error:
Traceback (most recent call last):
  File "C:\Users\User\PycharmProjects\pythonProject\venv\game1.py", line 97, in <module>
    if pygame.Rect.collidelist(fire_ball, enemy_rect_list) != -1:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: descriptor 'collidelist' for 'pygame.Rect' objects doesn't apply to a 'list' object

CodePudding user response:

fire_ball is a list. However, collidelist is there to detect collisions between a single rectangle and a list of rectangles. If you want to detect the collision between 2 lists of rectangles, you must do it in a loop:

for ball in fire_ball:
    if ball.collidelist(enemy_rect_list) >= 0:
        score  = 1

Alternatively you can use pygame.sprite.Sprite, pygame.sprite.Group and pygame.sprite.groupcollide (also see Permanently delete sprite from memory Pygame).

  • Related