Tutorial 02: Full sourcecode

#!/usr/bin/python

# This example runs in Python 2.6(w/wo psyco) and 3.1 using Pygame 1.9
# Using psycho - if installed, it almost cuts CPU usage in half
# This example builds on the stars.py example included in pygame
# Its located at: [python install]\Lib\site-packages\pygame\example

# Imports
import pygame
import random
from pygame.locals import * 

# Constants
WHITE   = 255, 240, 200
BLACK   =   0,   0,   0
RED     = 255,   0,   0
GREEN   =   0, 255,   0
BLUE    =   0,   0, 255
STAR_SIZE = 3
COLORS = [BLACK,WHITE,RED,GREEN,BLUE]   # Varies the stars colors
WIDTH   = 600                           # Use these values to adjust window size
HEIGHT  = 600
N_STAR = 100                            # Frequency of stars displayed


def initialize_stars():
    """
    Creates a list of stars, each star will receive a random pair of
    x and y coordinates for its position.
    """
    star_list = []
    for i in range(N_STAR):
        x = random.randrange(0,WIDTH)
        y = random.randrange(0,HEIGHT)
        star_list.append([x,y])
    
    return star_list


def draw_stars(surface, star_list):
    """
    This function takes a list of stars and displays them on the pygame surface.
    """
    for i in range(len(star_list)):
        starColor = random.randrange(0, 4)              # For white stars comment out
        color = COLORS[starColor]                       # For white stars set to WHITE
        #color = WHITE
        shimmerEffect = random.randrange(0,STAR_SIZE)
        pygame.draw.circle(surface, color, star_list[i],shimmerEffect)


def move_stars(surface, star_list, color):
    """
    This function generates the star movement, if the star has fallen below the
    height of the window, the star is recreated with new x and y coordinates, then
    added back into the list.
    """
    for i in range(len(star_list)):
        pygame.draw.circle(surface, color, star_list[i],STAR_SIZE)
        star_list[i][1] += 1
        # loop the top/bottom of the window
        if star_list[i][1] > HEIGHT:
            y = random.randrange(-HEIGHT/4, -HEIGHT/8)  # randomize before the star enters the window
            star_list[i][1] = y
            x = random.randrange(0,WIDTH)
            star_list[i][0] = x


def main():
    """
    This code assembles the starfield and sends it to pygame
    """
    clock = pygame.time.Clock()
    random.seed()
    stars = initialize_stars()
    pygame.init()                                       # Entry point to pygame
    screen = pygame.display.set_mode([WIDTH,HEIGHT])    # Create window
    pygame.display.set_caption('Tut02: Empty Space...') # Create window title
    screen.fill(BLACK)                                  # Create a "static" background
    while 1:                                            # The main game loop
        draw_stars(screen, stars)                       # Hide stars prior to move
        move_stars(screen, stars,BLACK)
        draw_stars(screen, stars)                       # Show stars after move
            
        pygame.display.update()
        for entry in pygame.event.get():                # Wait until input is generated
            if entry.type == QUIT or (entry.type == KEYUP and entry.key == K_ESCAPE):
                pygame.quit()
                return
                    
        clock.tick(30)


if __name__ == '__main__':      # Stand-alone execution
    main()

Advertisements
  1. Note about tutorials « Savalia Studios

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: