Python 如何实现图片而不是红色矩形?

Python 如何实现图片而不是红色矩形?,python,pygame,Python,Pygame,我似乎无法从文件中添加图片,而不是正在显示的红色矩形。 这只是我代码的一个类。 我已经看过了其他关于如何做到这一点的教程,但我没有任何运气。当我尝试添加image.load命令时,我一直得到一个没有任何内容的黑色窗口。 我想图像显示,而不是红色的矩形,但仍然与相同的x和y值等 def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) self.x_change = 0 self.y_change = 0

我似乎无法从文件中添加图片,而不是正在显示的红色矩形。 这只是我代码的一个类。 我已经看过了其他关于如何做到这一点的教程,但我没有任何运气。当我尝试添加image.load命令时,我一直得到一个没有任何内容的黑色窗口。 我想图像显示,而不是红色的矩形,但仍然与相同的x和y值等

def __init__(self, x, y):
    pygame.sprite.Sprite.__init__(self)

    self.x_change = 0
    self.y_change = 0
    self.jump_duration = 15
    self.jumping = False
    self.jump_cooldown = 0
    self.move_unit = 5
    self.jump_cooldown_duration = 0
    self.width = 200
    self.height = 200
    self.image = pygame.Surface([self.width, self.height])
    self.image.fill(RED)
    self.rect = self.image.get_rect()
    self.x = x
    self.y = y
    self.rect.x = self.x
    self.rect.y = self.y

def move(self, movement):
    if movement == "":
        self.x_change = 0
        self.y_change = 0

    if movement == "L":
        self.x_change =- self.move_unit

    if movement == "R":
        self.x_change = self.move_unit

    if movement == "U" and self.jump_duration >= 0 and self.jump_cooldown == 0:
        self.y_change =- (self.move_unit + 1)
        self.jump_duration -= 1
        self.jumping = True
        print("jumping")

    if self.jump_duration<0:
        self.jump_duration=10
        self.jump_cooldown=10
        self.jumping=False

def start_jump_cooldown(self):
    if self.jumping:
        self.jump_cooldown_duration = 60

def update(self, movement):
    if movement == "L":
        self.x_change=-self.move_unit
    if movement == "R":
        self.x_change=self.move_unit
    if self.jump_duration<0:
        self.jump_duration=5
        self.jump_cooldown_duration=10
        self.jumping=False
    if self.jump_cooldown_duration>0:
        self.jump_cooldown_duration-=1

    self.x += self.x_change
    self.y += self.y_change

    self.rect.x = self.x
    self.rect.y = self.y
def\uuuu init\uuuu(self,x,y):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.x_change=0
self.y_change=0
self.jump_持续时间=15
自我跳跃=错误
self.jump_冷却时间=0
self.move_单位=5
self.jump\u冷却时间\u持续时间=0
自宽=200
自身高度=200
self.image=pygame.Surface([self.width,self.height])
self.image.fill(红色)
self.rect=self.image.get_rect()
self.x=x
self.y=y
self.rect.x=self.x
self.rect.y=self.y
def移动(自身、移动):
如果移动==“”:
self.x_change=0
self.y_change=0
如果移动==“L”:
self.x\u change=-self.move\u单位
如果移动==“R”:
self.x\u change=self.move\u单元
如果移动=“U”且self.jump\U持续时间>=0且self.jump\U冷却时间==0:
self.y\u change=-(self.move\u单元+1)
self.jump_持续时间-=1
self.jumping=True
打印(“跳跃”)
如果self.jump\u duration使用该函数从硬盘加载映像,并将其分配给类的
\uuuu init\uuu
方法中的
self.image
属性

如果文件位于子目录中,则应使用
os.path.join
函数构造路径,以便它能在不同的操作系统中正常工作

应该调用(或
convert_alpha
用于透明图像)方法来提高性能

import os
import pygame

pygame.init()
# The display has to be initialized.
screen = pygame.display.set_mode((640, 480))
# Pass the path of the image to pygame.image.load.
MY_IMAGE = pygame.image.load('image.png').convert_alpha()
# If the image is in a subdirectory, for example "assets".
MY_IMAGE = pygame.image.load(os.path.join('assets', 'image.png')).convert_alpha()


class Player(pygame.sprite.Sprite):

    def __init__(self, pos):
        super().__init__()
        self.image = MY_IMAGE  # Assign the image.
        self.rect = self.image.get_rect(center=pos)

我们使用大写名称,如
MY_IMAGE
来表示它是一个常量,不应更改。在console/terminal/cmd.exe中运行它时是否收到错误消息?显示加载图像的代码。