Skip to main content
 首页 » 编程设计

python之我如何实现图片而不是我的红色矩形

2025年01月19日11xiaohuochai

<分区>

我似乎无法从文件中添加图片而不是显示的红色矩形。 这只是我的代码中的一类。 我看过其他关于如何执行此操作的教程,但我没有任何运气。当我尝试添加 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