ベイビーバンバンコマンド


ドラマ『3人のパパ』

昔見た『Three Men and a Baby』(1987)
と似てますが、次回予告を見る限り似てるのは1話だけっぽいです。赤ちゃんかわいいですねえ。

babyに関するコマンドがないかなあと思いaptで探してみました。

takk@deb8:~$ apt-cache search baby
dvdrip-queue - queue up dvd::rip projects
bambam - keyboard mashing and doodling game for babies
dictconv - convert a dictionary file type in another dictionary file type
guitarix - Rock guitar amplifier for Jack
ospics - Some images of operating system logos/mascots
goldendict - 機能豊富な辞書検索プログラム
takk@deb8:~$ 

こんなコマンドが見つかりました。
bambam – keyboard mashing and doodling game for babies
さっそくインストールして起動。

takk@deb8:~$ sudo apt-get install bambam

(snip)

takk@deb8:~$ bambam


ん〜。何でしょう。

でも、きっと、自分が赤ちゃんなら面白いゲームに違いありません。
ゲームは赤ちゃん向けでも、ソースは大人向けです。ソースを見てみます。

takk@deb8:~$ cd bambam-0.5+dfsg/
takk@deb8:~/bambam-0.5+dfsg$ ls
COPYING  README  bambam.6  bambam.py  data  debian  docs  extrasounds  icon.gif
takk@deb8:~/bambam-0.5+dfsg$ wc -l bambam.py
227 bambam.py
takk@deb8:~/bambam-0.5+dfsg$ 

ソースの量も少ないので、全文見てみます。

