pygame / examples /

ahvezda 99d32cc 

#!/usr/bin/env python

import time, random, math

import pygame
from pygame.locals import *

WINSIZE = [640, 480]
WINCENTER = [320, 240]

def init_star():
	"creates new star values"
	dir = random.randrange(100000)
	velmult = random.random()*.6+.4
	vel = [math.sin(dir) * velmult, math.cos(dir) * velmult]
	return vel, WINCENTER[:]

def initialize_stars():
	"creates a new starfield"
	stars = []
	for x in range(NUMSTARS):
		star = init_star()
		vel, pos = star
		steps = random.randint(0, WINCENTER[0])
		pos[0] += vel[0] * steps
		pos[1] += vel[1] * steps
		vel[0] *= steps * .09
		vel[1] *= steps * .09
	return stars

def draw_stars(surface, stars, color):
	"used to draw (and clear) the stars"
	for vel, pos in stars:
		surface.set_at(pos, color)

def move_stars(stars):
	"animate the star values"
	for vel, pos in stars:
		pos[0] += vel[0]
		pos[1] += vel[1]
		if pos[0] < 0 or pos[1] < 0 or pos[0] >= WINSIZE[0] or pos[1] >= WINSIZE[1]:
			vel[:], pos[:] = init_star()
			vel[0] *= 1.05
			vel[1] *= 1.05

def main():
	"This is the starfield code"
	#create our starfield
	stars = initialize_stars()

	#initialize and prepare screen
	screen = pygame.display.set_mode(WINSIZE)
	pygame.display.set_caption('pyGame Stars Example')
	white = screen.map_rgb(255, 240, 200)
	black = screen.map_rgb(20, 20, 40)

	#main game loop
	done = 0
	while not done:
		draw_stars(screen, stars, black)
		draw_stars(screen, stars, white)

		for e in pygame.event.get():
			if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
				done = 1
			elif e.type == MOUSEBUTTONDOWN and e.button == 1:
				WINCENTER[:] = list(e.pos)

# if python says run, then we should run
if __name__ == '__main__':
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.