Previous posts for this project can be found here:
I want to blog a little about the pumpkin mainly becuase I need to get the design nailed down while I wait for my hardware to arrive.
The pumpkin will play at random, pre-recorded sounds. When a sound plays, the eyes and mouth move around. When a
sound ends, the eyes will still move but the mouth wont.
Lets start with the eyes.
The eyes will blink randomly and if a user "presses" the eye it will squint. This means I need a set of images
that can be "flipped" to imitate the motions.
As an example, a blink is a series of images that are played in order. This means I need a method of loadig up images and storing them, as well as keeping them in order.
So what I will do is create a file structure that keeps everything nice and tidy. For now I will only have two
actions for the eye which will be blink and squint.
I propose the following directories:
faces/
faces/<name>/eye/
faces/<name>/left/
faces/<name>/right/
faces/<name>/mouth/
faces/<name>/sounds/
The <name> will be the name of the face, as I want to be able to load multiple faces. The default face must exists
and be called 'default'.
Within each eye folder, a set of files is needed. I propose the following:
faces/<name>/eye/left/eye.jpg
faces/<name>/eye/left/blink1.jpg
faces/<name>/eye/left/blink2.jpg
...etc
faces/<name>/eye/left/squint1.jpg
faces/<name>/eye/left/squint2.jpg
...etc
The same structure exists for the right eye and the other structures. When animating, the images are flipped in
order when the action is needed.
Having these common directories allows searching and loading of faces to be easier. For now I will use only the
default face for my development.
With the directory in place, now we need to load all the face components which includes the eyes, mouth and sounds.
This will require the use of a class for each face to keep everything together.
Lets start with an easy example on how to load up all the squint images for the default face.
import os import pygame,sys leftEyeSquint = list() for dirname, dirnames, filenames in os.walk('./eye/default/left/squint/'): for filename in sorted(filenames): try: leftEyeSquint.append( pygame.image.load(os.path.join(dirname, filename))) except: print(filename + ": invalid file") print str(len(leftEyeSquint)) + " Squint images loaded\n"
This gives us a list with an ordered set of squint images. It should be known that any fiels in the squint
directory will be attempted to be loaded as images, this is why an exception clause exists.
This function is a generic function for loading images for the left eye squint. It does not need to be hard coded for the left eye as it can be used for all the face images.
So I will make it part of a Face Class. This class will have this generic method of loading images given a path.
Here is the complete code for loading up all the components for the eyes and storing the images in a list that is part of a class.
import os import pygame,sys #define the face class, which is just used to keep all the images together that go together class Face: def __init__(self,path): #load each component of the eyes using the generic image loading function self.leftEyeSquint = self.LoadImages(os.path.join(path, 'eye/left/squint/')) self.leftEyeBlink = self.LoadImages(os.path.join(path , 'eye/left/blink/')) self.rightEyeSquint = self.LoadImages(os.path.join(path , 'eye/right/squint/')) self.rightEyeBlink = self.LoadImages(os.path.join(path , 'eye/right/blink/')) #a generic function for loading a set of images from a given directory def LoadImages(self,imagesPath): imageList = list() for dirname, dirnames, filenames in os.walk(imagesPath): for filename in sorted(filenames): try: imageList.append( pygame.image.load(os.path.join(dirname, filename))) except: pass return imageList #Create a list of faces classes, this example only has 1 face, but multiple faces can be used faces = list() #load the default face faces.append(Face('./faces/default/')) #test the class print str(len(faces[0].leftEyeSquint)) + " Left squint images in class\n" print str(len(faces[0].rightEyeSquint)) + " Right squint images in class\n"
The same pattern will be used for the mouth and sounds. The sound function will call a different method of the pygame but the logic remains the same. The class will be fully filled out and the a list of classes will be used in the main animation loop.
Top Comments