Skip to content

Instantly share code, notes, and snippets.

@ogilviemt
Created November 22, 2014 16:28
Show Gist options
  • Save ogilviemt/9b05a89d023054e6279f to your computer and use it in GitHub Desktop.
Save ogilviemt/9b05a89d023054e6279f to your computer and use it in GitHub Desktop.
Animated star field background using pygame
import pygame
import random
screen = pygame.display.set_mode([1024, 768])
height = pygame.display.Info().current_h
width = pygame.display.Info().current_w
pygame.display.set_caption('Window Caption')
clock = pygame.time.Clock()
#create the locations of the stars for when we animate the background
star_field_slow = []
star_field_medium = []
star_field_fast = []
for slow_stars in range(50): #birth those plasma balls, baby
star_loc_x = random.randrange(0, width)
star_loc_y = random.randrange(0, height)
star_field_slow.append([star_loc_x, star_loc_y]) #i love your balls
for medium_stars in range(35):
star_loc_x = random.randrange(0, width)
star_loc_y = random.randrange(0, height)
star_field_medium.append([star_loc_x, star_loc_y])
for fast_stars in range(15):
star_loc_x = random.randrange(0, width)
star_loc_y = random.randrange(0, height)
star_field_fast.append([star_loc_x, star_loc_y])
#define some commonly used colours
WHITE = (255, 255, 255)
LIGHTGREY = (192, 192, 192)
DARKGREY = (128, 128, 128)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
MAGENTA = (255, 0, 255)
CYAN = (0, 255, 255)
#create the window
pygame.init()
app_is_alive = True
while app_is_alive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print "Exiting... All hail the void!"
app_is_alive = False #murderer!
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
print "Exiting... All hail the void!"
app_is_alive = False #murderer!
#my soul knows only darkness
screen.fill(BLACK)
#animate some motherfucking stars
for star in star_field_slow:
star[1] += 1
if star[1] > height:
star[0] = random.randrange(0, width)
star[1] = random.randrange(-20, -5)
pygame.draw.circle(screen, DARKGREY, star, 3)
for star in star_field_medium:
star[1] += 4
if star[1] > height:
star[0] = random.randrange(0, width)
star[1] = random.randrange(-20, -5)
pygame.draw.circle(screen, LIGHTGREY, star, 2)
for star in star_field_fast:
star[1] += 8
if star[1] > height:
star[0] = random.randrange(0, width)
star[1] = random.randrange(-20, -5)
pygame.draw.circle(screen, YELLOW, star, 1)
#redraw everything we've asked pygame to draw
pygame.display.flip()
#set frames per second
clock.tick(30)
#quit gracefully
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment