1

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)

Second file:

    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()

Settings file:

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)

My bmp look like that:

enter image description here

I tried to add different image, but no luck:

enter image description here

How can I fix it?

4
  • Don't you have to apply convert() on your image? Commented Mar 21, 2016 at 17:56
  • I don't know, there is no such thing in book code. How can I modify my code with that? Commented Mar 21, 2016 at 18:11
  • self.image = pygame.image.load('ship.bmp').convert() - just try it with convert() at the end Commented Mar 21, 2016 at 19:00
  • Nothing changes, still the same deformed image. Commented Mar 21, 2016 at 19:36

1 Answer 1

1

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.

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.

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I think that's it and I could scratch my head for ages trying to fix that code. Mac with El Captain here.
Also El Capitain! Let me know if switching to something else works for you. Here's my source btw. Unfortunately, I wasn't able to downgrade SDL so I can't 100% say its the issue, although it's very likely. stackoverflow.com/questions/33582204/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.