Skip to content

Instantly share code, notes, and snippets.

@kliph
Created July 3, 2012 19:55
Show Gist options
  • Save kliph/3042535 to your computer and use it in GitHub Desktop.
Save kliph/3042535 to your computer and use it in GitHub Desktop.
possibly broken starfield animation
#!/usr/bin/python2.6
# -*- coding: utf-8 -*-
# from: https://lorenzod8n.wordpress.com/2007/05/25/pygame-tutorial-1-getting-started/
import pygame
import pdb
from random import randrange, choice
x = y = 0
running = 1
MAX_STARS = 30
LAYERS = "near", "mid", "far"
BGCOLOR = 0, 0, 0
SCREEN_WIDTH = 900
SCREEN_HEIGHT = 500
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
LEVELS = {
"foo" : "bar"# will need to move this back, just a placehoolder for now
}
class Star:
def __init__(self, **options):
self.xcoord = options.get("xcoord", randrange(0, screen.get_width() - 1))
self.ycoord = options.get("ycoord", randrange(0, screen.get_height() - 1))
self.coords = (self.xcoord, self.ycoord)
self.distance = options.get("distance", "far")
if self.distance == "near":
self.shade = randrange(255-30, 255)
self.char = choice([".", "'", ".",u"·" ])
# "█" ASCII 219
self.speed = 3
self.color = self.shade, self.shade, self.shade # XXX TODO figure out a better way to do this
elif self.distance == "mid":
self.shade = randrange(190-15, 190+15)
self.char = choice([ "!", ".", "'", "|", "*", "_", "'", ".", "'", "|", "*", "_", "'", u"†", "0"])
self.speed = 2
self.color = self.shade, self.shade, self.shade
elif self.distance == "far":
self.shade = randrange(100-15, 100+15)
self.char = choice(["_","-",".","'",".","'",".","'", "__", u"‡"])
self.speed = 1
self.color = self.shade, self.shade, self.shade
def getx(self):
return self.xcoord
def gety(self):
return self.ycoord
def draw(self):
self.font = pygame.font.Font(None, 12)
self.text = self.font.render(self.char, 1, self.color)
screen.blit(self.text, self.coords)
def check_collisions(self):
"Check if bottom"
if self.ycoord >= screen.get_height():
w.starfield.stars.remove(self)
star = Star(xcoord = randrange(0, screen.get_width() - 1),
ycoord = 0,
distance = choice(LAYERS))
w.starfield.stars.append(star)
def move(self):
self.ycoord += self.speed
self.coords = self.xcoord, self.ycoord
self.check_collisions()
def update(self):
self.move()
class LoadLevel:
def __init__(self, key):
self.level = LEVELS[key]
def draw(self):
print self.level
class Starfield:
def __init__(self):
self.max_stars = MAX_STARS
self.stars = []
for i in range(self.max_stars):
star = Star(xcoord = randrange(0, screen.get_width() - 1),
ycoord = randrange(0, screen.get_height() - 1),
distance = choice(LAYERS))
self.stars.append(star)
class Crosshairs:
"Cross hairs that follow the mouse and pulse blue"
def __init__(self):
self.blueval = 215
self.bluedir = -1.8
self.xcoord = 0
self.ycoord = 0
def draw(self, **options):
self.xcoord = options.get("xcoord", self.xcoord)
self.ycoord = options.get("ycoord", self.ycoord)
pygame.draw.line(screen, (0, 0, self.blueval), (self.xcoord,0), (self.xcoord,screen.get_height() - 1))
pygame.draw.line(screen, (0, 0, self.blueval), (0, self.ycoord), (screen.get_width() - 1, self.ycoord))
self.blueval += self.bluedir
if self.blueval >= 223 or self.blueval < 32: self.bluedir *= -1
def update(self, **options):
self.xcoord = options.get("xcoord", self.xcoord)
self.ycoord = options.get("ycoord", self.ycoord)
class World():
def __init__(self):
self.cross = Crosshairs()
self.starfield = Starfield()
# self.curlevel = LoadLevel("foo")
def draw(self):
screen.fill(BGCOLOR)
for star in self.starfield.stars:
star.draw()
# self.curlevel.draw()
self.cross.draw()
pygame.display.flip()
def updateObjects(self):
for star in self.starfield.stars:
star.update()
#################
## MAIN GAME LOOP
#################
def MainLoop(self):
running = 1
clock = pygame.time.Clock()
clock.tick(30) # 30 fps
while running:
self.event = pygame.event.poll()
if self.event.type == pygame.QUIT:
running = 0
elif self.event.type == pygame.MOUSEMOTION:
x, y = self.event.pos
self.cross.update(xcoord = x,ycoord = y)
self.updateObjects()
self.draw()
if __name__ == "__main__":
pygame.font.init()
w = World()
w.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment