import pygame
from pygame.sprite import Sprite
from pygame.sprite import Group
import sys
class Star (Sprite):
"""the class that represents a single star"""
def __init__(self, screen):
super().__init__()
self.screen = screen
-sharp Load the image and obtain its rectangular.
self.image = pygame.image.load(r"images\star.bmp")
self.rect = self.image.get_rect()
-sharp Make every star show up at someplace near the left corner.
self.rect.x = self.rect.width
self.rect.y = self.rect.height
def blitme():
self.screen.blit(self_screen, self.image)
def get_number_stars_x (screen_width, star_width):
available_space_x = screen_width - 2 * star_width
number_stars_x = int(available_space_x / 2 * star_width)
return number_stars_x
def get_number_rows (screen_height, star_height):
available_space_y = screen_height - 2 * star_height
number_rows = int(available_space_y / 2 * star_height)
return number_rows
def create_star (screen, stars, star_number, row_number):
"""Make one star and put it in the current line."""
star = Star(screen)
star.rect.x = star.rect.width + 2 * star.rect.width * star_number
star.rect.y = star.rect.height + 2 * star.rect.height * row_number
stars.add(star)
def create_bunch (screen, stars, screen_width, screen_height):
star = Star(screen)
number_stars_x = get_number_stars_x(screen_width, star.rect.width)
number_rows = get_number_rows(screen_height, star.rect.height)
for star_number in range(number_stars_x):
for row_number in range(number_rows):
create_star(screen, stars, star_number, row_number)
def check_events ():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
def show_stars ():
pygame.init()
screen_height = 675
screen_width = 1200
screen = pygame.display.set_mode((screen_width, screen_height))
bg_color = (255,255,255)
pygame.display.set_caption("Stars")
stars = Group()
create_bunch(screen, stars, screen_width, screen_height)
while True:
screen.fill(bg_color)
check_events()
stars.draw(screen)
pygame.display.flip()
show_stars ()