Python novice question, pie game attributeError: 'str' object has no attribute 'get_rect'

Asked 2 years ago, Updated 2 years ago, 107 views

To make a ball throwing game

Traceback (most recent call last):
  File "c:\Users\Jeongyoon\Desktop\pythonwork\l\pypang.py", line 96, in <module>
    ball_size = ball_images[ball_img_idx].get_rect().size     
AttributeError: 'str' object has no attribute 'get_rect'

I keep getting errors like this, But I don't know how to solve it. Help me

import pygame

pygame.init()

scrren_width = 640
screen_height = 480
screen = pygame.display.set_mode((scrren_width, screen_height))

pygame.display.set_caption("BBUZUBU")

clock = pygame.time.Clock()


background = pygame.image.load ("C:/Users/Jungyoon/Desktop/pythonwork/l/background.png")

stage = pygame.image.load ("C:/Users/Jungyoon/Desktop/pythonwork/l/stage.png")
stage_size = stage.get_rect().size
stage_height = stage_size[1]

character = pygame.image.load ("C:/Users/Jungyoon/Desktop/pythonwork/l/character.png")
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (scrren_width / 2) - (character_width / 2)
character_y_pos = (screen_height - character_height - stage_height)

chracter_to_x = 0

character_speed = 5

Weapon = pygame.image.load ("C:/Users/Jungyoon/Desktop/pythonwork/l/weapon.png")
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

weapons = []

weapon_speed = 10

ball_images = [
    "C:/Users/Jungyoon/Desktop/pythonwork/l/balloon1.png"
    "C:/Users/Jungyoon/Desktop/pythonwork/l/balloon2.png"
    "C:/Users/Jungyoon/Desktop/pythonwork/l/balloon3.png"
    "C:/Users/Jungyoon/Desktop/pythonwork/l/balloon4.png"]

ball_speed_y = [-18, -15, -12, -9]

balls = []

balls.append({
    "pos_x" : 50,
    "pos_y" : 50,
    "img_idx" : 0,
    "to_x": 3, 
    "to_y": -6,
    "init_spd_y" : ball_speed_y[0]
    })

running = True
while running: 
    dt = clock.tick (30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
               chracter_to_x -= character_speed
            elif event.key == pygame.K_RIGHT:
                chracter_to_x += character_speed
            elif event.key == pygame.K_SPACE:
                weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
                weapon_y_pos = character_y_pos
                weapons.append([weapon_x_pos, weapon_y_pos])

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                chracter_to_x = 0

    character_x_pos += chracter_to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > scrren_width - character_width:
        character_x_pos = scrren_width - character_width

    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons]    

    weapons = [[w[0], w[1]] for w in weapons if w[1] > 0]

    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx= ball_val["img_idx"]

        ball_size = ball_images[ball_img_idx].get_rect().size
        ball_width = ball_size[0]
        ball_height = ball_size[1]

        if ball_pos_x < 0 or ball_pos_x > scrren_width - ball_width:
            ball_val["to_x"] = ball_val["to_x"] * -1

        if ball_pos_y >= screen_height - stage_height - ball_height:
            ball_val["to_y"] = ball_val["init_spd_y"]
        else:
            ball_val["to_y"] += 0.5


        ball_val["pos_x"] += ball_val["to_x"]
        ball_val["pos_y"] += ball_val["to_y"]

    screen.blit(background, (0,0))
    for weapon_x_pos, weapon_y_pos in weapons: 
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))

    for idx, val in enumerate(balls):
        ball_pos_x = val["pos_x"]
        ball_pos_y = val["pos_y"]
        ball_img_idx = val["img_idx"]
        screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))




    pygame.display.update()

pygame.quit()

python pygame

2022-09-20 15:18

1 Answers

ball_images list variable is assigned 'file path and filename' as str type.

The str type does not have the get_rect() method. Image should be pygame.image.load to make it ImageFileObect.

You've already loaded all the other image files, but you made a mistake there

I think you should also check this part in screen.blit below.


2022-09-20 15:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.