Posts Tagged algorithms

Algorithm: Lightning

Another simple graphical trick you can play with in games is lightning.  One algorithm for this is to draw a white square over the entire bounds of the screen for a very short time.  The following code can be used to flash every second:

#Lightning.py
#Flash of lightning visual effect.

import pygame

from engine.AtlanSprite import AtlanSprite

class Lightning(AtlanSprite):

“”"
AtlanSprite is one of my game engine classes.
Replace this with your own basic sprite class.
“”"

def __init__(self, m):

“”"
Constructor – set up the counter variable and bounds.
m = whatever container class you are using
“”"
AtlanSprite.__init__(self)
#make the bounds however large your screen size is
self.bounds = pygame.Rect(0, 0, 800, 600)
self.master = m
self.counter = 0
self.flashing = False

def draw(self, surface):

“”"
Called from the main game loop every frame.
Draw a white square when the lightning is flashing.
“”"

if self.flashing:

#see pygame.org’s documentation on the surface.fill() method
surface.fill((255, 255, 255), self.bounds)

def update(self, time):

“”"
Should be called every game frame with the time that has ellapsed.
time = time elapsed in milliseconds

“”"

self.counter = self.counter + time
#flash every second

if self.counter >= 1000:

if not self.flashing:

self.flashing = True

#only flash for 50 milliseconds

if self.counter >= 1050:

self.flashing = False
self.counter = 0

This works best if you have a thunder sound effect to go with it.  The reason it works is that the square is drawn so quickly that the human eye can’t really tell what is going on.  All you see is everything get brighter so it looks like lightning flashed.  Note that you’ll probably want to draw the lightning last in your list of sprites so that it doesn’t get covered up by anything else.

Tags: , , ,

Algorithm: Procedural Clouds

Lately, I’ve been working on a game idea where you guide a sailboat through dangerous waters by controlling the wind.  It’s not quite ready for primetime just yet, but today I wanted to share a code snippet for the procedural generation of 2D clouds that I’m using in it.

Clouds

Clouds

Designing games is a lot like being a magician:  you provide illusions for other people’s entertainment.  You want your player to think that a collection of pixels on a screen is really a cloud.  What do clouds look like in real life?  They are white, fluffy, semi-transparent, and each one looks unique.  To pull off the magic trick, you need an algorithm to produce objects that satisfy all of these conditions.

You also want to do it in a way that is easy on their processor.  My solution is to create a surface and draw a lot of small circles on it in pseudorandom positions.  It seems to work pretty well.  Below is the Python code for the algorithm (using the Pygame module):

#uses the random and pygame modules

#build the cloud at location 100, 100.  Note that for a game you’d probably want to randomize its location

bounds = pygame.Rect(100, 100, random.randint(50, 300), random.randint(50, 300))

#create a surface with an alpha channel

cloudSurface = pygame.Surface((bounds.width, bounds.height), flags=pygame.SRCALPHA).convert_alpha()

#drawing to locked surfaces is more efficient

cloudSurface.lock()

center = (bounds.x + (bounds.width / 2), bounds.y + (bounds.height / 2))

#little bit of black magic here – I found out how many circles to draw through trial and error

#this value seems to work reasonably well

for i in xrange((bounds.width * bounds.height) / 200):

#draw circles randomly in 30% to 70% range of the rectangle

centerX = random.randint(int(bounds.width * 0.3), int(bounds.width * 0.7))

centerY = random.randint(int(bounds.height * 0.3), int(bounds.height * 0.7))

#circles should have a radius between 3 and 10

radius = random.randint(3, 10)

#draw the circle to the surface, make it halfway transparent and white

pygame.draw.circle(cloudSurface, (255, 255, 255, 128), (centerX, centerY), radius)

cloudSurface.unlock()

Now that you’ve created the surface, blit it in your draw loop.  If you’re not satisfied with the clouds this algorithm draws, mess with the constants I’ve used a little.  Try changing the size of the circles drawn, how many are drawn, and in what range of the rectangle they are drawn.

Tags: , , ,

Be Cool, Stay in School

So you’re sitting in your high school math class, programming a sweet text based adventure on your TI-83+.  The teacher at the front of the room is droning on about cosines and parallel lines.  Who cares, you think.  I just want to make games.  Well, here are a few math subjects you should definiately pay attention to if you want to be a game programmer:

  • Matrix operations – If you want to learn 3D programming, you need to be real familiar with matrices.  OpenGL, the 3D standard, is a whole heap of matrix math and if you want it to make sense you need to know your stuff.
  • Probability – Randomness is huge for RPG’s and strategic AI.  Why won’t those random battles happen as often as you want them to?  Probably (ha) because you got your terms mixed up.
  • Geometry – Trigonometric functions, area calculations, and the intersections between rectangles are vital components for 2D physics engines.  Make sure Mario jumps with a height of sine(angle) * jump speed and not cosine(angle) * jump speed. :)
  • Algorithms – Be able to tell the difference between Prim and Kruskal.  Know how to sort, how to search.  These functions come up all the time in unexpected ways.  Oh, and never ever use stupidsort.

While libraries exist for all of these subject areas, you need to be familiar with the terms at a bare minimum.  Plus, one day you might be writing a library and you’ll be the one to have to go back and dig through your old dusty textbooks and figure out how things are done.  Study hard!

Tags: , , , ,