Python Forum
[PyGame] drawing images onto pygame window - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: [PyGame] drawing images onto pygame window (/thread-32626.html)



drawing images onto pygame window - djwilson0495 - Feb-22-2021

Hi I'm trying to draw a grid of stars onto a pygame screen as a background. The code runs but the pygame window is empty. Here is my code:

import sys 

import pygame  # contains functionality to make a game

from settings import Settings
from star import Star

class StarrySky:
    """Initialise a grid of stars on the screen."""
    def __init__(self):
        """Initialise the game and create game resources."""
        pygame.init() # initialise the background settings 
        # which the game needs to work properly
        self.settings = Settings()

        self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
        # tells pygame to figure out a window size to fill the screen
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        # updates the screen settings after fullscreen has been created
        pygame.display.set_caption("Starry Sky")

        self.star = Star(self)
        self.stars = pygame.sprite.Group()

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self.check_events()         
            self.update_screen()
            
    def check_events(self):
    # Watch for keyboard and mouse events.
        for event in pygame.event.get():
            if event.type == pygame.K_q:
                sys.exit()

    def create_grid(self):
        """Create the grid of stars."""
        # create a star and find the number of stars in a row.
        # spacing between stars equal to 1 star width.
        star = Star(self)
        star_width, star_height = star.rect.size
        available_space_x = self.settings.screen_width - (2 * alien_width)
        number_stars_x = available_space_x // (2 * star_width)

        # Determine the number of rows of aliens that fit on the screen.
        available_space_y = (self.settings.screen_height - 
        (3 * alien_height) )
        number_rows = available_space_y // (2 * star_height)

        # Create the full sky of stars. 
        for row_number in range(number_rows):
        # Create the first row of stars.
            for star_number in range(number_stars_x):
                # Create a star and place it in the row.
                self._create_star(star_number,row_number) 
        
    def create_star(self, row_number, star_number):
        star = Star(self)
        star.x = star_width + 2 * star_width * star_number
        star.rect.x = star.x
        star.rect.y = star.rect.height + 2 * star.rect.height * row_number
        self.stars.add(star)

    def update_screen(self):
        """Update images on the screenand flip to the new screen."""
        # redraw the screen during each pass through the loop 
        self.screen.fill(self.settings.bg_color)
        self.stars.draw(self.screen)
        pygame.display.flip()

if __name__ == "__main__":
    # make a game instance and run the game
    ai = StarrySky()
    ai.run_game()
            
Can someone help?


RE: drawing images onto pygame window - nilamo - Feb-22-2021

self.stars is a SpriteGroup that you draw each frame. It looks like the only place you add items to that sprite group is in StarrySky.create_grid()... but I don't see anywhere where that function gets called. Should that be included in the __init__ method?

Something like:
    def __init__(self):
        """Initialise the game and create game resources."""
        pygame.init() # initialise the background settings 
        # which the game needs to work properly
        self.settings = Settings()
 
        self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
        # tells pygame to figure out a window size to fill the screen
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        # updates the screen settings after fullscreen has been created
        pygame.display.set_caption("Starry Sky")
 
        self.star = Star(self)
        self.stars = pygame.sprite.Group()
        self.create_grid()