Created
June 13, 2021 06:57
-
-
Save PatrickKalkman/5a99f18c5b0844c1f28fd92101422862 to your computer and use it in GitHub Desktop.
The background star field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StarField(): | |
def __init__(self): | |
self.star_field_slow = self.create_stars(50) | |
self.star_field_medium = self.create_stars(35) | |
self.star_field_fast = self.create_stars(30) | |
def create_stars(self, number_of_stars): | |
stars = [] | |
for _ in range(number_of_stars): | |
star_loc_x = random.randrange(0, constants.SCREEN_WIDTH) | |
star_loc_y = random.randrange(0, constants.SCREEN_HEIGHT) | |
stars.append([star_loc_x, star_loc_y]) | |
return stars | |
def render_stars(self, screen, star_collection, speed, size, color): | |
for star in star_collection: | |
star[1] += speed | |
if star[1] > constants.SCREEN_HEIGHT: | |
star[0] = random.randrange(0, constants.SCREEN_WIDTH) | |
star[1] = random.randrange(-20, -5) | |
pygame.draw.circle(screen, color, star, size) | |
def render(self, screen): | |
self.render_stars(screen, self.star_field_slow, 1, 3, DARKGREY) | |
self.render_stars(screen, self.star_field_medium, 4, 2, LIGHTGREY) | |
self.render_stars(screen, self.star_field_fast, 8, 1, YELLOW) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment