Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
225 lines (186 sloc) 7.93 KB
"""
A small puppet program designed for 2D puppets
@version: 1.0
@author: iAreNubcake
"""
import sys, os, pygame
import ConfigParser
from pygame.locals import *
import pygame.rect
import pygame.transform
class puppet(object):
def __init__(self):
pygame.init()
# Initiate surfaces
self.window = pygame.display.set_mode((640, 480))
pygame.display.set_caption('2D Puppet ')
self.screen = pygame.display.get_surface()
self.screen.convert()
# Get the puppet the user wants and load it
self.get_puppet()
# Start loop
self.main()
def get_puppet(self):
CP = ConfigParser.ConfigParser()
CP.read("puppet.ini")
self.puppet = CP.get("Puppet", "name")
self.load_config(self.puppet)
self.load_images(self.puppet, self.single_eyebrow, self.single_eye)
self.set_states()
def load_config(self, puppet):
CP = ConfigParser.ConfigParser()
CP.read("images/%s/config.cfg" % puppet)
# Load general properties
self.background_color = (CP.getint("General", "background_color_r"),CP.getint("General", "background_color_g"),CP.getint("General", "background_color_b"))
self.single_eyebrow = CP.getboolean("General", "single_eyebrow")
self.single_eye = CP.getboolean("General", "single_eye")
# Load facial properties
self.face_size = (CP.getint("Face", "face_width"),CP.getint("Face", "face_height"))
self.face_surface = pygame.surface.Surface(self.face_size)
self.face_surface.convert()
self.mouth_position = (CP.getint("Face", "mouth_position_x"),CP.getint("Face", "mouth_position_y"))
self.left_eye_position = (CP.getint("Face", "left_eye_position_x"),CP.getint("Face", "left_eye_position_y"))
self.right_eye_position = (CP.getint("Face", "right_eye_position_x"),CP.getint("Face", "right_eye_position_y"))
self.left_eyebrow_position = (CP.getint("Face", "left_eyebrow_position_x"),CP.getint("Face", "left_eyebrow_position_y"))
self.right_eyebrow_position = (CP.getint("Face", "right_eyebrow_position_x"),CP.getint("Face", "right_eyebrow_position_y"))
def load_images(self, puppet="mr-derp", single_eyebrow=True, single_eye=True):
# Eyebrow images
if single_eyebrow:
self.eyebrow_sad_l = pygame.image.load('images/%s/eyebrow/eyebrow_sad.png' % puppet)
self.eyebrow_happy_l = pygame.image.load('images/%s/eyebrow/eyebrow_happy.png' % puppet)
self.eyebrow_angry_l = pygame.image.load('images/%s/eyebrow/eyebrow_angry.png' % puppet)
self.eyebrow_sad_r = pygame.transform.flip(self.eyebrow_sad_l, True, False)
self.eyebrow_happy_r = pygame.transform.flip(self.eyebrow_happy_l, True, False)
self.eyebrow_angry_r = pygame.transform.flip(self.eyebrow_angry_l, True, False)
else:
self.eyebrow_sad_l = pygame.image.load('images/%s/eyebrow/eyebrow_sad_l.png' % puppet)
self.eyebrow_happy_l = pygame.image.load('images/%s/eyebrow/eyebrow_happy_l.png' % puppet)
self.eyebrow_angry_l = pygame.image.load('images/%s/eyebrow/eyebrow_angry_l.png' % puppet)
self.eyebrow_sad_r = pygame.image.load('images/%s/eyebrow/eyebrow_sad_r.png' % puppet)
self.eyebrow_happy_r = pygame.image.load('images/%s/eyebrow/eyebrow_happy_r.png' % puppet)
self.eyebrow_angry_r = pygame.image.load('images/%s/eyebrow/eyebrow_angry_r.png' % puppet)
# Eye images
if single_eye:
self.eye_open_l = pygame.image.load('images/%s/eye/eye_open.png' % puppet)
self.eye_closed_l = pygame.image.load('images/%s/eye/eye_closed.png' % puppet)
self.eye_open_r = pygame.transform.flip(self.eye_open_l, True, False)
self.eye_closed_r = pygame.transform.flip(self.eye_closed_l, True, False)
else:
self.eye_open_l = pygame.image.load('images/%s/eye/eye_open_l.png' % puppet)
self.eye_closed_l = pygame.image.load('images/%s/eye/eye_closed_l.png' % puppet)
self.eye_open_r = pygame.image.load('images/%s/eye/eye_open_r.png' % puppet)
self.eye_closed_r = pygame.image.load('images/%s/eye/eye_closed_r.png' % puppet)
# Mouth images
self.mouth_sad = pygame.image.load('images/%s/mouth/mouth_sad.png' % puppet)
self.mouth_happy = pygame.image.load('images/%s/mouth/mouth_happy.png' % puppet)
self.mouth_angry = pygame.image.load('images/%s/mouth/mouth_angry.png' % puppet)
self.mouth_big = pygame.image.load('images/%s/mouth/mouth_big.png' % puppet)
self.mouth_small = pygame.image.load('images/%s/mouth/mouth_small.png' % puppet)
# Face
self.face_image = pygame.image.load('images/%s/face.png' % puppet)
def set_states(self):
# Eyebrows
self.eyebrow_l = self.eyebrow_happy_l
self.eyebrow_r = self.eyebrow_happy_r
self.eyebrow_raise_l = [False]
self.eyebrow_raise_r = [False]
# Eyes
self.eye_is_closed_l = [False]
self.eye_is_closed_r = [False]
# Mouth
try:
self.mouth_state = self.mouth_state[0:1]
except:
self.mouth_state = [self.mouth_happy]
def handle_input(self, events):
for event in events:
# Check for quit
if event.type == KEYUP and event.key == K_ESCAPE or event.type == QUIT:
sys.exit(0)
# Check key presses
if event.type == KEYDOWN:
# Face State
if event.key == K_1:
self.eyebrow_l = self.eyebrow_sad_l
self.eyebrow_r = self.eyebrow_sad_r
self.mouth_state[0] = self.mouth_sad
elif event.key == K_2:
self.eyebrow_l = self.eyebrow_happy_l
self.eyebrow_r = self.eyebrow_happy_r
self.mouth_state[0] = self.mouth_happy
elif event.key == K_3:
self.eyebrow_l = self.eyebrow_angry_l
self.eyebrow_r = self.eyebrow_angry_r
self.mouth_state[0] = self.mouth_angry
# Eyebrow raising
elif event.key == K_q:
self.eyebrow_raise_l.append(True)
elif event.key == K_w:
self.eyebrow_raise_l.append(True)
self.eyebrow_raise_r.append(True)
elif event.key == K_e:
self.eyebrow_raise_r.append(True)
# Eyes
elif event.key == K_a:
self.eye_is_closed_l.append(True)
elif event.key == K_s:
self.eye_is_closed_l.append(True)
self.eye_is_closed_r.append(True)
elif event.key == K_d:
self.eye_is_closed_r.append(True)
# Mouth
elif event.key == K_z:
self.mouth_state.append(self.mouth_big)
elif event.key == K_x:
self.mouth_state.append(self.mouth_small)
# Check key releases
if event.type == KEYUP:
# Eyebrow raising
if event.key == K_q:
self.eyebrow_raise_l.remove(True)
elif event.key == K_w:
self.eyebrow_raise_l.remove(True)
self.eyebrow_raise_r.remove(True)
elif event.key == K_e:
self.eyebrow_raise_r.remove(True)
# Eyes
elif event.key == K_a:
self.eye_is_closed_l.remove(True)
elif event.key == K_s:
self.eye_is_closed_l.remove(True)
self.eye_is_closed_r.remove(True)
elif event.key == K_d:
self.eye_is_closed_r.remove(True)
# Mouth
elif event.key == K_z:
self.mouth_state.remove(self.mouth_big)
elif event.key == K_x:
self.mouth_state.remove(self.mouth_small)
def draw(self):
# Fill screen with desired background color
self.screen.fill(self.background_color)
self.face_surface.fill(self.background_color)
# Display face
self.face_surface.blit(self.face_image, (0, 0))
# Blit mouth
self.face_surface.blit(self.mouth_state[-1], self.mouth_position)
# Blit eyes
if self.eye_is_closed_l[-1]:
self.face_surface.blit(self.eye_closed_l, self.left_eye_position)
else:
self.face_surface.blit(self.eye_open_l, self.left_eye_position)
if self.eye_is_closed_r[-1]:
self.face_surface.blit(self.eye_closed_r, self.right_eye_position )
else:
self.face_surface.blit(self.eye_open_r, self.right_eye_position )
# Blit eyebrows
self.face_surface.blit(self.eyebrow_l, (self.left_eyebrow_position[0], self.left_eyebrow_position[1] - (self.eyebrow_raise_l[-1] * 6)))
self.face_surface.blit(self.eyebrow_r, (self.right_eyebrow_position[0], self.right_eyebrow_position[1] - (self.eyebrow_raise_r[-1] * 6)))
self.screen.blit(self.face_surface, ((self.screen.get_width() - self.face_size[0]) / 2, (self.screen.get_height() - self.face_size[1]) / 2))
pygame.display.flip()
def main(self):
while 1:
self.handle_input(pygame.event.get())
self.draw()
if __name__ == '__main__':
puppet()
Something went wrong with that request. Please try again.