A clone of Asteroids.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

import pygame as pg
import random
WHITE = (255,)*3
def snap(x, scale):
return int(x//scale)*scale
class Starfield:
def __init__(self,
screen,
inv_density=10,
grid_step=31,
paralax = 2,
star_color=WHITE,
star_size = 1):
self.rng = random.Random()
self.screen = screen
self.inv_density = inv_density
self.paralax = paralax
self.grid_step = grid_step
self.star_color = star_color
self.star_size = star_size
def draw(self, screen, map_pos, map_period):
sky_pos = pg.math.Vector2((map_pos.x, map_pos.y))/self.paralax
xstart = snap(sky_pos.x, self.grid_step)
ystart = snap(sky_pos.y, self.grid_step)
srect = screen.get_rect()
xstop = xstart + srect.width
ystop = ystart + srect.height
for x in range(xstart, xstop, self.grid_step):
for y in range(ystart, ystop, self.grid_step):
self.rng.seed(hash((
x % map_period[0], y % map_period[1])))
if self.rng.randint(0, self.inv_density) == 0:
xoff = self.rng.randint(0, self.grid_step)
yoff = self.rng.randint(0, self.grid_step)
cx = x + xoff - sky_pos.x
cy = y + yoff - sky_pos.y
pg.draw.circle(screen, self.star_color, (cx,cy), self.star_size)