Python - 无法打开图像

时间:2018-04-02 17:54:28

标签: pygame

我的PYTHON游戏中出现错误的图像:
IMAGE WITH ERROR IN MY PYTHON GAME

我想运行我的应用程序,但是看到这个错误,如何修复它?

具有正确格式的图像无法打开,这是一个简单的Python游戏

import random
from livewires  import games
games.init(screen_width=640,screen_height=480,fps=50)
class Ship(games.Sprite):
def update(self):

    if games.keyboard.is_pressed(games.K_RIGHT):
        self.angle+=1
    if games.keyboard.is_pressed(games.K_LEFT):
        self.angle-=1

    if games.keyboard.is_pressed(games.K_1):
        self.angle=0
    if games.keyboard.is_pressed(games.K_2):
        self.angle=90
    if games.keyboard.is_pressed(games.K_3):
        self.angle=180
    if games.keyboard.is_pressed(games.K_4):
        self.angle=270
class Asteroid(games.Sprite):
    SMALL = 1
    MEDIUM = 2
    LARGE = 3
    images={SMALL : games.load_image("asteroida_s.bmp"),
            MEDIUM : games.load_image('asteroida_m.bmp'),
            LARGE : games.load_image('asteroida_l.bmp')   }
    SPEED=2
def main():
nebula_image=games.load_image("mglawica.jpg")
games.screen.background=nebula_image

for i in range(8):
    x=random.randrange(games.screen.width)
    y=random.randrange(games.screen.height)
    size-random.choice([Asteroid.SMALL,Asteroid.MEDIUM,Asteroid.LARGE,])
    new_asteroid=Asteroid(x=x,y=y,size=size)
    game.screen.add(new_asteroid)
games.screen.mainloop()
def main():
nebula_image=games.load_image("mglawica.jpg",transparent=false)
games.screen.background=nebula_image

ship_image=games.load_image("statek.bmp")
the_ship=Ship(image=ship_image,
    x=games.scren.width/2,
    y=games.scren.height/2)
games.scren.add(the_ship)
games.screen.mainloop()
def play(self):
nebula_image=games.load_image("mglawica.jpg")
games.screen.background=nebula_image
self.advance()
games.screen.mainloop()
def advanced(self):
self.level+=1
BUFFER=150
for i in range(self.level):
    x_min=random.randrage(BUFFER)
    y_min=BUFFER-x_min
    x=self.ship.x+x_distance
    y=self.ship.y+y_distance
    x%=games.screen.width
    y%=games.screen.height
    new_asteroid=Asteroid(game=self,x=x,y=y,size=Asteroid.LARGE)
    game.screen.add(new_asteroid)
def end():
end_message=games.Message(value="Koniec gry",
                          size=90,
                          color=color.red,
                          x=games.screen.width/2,
                          y=games.screen.height/2,
                          lifetime=10*games.scren.fps,after_death=games.screen.quit,
                          is_sollideable=False)
games.scren.add(end_message)
def die(self):
    if self.size !=Asteroid.SMALL:
        for i in range(Asteroid.SMALL):
            new_asteroid=Asteroid(x=self.x,
                                  y=self.y,
                                  size=self.size-1)
            game.screen.add(new_asteroid)
    self.destroy()

1 个答案:

答案 0 :(得分:0)

虽然你的代码非常混乱(没有缩进),但我认为我有办法解决它。首先,我质疑你需要导入livewire,还想知道为什么你把这个问题标记为pygame。你从来没有导入过pygame。所以我的解决方案将以pygame编写。

首先,您必须加载图像(如果您想将其分配给变量):

 image = pygame.image.load(filename)

然后,你必须转换Surface,或者基本上只转换图像。

 image = pygame.image.load(filename).convert()