So basically im trying to make this grid of stars for something, however when I run the program no image appears on the screen. Please could someone tell me if there are any visible errors, as I just cant see a reason for this not to work.
This is the main program:
import sys
import pygame
from sett import Sett
from star import Star
class Stars:
"""Overall class to manage star assests and behaviour."""
def __init__(self):
"""Initialise sky and create game resources."""
pygame.init()
self.sett = Sett()
self.screen = pygame.display.set_mode(
(self.sett.screen_width, self.sett.screen_height))
pygame.display.set_caption("STARS")
self.stars = pygame.sprite.Group()
self._create_sky()
def run_s(self):
"""start the main loop for the stars."""
while True:
self._check_events()
self._update_screen()
def _check_events(self):
"""Respond to keypresses and mouse events."""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def _create_sky(self):
"""Create sky of stars"""
#Determine how many stars fit in a row.
star = Star(self)
star_width, star_height = star.rect.size
available_space_x = self.sett.screen_width - (2 * star_width)
number_stars_x = available_space_x // (2 * star_width)
#Determine how many rows fit on the screen.
available_space_y = (self.sett.screen_height - (3 * star_height))
number_rows = available_space_y // (2 * star_height)
#Create the full sky of aliens.
for row_number in range(number_rows):
for star_number in range(number_queens_x):
self._create_star(star_number, row_number)
def _create_star(self, queen_number, row_number):
"""Create star and place it in row"""
star = Star(self)
star_width, star_height = star.rect.size
star.x = star_width + 2 * star_width * star_number
star.rect.x = star.x
star.rect.y = star_height + 2 * star.rect.height * row_number
self.stars.add(star)
def _update_screen(self):
"""Update images on the screen and flip to the new screen"""
#Redraw the screen for each pass through the loop.
self.screen.fill(self.sett.bg_color)
self.stars.draw(self.screen)
pygame.display.flip()
if __name__ == '__main__':
si = Stars()
si.run_s()
this is the module containing the star:
import pygame
from pygame.sprite import Sprite
from sett import Sett
class Star(Sprite):
"""A class to represent a single star"""
def __init__(self, ai_game):
"""Initialise the queen and set its starting position"""
super().__init__()
self.screen = ai_game.screen
self.sett = ai_game.sett
#Load star image and set its rect attribute.
self.image = pygame.image.load('star1.bmp')
self.rect = self.image.get_rect()
#Start each new star near the top left of the screen.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
#Store the stars exact horizontal position.
self.x = float(self.rect.x)
Any help woukd be greatly appreciated… thanks everyone.