Python Pygame无法正确显示图像 [英] Python Pygame doesn't display images correctly

查看:238
本文介绍了Python Pygame无法正确显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,我从Eric Matthes的"Python崩溃课程"开始学习.我正处于Pygame章节的开头,并且遵循代码,但是加载的图像始终看起来损坏,我也不知道为什么.代码来自本书.第一个文件:

I'm new to Python and I started learning with "Python crash course" by Eric Matthes. I'm in the beginning of Pygame chapter and I follow the code, but my loaded images always look damaged and I don't know why. Code is from the book. First file:

    import pygame
    class Ship():
        def __init__(self, screen):
            """Initialize the ship and set its starting position."""

    # Load the ship image and get its rect.
            self.image = pygame.image.load('ship.bmp')
            self.screen = screen
            self.rect = self.image.get_rect()
            self.screen_rect = screen.get_rect()
    # Start each new ship at the bottom center of the screen.
            self.rect.centerx = self.screen_rect.centerx
            self.rect.bottom = self.screen_rect.bottom
        def blitme(self):
            self.screen.blit(self.image, self.rect)

第二个文件:

    import sys
    import pygame
    from settings import Settings
    from ship import Ship
    def run_game():
        # Initialize game and create a screen object.
        pygame.init()
        ai_settings = Settings()
        screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
        pygame.display.set_caption("Alien Invasion")
        ship = Ship(screen)
        bg_color = (230, 230, 230)

        # Start the main loop for the game.
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()      
            # Make the most recently drawn screen visible.

            screen.fill(ai_settings.bg_color)
            ship.blitme()


            pygame.display.flip()


    run_game()

设置文件:

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

我的bmp看起来像这样:

My bmp look like that:

我尝试添加其他图片,但没有运气:

I tried to add different image, but no luck:

我该如何解决?

推荐答案

我认为您使用的是Mac,并且具有相对较新的SDL版本.问题不在于您的代码,而在于SDL的较新版本在Mac OS中存在错误.

I presume you are on a Mac, with a relatively new version of SDL. The issue is not with your code, but newer versions of SDL having a bug with Mac OS.

要解决此问题,您需要将SDL降级到1.2版之前的版本(该版本就在那里,忘记了确切的版本),或者在其他操作系统上工作.

To resolve, you either need to downgrade your SDL to a version prior to around version 1.2 (it's around there, forgot the exact version), or work on a different operating system.

这很烦人..我最终在Mac上安装了virtualbox并运行Linux,只是为了能够编写pygame!

It's very annoying.. I ended up installing virtualbox and running Linux on my Mac just to be able to code pygame!

这篇关于Python Pygame无法正确显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