2017-10-21 163 views
1

我正在構建一個遊戲,我正試圖在屏幕中間的底部顯示一個小圖片。 我不明白爲什麼我根本看不到圖像?32位bmp圖像不顯示在pygame的屏幕上

這是我的照片級的代碼,在一個名爲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文件中設置類:

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'的東西,它能夠正常工作對我來說。請添加來自th的相關變量e'settings'模塊。圖像可能有些問題。你有沒有收到任何錯誤信息? – skrx

+0

@skrx我添加了設置代碼。我沒有得到任何錯誤消息... –

回答

1

我發現了這個問題 - 這是一個非常奇怪的問題: 當我編輯我的圖像時,我將它保存爲一個32位bmp文件(默認選項是24位,我想我自己「我」 m使用32位python,我認爲它會匹配更好) 但是當我試圖在pygame中顯示我的圖像 - 它沒有顯示出來 我嘗試了任何東西,最後我試圖再次編輯圖像。

現在,我救它作爲一個24位BMP和它工作得很好!

+1

32位的意思是[只有一個額外的字節用作BMP中的填充](https://stackoverflow.com/questions/7369649/how-to-convert-32- bit-bmp-to-contain-alpha-channel)(8bit pr.channel)。這與你的python安裝的位數無關。我的猜測是,pygame中的BMP加載程序不支持32位BMP(..這是一種無用的方式,因爲有許多格式可以完成BMP所做的一切,但效果會更好) – MatsLindh

+0

我正要建議使用另一種格式,如.png(也因爲bmp文件相當大),但忘了張貼。很好,你已經知道了你自己。 – skrx