파이썬 초보 질문, 파이게임 AttributeError: 'str' object has no attribute 'get_rect'

조회수 1649회

공던지기 게임을 만드는데용

Traceback (most recent call last):
  File "c:\Users\정윤\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'

이런 식으로 자꾸 에러가 뜨더라구요,,, 그런데 어떻게 해결해야할 지 감이 안옵니다.. 도와주세요 ㅠㅠ

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/정윤/Desktop/pythonwork/l/background.png")

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

character = pygame.image.load("C:/Users/정윤/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/정윤/Desktop/pythonwork/l/weapon.png")
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

weapons = []

weapon_speed = 10

ball_images = [
    "C:/Users/정윤/Desktop/pythonwork/l/balloon1.png"
    "C:/Users/정윤/Desktop/pythonwork/l/balloon2.png"
    "C:/Users/정윤/Desktop/pythonwork/l/balloon3.png"
    "C:/Users/정윤/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()
  • ball_size = ball_images[ball_img_idx].get_rect().size 부분에 문제가 있습니다. ball_images[ball_img_idx]가 get_rect가 가능한 객체인지 확인해보세요. 초보자 2021.8.12 11:29

1 답변

  • ball_images 리스트 변수에 str 타입으로 '파일경로 및 파일명'이 할당되어 있습니다.

    str 타입에는 get_rect() 메소드가 없지요. image를 pygame.image.load 해서 Image File Obect로 만들어 주어야 합니다.

    다른 이미지 파일은 다 image load 하셨는데, 저 부분에서 실수하셨네요

    이하 screen.blit 등에서도 이 부분을 같이 확인해주셔야 할 것 같습니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)