2017-10-21 12 views
1

私はゲームを構築しており、画面の中央下部に小さな画像を表示しようとしています。 私はイメージが全く見えない理由を理解できません。pygameの画面に32bitのbmpイメージが表示されない

これは、ファイルにdevil.pyと呼ばれる私の写真のクラスコードです:

import pygame 

class Devil(): 

    def __init__(self, screen): 
     """Initialize the devil and set its starting position""" 
     self.screen=screen 

     #Load the devil image and get its rect 
     self.image=pygame.image.load('images/devil.bmp') 
     self.rect=self.image.get_rect() 
     self.screen_rect=screen.get_rect() 

     #Start each new devil at the bottom center of the screen 
     self.rect.centerx=self.screen_rect.centerx 
     self.rect.bottom=self.screen_rect.bottom 

    def blitme(self): 
     """Draw the devil at its current location""" 
     self.screen.blit(self.image,self.rect) 

そして、これは別のファイルに書かれている私のメインのコードです:

import sys 
import pygame 

from settings import Settings 
from devil import Devil 

def run_game(): 
    #Initialize pygame, settings and create a screen object. 
    pygame.init() 
    dvs_settings=Settings() 
    screen=pygame.display.set_mode(
     (dvs_settings.screen_width, dvs_settings.screen_height)) 
    pygame.display.set_caption("Devil vs Shitty") 

    #Make a devil 
    devil=Devil(screen) 

    #Start the main loop the game. 
    while True: 

     #Watch for keyboard and mouse events. 
     for event in pygame.event.get(): 
      if event.type==pygame.QUIT: 
       sys.exit() 

     #Redraw the screen during each pass through the loop. 
     screen.fill(dvs_settings.bg_color) 
     devil.blitme() 

     #Make the most recently drawn screen visible 
     pygame.display.flip() 
run_game() 

そしてこれが私ですsettings.pyというファイルのsettings.py:

class Settings(): 
    """A Class to store all settings for Alien Invasion.""" 
    def __init__(self): 
     """Initialize the game's settings""" 
     #Screen settings 
     self.screen_width = 1000 
     self.screen_height = 600 
     self.bg_color=(230,230,230) 

私がここで間違っていることが見つかりません。

+0

私はエラーを再現することはできません。私は画像のみと 'settings'ものを交換し、それが私のために正常に動作します〜から関連する変数を追加してくださいe 'settings'モジュールも同様です。おそらく何かイメージに間違いがあります。エラーメッセージが出ますか? – skrx

+0

@skrx設定コードを追加しました。私はエラーメッセージを受け取っていません... –

答えて

1

私はこの問題を発見しました - これは非常に奇妙なものです イメージを編集しているときに、32ビットのbmpファイルとして保存しました(デフォルトのオプションは24ビットでした。私は32ビットのPythonを使っていますが、それはより良くマッチすると思います) しかし、私はパイゲームで自分のイメージを表示しようとしました - それは表示されませんでした 私は何も試しました。画像。今

、私は24ビットBMPとして保存し、それがうまく機能!!!

+1

32ビットは[BMPにパディングとして使用される余分なバイトがあることを意味します](https://stackoverflow.com/questions/7369649/how-to-convert-32- bit-bmp-to-contain-alpha-channel)(8ビットチャネル)。あなたのPythonインストールのビット数には関係ありません。私の推測では、pygameのBMPローダーは32ビットのBMPをサポートしていません(BMPのすべてのことを行う多くのフォーマットがありますが、役に立たないですが)。 – MatsLindh

+0

私は、 .pngのような別のフォーマット(bmpファイルがかなり大きいため)でも、投稿するのを忘れてしまいました。あなた自身でそれを理解したことは良いことです。 – skrx

関連する問題