takk@deb8:~/bambam-0.5+dfsg$ cat -n bambam.py
     1	#!/usr/bin/env python
     2	#Copyright (C) 2007-2008 Don Brown 2010 Spike Burch <spikeb@gmail.com>
     3	#    This program is free software: you can redistribute it and/or modify
     4	#    it under the terms of the GNU General Public License as published by
     5	#    the Free Software Foundation, either version 3 of the License, or
     6	#    (at your option) any later version.
     7	#
     8	#    This program is distributed in the hope that it will be useful,
     9	#    but WITHOUT ANY WARRANTY; without even the implied warranty of
    10	#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11	#    GNU General Public License for more details.
    12	#
    13	#    You should have received a copy of the GNU General Public License
    14	#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15	
    16	import pygame, sys,os, random, string, glob
    17	import argparse
    18	import fnmatch
    19	from pygame.locals import * 
    20	
    21	# draw filled circle at mouse position
    22	def draw_dot():
    23	    r = 30
    24	    mousex, mousey = pygame.mouse.get_pos()
    25	    
    26	    dot = pygame.Surface((2 * r, 2 * r))
    27	    pygame.draw.circle(dot, get_color(), (r, r), r, 0)
    28	    dot.set_colorkey(0, pygame.RLEACCEL)
    29	    
    30	    screen.blit(dot, (mousex - r, mousey - r))
    31	
    32	
    33	# Return bright color varying over time
    34	def get_color():
    35	    col = Color('white');
    36	    
    37	    hue = pygame.time.get_ticks() / 50 % 360
    38	    col.hsva = (hue, 100, 100, 50)
    39	    
    40	    return Color(col.r, col.g, col.b)
    41	
    42	
    43	# Load image/, handling setting of the transparency color key
    44	def load_image(fullname, colorkey = None):
    45	    try:
    46	        image = pygame.image.load(fullname)
    47	    except pygame.error, message:
    48	        print "Cannot load image:", fullname
    49	        raise SystemExit, message
    50	    image = image.convert()
    51	    if colorkey is not None:
    52	        if colorkey is -1:
    53	            colorkey = image.get_at((0, 0))
    54	        image.set_colorkey(colorkey, RLEACCEL)
    55	    return image
    56	
    57	
    58	# Load sound file in data/
    59	def load_sound(name):
    60	    class NoneSound:
    61	        def play(self): pass
    62	    if not pygame.mixer:
    63	        return NoneSound()
    64	    try:
    65	        sound = pygame.mixer.Sound(name)
    66	    except pygame.error, message:
    67	        print "Cannot load sound:", name
    68	        raise SystemExit, message
    69	    return sound
    70	
    71	
    72	# Loads a list of sounds
    73	def load_sounds(lst):
    74	    result = []
    75	    global args
    76	    for name in lst:
    77	        if True in [fnmatch.fnmatch(name, p) for p in args.sound_blacklist]:
    78	            print "Skipping blacklisted sound:", name
    79	        else:
    80	            result.append(load_sound(name))
    81	    return result
    82	
    83	
    84	# Processes events
    85	def input(events, quit_pos):
    86	    global sequence, mouse_down, sound_muted
    87	    for event in events: 
    88	        if event.type == QUIT: 
    89	            sys.exit(0)
    90	        
    91	        # handle keydown event     
    92	        elif event.type == KEYDOWN:
    93	            # check for words like quit
    94	            if event.type == KEYDOWN:
    95	                if is_alpha(event.key):
    96	                    sequence += chr(event.key)
    97	                    if sequence.find('quit') > -1:
    98	                        sys.exit(0)
    99	                    elif sequence.find('unmute') > -1:
   100	                        sound_muted = False
   101	                        #pygame.mixer.unpause()
   102	                        sequence = ''
   103	                    elif sequence.find('mute') > -1:
   104	                        sound_muted = True
   105	                        pygame.mixer.fadeout(1000)
   106	                        sequence = ''
   107	            
   108	            # Clear the background 10% of the time
   109	            if random.randint(0, 10) == 1:
   110	                screen.blit(background, (0, 0))
   111	                pygame.display.flip()
   112	            
   113	            # play random sound
   114	            if not sound_muted:
   115	               sounds[random.randint(0, len(sounds) - 1)].play()
   116	            
   117	            # show images 
   118	            if is_alpha(event.key):
   119	                print_letter(event.key)
   120	            else:
   121	                print_image()
   122	            pygame.display.flip()
   123	            
   124	        # mouse motion
   125	        elif event.type == MOUSEMOTION :
   126	            if mouse_down:
   127	                draw_dot()
   128	                pygame.display.flip()
   129	        
   130	        # mouse button down
   131	        elif event.type == MOUSEBUTTONDOWN:
   132	            draw_dot()
   133	            mouse_down = True
   134	            pygame.display.flip()
   135	
   136	        # mouse button up
   137	        elif event.type == MOUSEBUTTONUP:
   138	            mouse_down = False
   139	        
   140	    return quit_pos
   141	
   142	
   143	# Prints an image at a random location
   144	def print_image():
   145	    #global swidth, sheigh
   146	    img = images[random.randint(0, len(images) - 1)]
   147	    w = random.randint(0, swidth  - img.get_width())
   148	    h = random.randint(0, sheight - img.get_height())
   149	    screen.blit(img, (w, h))
   150	
   151	# Is the key that was pressed alphanumeric
   152	def is_alpha(key):
   153	    return key < 255 and (chr(key) in string.letters or chr(key) in string.digits)
   154	
   155	# Prints a letter at a random location
   156	def print_letter(key):
   157	    global args
   158	    font = pygame.font.Font(None, 256)
   159	    if args.uppercase:
   160	        char = chr(key).upper()
   161	    else:
   162	        char = chr(key)
   163	    text = font.render(char, 1, colors[random.randint(0, len(colors) - 1)])
   164	    textpos = text.get_rect()
   165	    center = (textpos.width / 2, textpos.height / 2)
   166	    w = random.randint(0 + center[0], swidth - center[0])
   167	    h = random.randint(0 + center[1], sheight - center[1])
   168	    textpos.centerx = w
   169	    textpos.centery = h
   170	    screen.blit(text, textpos) 
   171	
   172	# Main application
   173	#
   174	parser = argparse.ArgumentParser(description='A keyboard mashing game for babies.')
   175	parser.add_argument('-u', '--uppercase', action='store_true', help='Whether to show UPPER-CASE letters.')
   176	parser.add_argument('--sound_blacklist', action='append', default=[], help='List of sound filename patterns to never play.')
   177	args = parser.parse_args()
   178	
   179	if not pygame.font: print 'Warning, fonts disabled'
   180	if not pygame.mixer: print 'Warning, sound disabled'
   181	 
   182	pygame.init()
   183	
   184	# figure out the install base to use with image and sound loading
   185	progInstallBase = '/usr/share/bambam' 
   186	
   187	# swith to full screen at current screen resolution 
   188	window = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
   189	
   190	# determine display resolution
   191	displayinfo = pygame.display.Info()
   192	swidth = displayinfo.current_w
   193	sheight = displayinfo.current_h
   194	
   195	pygame.display.set_caption('Bam Bam') 
   196	screen = pygame.display.get_surface() 
   197	
   198	background = pygame.Surface(screen.get_size())
   199	background = background.convert()
   200	background.fill((250, 250, 250))
   201	sequence = ""
   202	screen.blit(background, (0, 0))
   203	pygame.display.flip()
   204	
   205	mouse_down = False
   206	sound_muted = False
   207	
   208	def glob_data(pattern):
   209	    return glob.glob(os.path.join(progInstallBase, 'data', pattern))
   210	
   211	sounds = load_sounds(glob_data('*.wav'))
   212	
   213	colors = ((  0,   0, 255), (255,   0,   0), (255, 255,   0), 
   214	          (255,   0, 128), (  0,   0, 128), (  0, 255,   0), 
   215	          (255, 128,   0), (255,   0, 255), (  0, 255, 255)
   216	)
   217	
   218	images = [load_image(glob_data('chimp.bmp')[0], -1)]
   219	images.extend([load_image(name) for name in glob_data('*.gif')])
   220	
   221	quit_pos = 0
   222	
   223	clock = pygame.time.Clock()
   224	
   225	while True:
   226	    clock.tick(60)
   227	    quit_pos = input(pygame.event.get(), quit_pos)

pygame使っているんですね。
dataディレクトリに格納したgifファイルを使うので、自分で作った画像も使えるようです。

コメント

タイトルとURLをコピーしました