1

I've tried everything to fix this, my code runs and a screen is shown but the image is not on the screen. so far I've tried:

  • putting an absolute path when entering the image in pygame.image.load
  • trying os.join.path method (file cannot be found error)
  • And loads of other things -How after trying everything, and closely following the book, i cant get it to work?

my code from alien_invasion.py:

import sys

import pygame

from settings import Settings

from ship import Ship


class AlienInvasion:

    def __init__(self):
        pygame.init()
        self.settings = Settings()

        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        pygame.display.set_caption("Alien Invasion")

        self.bg_color = (255, 0, 0)
        self.ship = Ship(self)

    def run_game(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
            self.screen.fill(self.settings.bg_color)
            pygame.display.flip()

            self.screen.fill(self.bg_color)
        
            self.ship.blitme()
        
if __name__ == '__main__':
    ai = AlienInvasion()
    ai.run_game()

code from ship.py:


import pygame




class Ship:
    def __init__(self, ai_game):
        self.screen = ai_game.screen
        self.screen_rect = ai_game.screen.get_rect()

        self.image = pygame.image.load('C:/Users/Documents/Python Learning/python crash course lessons/Alien Invasion/ship1.bmp')
        self.rect = self.image.get_rect()
        self.rect.midbottom = self.screen_rect.midbottom

    def blitme(self):
        self.screen.blit(self.image, self.rect)

code from 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 = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)
    

I really want to find the solution to this otherwise I will remain stuck since none of the solutions to other people with the same problem has solved my problem... the program works, there are no errors shown the image just doesn't load to the image to the screen.

thank you if you helped

student789
  • 23
  • 2
  • Normally you would call flip after rendering to the back buffer, but it isn't clear you've set up double buffering. – Garr Godfrey May 25 '21 at 18:57
  • 3
    Stupid question: are you absolutely sure that's the correct full path to the image? Typically on Windows systems the path would begin with `C:/Users/username/Documents/...`. You don't have a username. – MattDMo May 25 '21 at 18:57

1 Answers1

2

The image is not shown, because the display is not updated after drawing the image. Update the display after drawing the ship:

class AlienInvasion:
    # [...]

    def run_game(self):

        clock = pygame.time.Clock()
        run = True
        while run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False    
            
            self.screen.fill(self.bg_color)
            self.ship.blitme()
            pygame.display.flip()
            clock.tick(60)

        pygame.quit()
        sys.exit()

The typical PyGame application loop has to:

sloth
  • 99,095
  • 21
  • 171
  • 219
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • thanks for your answer, it displays an image but i will have to study this more because the image keeps blinking, thanks so much for your insight its been very helpful – student789 May 26 '21 at 08:19
  • @student789 The image is blinking when you update the display more than once. See [Why is the PyGame animation is flickering](https://stackoverflow.com/questions/62120723/why-is-the-pygame-animation-is-flickering/62120776#62120776) . Do just on call of `pygame.display.update()` or `pygame.display.flip()` at the end of the application loop. – Rabbid76 May 26 '21 at 08:29