Pygame Starfield Rain
It rained yesterday afternoon, and I had the chance to observe raindrops descend from the edge of a roof while I gave an exam to some of my math students. While doing so, I thought that the effect that it gave might look good as a starfield in a space shooter. And so, we have this “starfield rain” demo:

Three things would probably make this implementation different from your usual starfield:
- The stars’ movement is influenced by constant acceleration, not by constant velocity (much like raindrops against gravity).
- This isn’t a parallax starfield, meaning, we do not have more than two layers of starfields with differing velocities. The fact that the velocity of each star changes at each frame should make up for this – a large number of velocities may be observed at any given time.
- The stars have trails, much like heavy raindrops. I think the trails add a nice touch.
I was able to write out this code in a little less than a couple of hours, and I am certain that someone with more programming experience would probably be able to produce something like this, or something of even better quality, in a few minutes.
I think that mini-coding exercises like this are a good thing for beginners – you don’t immediately go for Everest; instead, you gain experience by climbing a a lot of other mountains first.
The code (requires Python and Pygame to run) should be easy to understand, but I wasn’t able to add comments to it. I will probably discuss how it works in a future post.
But of course, in keeping with the spirit of the exercise, I encourage you to try to implement this on your own. Starting small should be helpful in managing bigger game programming projects later on.
I think we have the beginnings of a Pygame particle system here
.


October 18th, 2006 at 2:03 pm
Hey there, been following your blog for a while. I really like your tutorials, they’re very helpful to me. I’m working on a pygame of my own, I’d like to get your opinion on it if you don’t mind.
As for a particle system, have you heard of Verlet integration? Here’s the paper on it: http://www.teknikus.dk/tj/gdc2001.htm
It makes for really nice animations, and is nice because you only have to store the current location and old location of a particle.
October 19th, 2006 at 12:59 pm
this is really interesting. Can I use the code in my own game?
October 20th, 2006 at 6:45 am
Carlo: Thank you so much for visiting the site, and I’m glad you’ve found the tutorials helpful. Sure, I’d like to see what you’re working on
. Thanks for the link to the article. I’ve come across this article before, and I think it’s really interesting. Hopefully, I’ll be able to use the content of the article eventually to write something on game physics implementation. And yes, as you have pointed out, it should work well with a particle system. Thanks for pointing it out!
Dave: Sure! Feel free to use the code as you wish.
October 20th, 2006 at 9:22 pm
Thanks!
I have read over the code a few times and i’ve figured out how it works. I have one question. When you look at the rain droplets, you see that they are square shaped. Is there a way to make them circular?
October 21st, 2006 at 4:27 am
The droplet’s appearance is determined by the dropimage() function. To make the drop circular, you would probably have to change the line with the fill into this:
pygame.draw.circle(image, (255, 255, 255), (1, 1), 2)
Here, we use pygame’s draw module to draw a white circle at the center of the “droplet surface” with radius 2, which should just about fill the “droplet surface”.
Hope this helps.
October 21st, 2006 at 1:20 pm
typing in comments like this is annoying. I think you should start a simple free forum. There is al ist of software here: http://thinkofit.com/webconf/forumsoft.htm
October 21st, 2006 at 9:20 pm
Thanks for the suggestion, Dave. I might consider putting up a forum sometime in the future, but for now, I would like to focus my efforts on adding content to this site. Thank you again!
October 24th, 2006 at 3:38 am
hehe, scriptfun may be the future community of game creators who share their codes and advices.. python/pygame is a language who permit this way.
good luck Chuck !
October 26th, 2006 at 11:22 am
I thought your rain generator was interesting. Here’s a short version, that goes 10x faster, and still uses dirty rectangles. Enjoy!
import pygame
import random
import time
SCREENSIZE = 640, 480
class Rain(object):
‘ Rain generator’
drops = []
height = 160
speed = 1
color = (255, 255, 255, 255)
chance = .05
def __init__(self, **kwargs):
‘ Allow programmer to change settings of rain generator’
self.__dict__.update(kwargs)
def Render(self, screen):
‘ Render the rain’
dirtyrects = []
for drop in self.drops:
dr = drop.Render(dirtyrects, screen)
if drop.dead:
self.drops.remove(drop)
dirtyrects.append(dr)
if random.random() self.maxy:
self.dead = 1
else:
screen.blit(self.pic, self.pos)
return r
def main():
# Initialize pygame
pygame.init()
screen = pygame.display.set_mode(SCREENSIZE, 0, 32)
# Create rain generator
rain = Rain()
# Main loop
nexttime = time.time()
ctr = 0
quit = 0
while not quit:
# Uncomment the following line to make the rain go slower
#time.sleep(.01)
# Track FPS
if time.time() > nexttime:
nexttime = time.time() + 1
print ‘%d fps’ % ctr
ctr = 0
ctr += 1
# Draw rain
screen.fill((0, 0, 0, 0))
r = rain.Render(screen)
# Look for user quit
pygame.display.update(r)
pygame.event.pump()
for e in pygame.event.get():
if e.type in [pygame.QUIT, pygame.KEYDOWN, pygame.MOUSEBUTTONDOWN]:
quit = 1
break
# Terminate pygame
pygame.quit()
if __name__ == “__main__”:
main()
October 26th, 2006 at 11:26 am
Since the forum ate my spaces, here’s a link to the smaller faster rain generator:
http://incarnation.danbo.com/ubbthreads/showflat.php?Cat=0&Number=10483&an=0&page=0#Post10483
[url]http://incarnation.danbo.com/ubbthreads/showflat.php?Cat=0&Number=10483&an=0&page=0#Post10483[/url]