关于python的问题,请各位解答!急!

用Pycharm的"AttributeError: 'str' object has no attribute 'convert_alpha'"如何解决,急!
请看看如何解决?

# Terraria
# by the Spring Rain
# 2023/7

import os
import sys
import json
import pygame

FPS = 60
WIDTH = 800
HEIGHT = 600


def get_all_items():
    items_dic = {}
    with open("Terraria\\items.json", encoding="utf8") as fp:
        temp = json.load(fp)
        for k in temp.keys():
            items_dic[k] = temp[k]["Name"]
    return items_dic


items_name = get_all_items()

print(items_name)

pygame.init()
pygame.key.set_repeat(1, 10)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)
s = 0
title = pygame.image.load("pictures\\Terraria.png")
player = pygame.image.load("pictures\\Iron_Pickaxe.jpg")
background1 = pygame.image.load("pictures\\Forest_background_15_副本.jpg")
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
pygame.display.set_caption("Terraria")
screen.blit(background1, (0, 0))

frames = []
for i in range(4):
    frame = pygame.image.load(os.path.join("pictures\\Style_1_male_walking.gif").convert_alpha())
    frames.append(frame)

x = 25
y = 25
mx, my = pygame.mouse.get_pos()


def start(x, y):
    while True:
        screen.blit(background1, (0, 0))
        screen.blit(player, (x, y))
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT or event.key == pygame.K_a:
                    x -= 1
                elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
                    x += 1
                elif event.key == pygame.K_UP or event.key == pygame.K_w:
                    y -= 1
                elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
                    y += 1


def open_game():
    while True:
        screen.blit(background1, (0, 0))
        screen.blit(title, (400, 300))
        pygame.display.update()
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                if screen.get_at((mx - 20, my)) == title or screen.get_at((mx + 20, my)) == title or screen.get_at(
                        (mx, my - 10)) == title or screen.get_at((mx, my + 10)) == title:
                    s = 1
                    break
                else:
                    continue


open_game()

while True:
    if s == 1:
        start(x, y)
    else:
        continue


谢谢!

你的代码中,错误出现在这行:

frame = pygame.image.load(os.path.join("pictures\\Style_1_male_walking.gif").convert_alpha())

问题是 convert_alpha()pygame.Surface 类的方法,也就是图片加载后(pygame.image.load)才可以调用。但你试图在字符串上使用这个方法,这就是为什么你收到 'str' object has no attribute 'convert_alpha' 错误。

你应该首先调用 pygame.image.load() 来加载图片,然后在结果上调用 convert_alpha()

这样修复你的代码:

frame = pygame.image.load(os.path.join("pictures", "Style_1_male_walking.gif"))
frame = frame.convert_alpha()
frames.append(frame)

注意我也在 os.path.join 中使用了逗号来分隔目录和文件名,这是它的正确使用方法。os.path.join 会根据你的操作系统自动选择正确的路径分隔符。

第46行代码错误
代码应修改为

frame = pygame.image.load(os.path.join("pictures\\Style_1_male_walking.gif")).convert_alpha()