Created
June 5, 2016 21:56
-
-
Save anonymous/222b176a0466f850c37d469c7417eb7e to your computer and use it in GitHub Desktop.
Python Stars
This file contains 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
""" | |
Python (ve Pygame) Starfield | |
Made by Admicos | |
""" | |
from __future__ import print_function | |
from random import randint | |
#from pygame import gfxdraw | |
import pygame | |
width = 800 | |
height = 600 | |
spread = 20 | |
count = 4096 | |
speed = 20 | |
deltaSpeed = 5.0 | |
starX = [None] * count | |
starY = [None] * count | |
starZ = [None] * count | |
def init_stars(): | |
for i in range(0, count): | |
new_star(i) | |
def render_stars(display, delta): | |
hWidth = width / 2 | |
hHeight = height / 2 | |
for i in range(0, count): | |
starZ[i] -= delta * speed | |
if starZ[i] <= 0: | |
new_star(i) | |
x = (starX[i] / starZ[i]) * hWidth + hWidth | |
y = (starY[i] / starZ[i]) * hHeight + hHeight | |
if x < 0 or x >= width or y < 0 or y >= height: | |
new_star(i) | |
else: | |
#display.set_at((x, y), (255, 255, 255)) | |
display.fill((255, 255, 255), ((x, y), (1, 1))) | |
def new_star(index): | |
starX[index] = randint(-width, width) * spread | |
starY[index] = randint(-height, height) * spread | |
starZ[index] = randint(1, width) * spread | |
def main(): | |
pygame.init() | |
display = pygame.display.set_mode((width, height)) | |
clock = pygame.time.Clock() | |
init_stars() | |
running = True | |
last_ticks = pygame.time.get_ticks() | |
while running: | |
ticks = pygame.time.get_ticks() | |
delta = (ticks - last_ticks) / deltaSpeed | |
last_ticks = ticks | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
running = False | |
display.fill((0, 0, 0)) | |
render_stars(display, delta) | |
pygame.display.flip() | |
# clock.tick(60) | |
return 0 | |
if __name__ == '__main__': | |
exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment