Littluns

Littluns Commit Details

Date:2009-12-21 18:25:43 (3 years 5 months ago)
Author:Ciaran Gultnieks
Branch:master
Commit:b2b2e2f1d57283531928ebeae2442ed0597e7d0d
Parents: ba74153520962850028eabafe34649fdba90ef52
Message:Reformatted code according to PEP 8

Changes:
MREADME (1 diff)
Mactivity.py (5 diffs)
Mcheese.py (4 diffs)
Mcrayon.py (4 diffs)
Mkeys.py (2 diffs)
Mlittluns.py (1 diff)
Mmenu.py (6 diffs)

File differences

README
11
22
3
3
4
5
6
7
8
9
10
11
12
13
14
15
416
keys.svg - http://commons.wikimedia.org/wiki/File:Input-keyboard.svg
== Media ==
Much of the images and sounds come from the original pyToddler distribution.
Additionally, the following come from external sources:
keys.svg - http://commons.wikimedia.org/wiki/File:Input-keyboard.svg
== Coding Standards ==
To encourage collaboration and improve readability for the widest possible
audience, all code follows the PEP-8 standard as closely as possible. See:
http://www.python.org/dev/peps/pep-0008/
activity.py
1717
1818
1919
20
20
21
2122
2223
2324
2425
26
2527
2628
2729
2830
2931
3032
31
33
3234
3335
3436
3537
38
3639
40
3741
38
42
43
44
45
3946
4047
4148
......
4350
4451
4552
46
53
4754
4855
56
4957
58
5059
5160
5261
......
6271
6372
6473
65
74
6675
6776
6877
......
7180
7281
7382
74
83
7584
7685
7786
......
92101
93102
94103
95
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import os, sys
import os
import sys
import pygame
path = os.path.dirname(os.path.abspath(sys.argv[0]))
def load_image(name, colorkey=None):
fullname = os.path.join(path, 'data', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', name
raise SystemExit, message
sys.exit(1)
image = image.convert_alpha()
return image, image.get_rect()
def load_sound(name):
class NoneSound:
def play(self): pass
def play(self):
pass
if not pygame.mixer:
return NoneSound()
fullname = os.path.join(path, 'data', name)
sound = pygame.mixer.Sound(fullname)
except pygame.error, message:
print 'Cannot load sound:', name
raise SystemExit, message
sys.exit(1)
return sound
class Activity:
def __init__(self, screen):
self.screen = screen
self.mprev = None
def setup_background(self):
self.background.fill((0,0,0))
self.background.fill((0, 0, 0))
def load_image(self, name, colorkey=None):
return load_image(name, colorkey)
return load_sound(name)
def run(self):
self.screen.blit(self.background, (0,0))
self.screen.blit(self.background, (0, 0))
self.setup()
pygame.display.flip()
while True:
def on_change(self):
pass
cheese.py
1717
1818
1919
20
20
21
2122
2223
2324
2425
25
26
27
26
27
28
29
30
31
32
33
34
2835
2936
3037
3138
3239
40
3341
42
3443
35
44
3645
37
38
46
47
48
49
50
3951
4052
4153
......
4456
4557
4658
47
59
60
4861
4962
50
63
64
5165
5266
5367
5468
5569
70
5671
72
5773
58
74
5975
60
76
6177
6278
6379
6480
81
6582
6683
84
6785
68
69
70
86
87
88
89
7190
7291
92
7393
7494
7595
......
7999
80100
81101
82
102
83103
84104
85
105
86106
87107
88108
......
111131
112132
113133
114
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import pygame, random
import pygame
import random
from pygame.locals import *
from activity import *
themes = [
{'mouse':'mouse.png','cheese':'cheese.png','mouth':'bottom','shrink':0},
{'mouse':'mouse_cz.png','cheese':'cheese_cz.png','mouth':'top','shrink':-20}
]
{'mouse': 'mouse.png',
'cheese': 'cheese.png',
'mouth': 'bottom',
'shrink': 0},
{'mouse': 'mouse_cz.png',
'cheese': 'cheese_cz.png',
'mouth': 'top',
'shrink': -20}]
THEME = themes[1]
if len(sys.argv) > 1:
THEME = themes[0]
MAKECHEESE = USEREVENT + 1
class Mouse(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image(THEME['mouse'])
self.mouth = pygame.Rect((0,0),(80,100))
self.yums = [load_sound('yum1.ogg'),load_sound('yum2.ogg'),load_sound('slurp.ogg')]
self.mouth = pygame.Rect((0, 0), (80, 100))
self.yums = [load_sound('yum1.ogg'),
load_sound('yum2.ogg'),
load_sound('slurp.ogg')]
def update(self):
pos = pygame.mouse.get_pos()
if THEME['mouth'] == 'bottom':
else:
self.rect.midtop = pos
self.mouth.midtop = self.rect.midtop
def eat(self,group):
def eat(self, group):
slurp = False
for x in group.sprites():
if self.mouth.colliderect(x.rect.inflate(THEME['shrink'],THEME['shrink'])):
colrect = x.rect.inflate(THEME['shrink'], THEME['shrink'])
if self.mouth.colliderect(colrect):
x.remove(group)
slurp = True
if slurp:
random.choice(self.yums).play()
class Cheese(pygame.sprite.Sprite):
def __init__(self, screen):
pygame.sprite.Sprite.__init__(self)
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image(THEME['cheese'])
self.pos = (-self.rect.width,-self.rect.height)
self.pos = (-self.rect.width, -self.rect.height)
self.x, self.y = screen.get_size()
self.x = self.x - self.rect.width
self.y = self.y - self.rect.height
def update(self):
self.rect.center = self.pos
def change(self):
x = int((random.random()*self.x)+(self.rect.width/2.0))
y = int((random.random()*self.y)+(self.rect.height/2.0))
self.pos = (x,y)
x = int((random.random() * self.x) + (self.rect.width / 2.0))
y = int((random.random() * self.y) + (self.rect.height / 2.0))
self.pos = (x, y)
class CheeseActivity(Activity):
def __init__(self, screen):
Activity.__init__(self, screen)
self.mousey = Mouse()
self.pos = None
def setup(self):
pygame.time.set_timer(MAKECHEESE,3000)
pygame.time.set_timer(MAKECHEESE, 3000)
def setup_background(self):
self.background.fill((200,200,200))
self.background.fill((200, 200, 200))
def handle_events(self):
event = pygame.event.wait()
self.mouse.draw(self.screen)
self.mprev = self.pos
crayon.py
2121
2222
2323
24
2425
26
2527
26
27
28
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2944
3045
3146
3247
3348
49
3450
3551
36
37
38
39
40
41
42
43
44
45
46
47
48
52
53
54
55
56
57
58
59
60
61
62
63
4964
5065
5166
......
5974
6075
6176
62
63
64
65
77
78
79
80
81
6682
6783
6884
6985
86
7087
7188
89
7290
7391
92
7493
7594
7695
7796
7897
98
7999
80100
81101
82102
83103
104
84105
85106
86
87
88
107
108
109
110
89111
90112
91113
......
97119
98120
99121
100
122
101123
102124
103125
......
108130
109131
110132
133
111134
112135
113136
114
137
138
115139
116140
117141
118142
119
from pygame.locals import *
from activity import *
class Crayon(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.ptlist = [(12,3),(3,7),(11,68),(4,77),(15,99),(77,70),(66,47),(54,47)]
self.endlist = [(138,355),(150,380),(167,380),(182,376),(196,368),(211,351),(199,325)]
pygame.sprite.Sprite.__init__(self)
self.ptlist = [(12, 3),
(3, 7),
(11, 68),
(4, 77),
(15, 99),
(77, 70),
(66, 47),
(54, 47)]
self.endlist = [(138, 355),
(150, 380),
(167, 380),
(182, 376),
(196, 368),
(211, 351),
(199, 325)]
self.sounds = {}
self.changed = False
self.cover, self.rect = load_image('crayon.png')
self.image, self.rect = load_image('crayon.png')
self.init_colors()
def init_colors(self):
self.index = 0
self.colors = [
((255,0,0),'red'),
((245,121,0),'orange'),
((255,255,0),'yellow'),
((0,200,0),'green'),
((0,0,255),'blue'),
((128,0,128),'purple'),
((80,60,20),'brown'),
((0,0,0),'black'),
((128,128,128),'gray'),
((254,254,254),'white'),
((255,0,128),'pink')
]
self.colors = [
((255, 0, 0), 'red'),
((245, 121, 0), 'orange'),
((255, 255, 0), 'yellow'),
((0, 200, 0), 'green'),
((0, 0, 255), 'blue'),
((128, 0, 128), 'purple'),
((80, 60, 20), 'brown'),
((0, 0, 0), 'black'),
((128, 128, 128), 'gray'),
((254, 254, 254), 'white'),
((255, 0, 128), 'pink')]
self.change(0)
def update(self):
self.index = len(self.colors) - 1
elif self.index >= len(self.colors):
self.index = 0
pygame.draw.polygon(self.image, self.color(),self.ptlist)
pygame.draw.polygon(self.image, self.color(),self.endlist)
self.image.blit(self.cover,(0,0))
self.sounds.setdefault(self.name(),load_sound('%s.ogg' % self.name())).play()
pygame.draw.polygon(self.image, self.color(), self.ptlist)
pygame.draw.polygon(self.image, self.color(), self.endlist)
self.image.blit(self.cover, (0, 0))
newsound = load_sound('%s.ogg' % self.name())
self.sounds.setdefault(self.name(), newsound).play()
self.changed = True
def up(self):
self.change(1)
def down(self):
self.change(-1)
def color(self):
return self.colors[self.index][0]
def name(self):
return self.colors[self.index][1]
class CrayonActivity(Activity):
def __init__(self, screen):
Activity.__init__(self, screen)
self.crayon = Crayon()
self.allsprites = pygame.sprite.RenderPlain((self.crayon))
self.pos = None
def line(self, surf, begin, end, color):
size = 20
pygame.draw.circle(surf, color, begin, size-1)
pygame.draw.line(surf, color, begin, end, 2*size)
pygame.draw.circle(surf, color, end, size-1)
pygame.draw.circle(surf, color, begin, size - 1)
pygame.draw.line(surf, color, begin, end, 2 * size)
pygame.draw.circle(surf, color, end, size - 1)
def handle_events(self):
event = pygame.event.wait()
if event.type == QUIT:
self.quit = True
return
elif event.key == K_n and KMOD_CTRL & event.mod:
self.background.fill((0,0,0))
self.background.fill((0, 0, 0))
self.changed = True
elif event.type == MOUSEBUTTONUP:
if event.button == 1:
self.pos = pygame.mouse.get_pos()
if self.pos != self.mprev:
self.changed = True
def on_change(self):
if self.pos != self.mprev:
if self.mprev:
self.line(self.background, self.mprev, self.pos, self.crayon.color())
self.line(self.background, self.mprev, self.pos,
self.crayon.color())
self.mprev = self.pos
self.allsprites.update()
self.screen.blit(self.background, (0, 0))
self.allsprites.draw(self.screen)
keys.py
1717
1818
1919
20
20
21
22
2123
2224
2325
2426
27
2528
29
2630
2731
2832
2933
30
34
35
36
37
38
39
40
41
42
43
3144
3245
3346
47
3448
3549
50
3651
3752
3853
......
5368
5469
5570
56
71
72
5773
5874
5975
6076
6177
62
63
64
78
79
80
81
82
83
6584
6685
6786
87
6888
6989
7090
7191
7292
73
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import random, re, string
import random
import re
import string
import pygame
from pygame.locals import *
from activity import Activity
class KeyActivity(Activity):
def __init__(self, screen):
Activity.__init__(self, screen)
self.sounds = {}
self.sound = None
self.kp_dict = {K_KP0:"0",K_KP1:"1",K_KP2:"2",K_KP3:"3",K_KP4:"4",K_KP5:"5",K_KP6:"6",K_KP7:"7",K_KP8:"8",K_KP9:"9"}
self.kp_dict = {K_KP0: "0",
K_KP1: "1",
K_KP2: "2",
K_KP3: "3",
K_KP4: "4",
K_KP5: "5",
K_KP6: "6",
K_KP7: "7",
K_KP8: "8",
K_KP9: "9"}
self.kp_set = set(self.kp_dict.keys())
self.test = re.compile('^[0-9A-Za-z]$')
self.change = self.noop
def noop(self):
pass
def handle_events(self):
self.sound = None
self.change = self.noop
key = pygame.key.name(event.key)
if self.test.match(key):
self.sound = self.sounds.setdefault(key.lower(),self.load_sound('%s.ogg' % key))
newsound = self.load_sound('%s.ogg' % key)
self.sound = self.sounds.setdefault(key.lower(), newsound)
if KMOD_CAPS & event.mod:
key = key.upper()
if KMOD_NUM & event.mod and key in string.ascii_letters:
key = key.upper() + key.lower()
if pygame.font:
font = pygame.font.Font(None, int(self.background.get_height()*0.9))
text = font.render(key, 1, (255,255,255))
textpos = (self.center[0]-int(text.get_width()/2),self.center[1]-int(text.get_height()/2))
font = pygame.font.Font(None,
int(self.background.get_height() * 0.9))
text = font.render(key, 1, (255, 255, 255))
textpos = (self.center[0] - int(text.get_width() / 2),
self.center[1] - int(text.get_height() / 2))
def op():
self.screen.blit(text, textpos)
self.change = op
def on_change(self):
if self.change:
self.change()
if self.sound:
self.sound.play()
littluns.py
2828
2929
3030
31
31
3232
3333
3434
3535
3636
37
38
39
37
38
4039
4140
42
pygame.init()
screen=pygame.display.set_mode((0,0),pygame.FULLSCREEN)
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('littluns')
pygame.mouse.set_visible(0)
items = [
MenuItem('keys.png', 'keys-highlight.png', KeyActivity),
MenuItem('crayon.png', 'crayon-highlight.png', CrayonActivity),
MenuItem('mouse.png', 'mouse-highlight.png', CheeseActivity)
]
MenuItem('crayon.png', 'crayon-highlight.png', CrayonActivity),
MenuItem('mouse.png', 'mouse-highlight.png', CheeseActivity)]
MenuActivity(screen, items).run()
menu.py
1818
1919
2020
21
21
22
2223
2324
2425
26
2527
28
2629
2730
2831
......
3134
3235
3336
34
37
38
3539
3640
3741
38
42
43
3944
4045
46
4147
4248
4349
50
4451
4552
4653
4754
4855
4956
57
5058
59
5160
52
61
5362
63
5464
5565
5666
5767
68
5869
70
5971
6072
6173
......
7688
7789
7890
79
91
8092
8193
8294
8395
84
96
8597
8698
8799
......
94106
95107
96108
97
109
110
98111
99112
100113
......
107120
108121
109122
110
123
124
111125
112126
113127
......
127141
128142
129143
130
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import pygame, math
import pygame
import math
from pygame.locals import *
from activity import *
class MenuItem(pygame.sprite.Sprite):
def __init__(self, image, highlightimage, activity):
pygame.sprite.Sprite.__init__(self)
self.normalimage, self.rect = load_image(image)
else:
self.highlightimage, hrect = load_image(highlightimage)
if hrect != self.rect:
print "Error: Normal and highlight image for menu items should be the same size"
print "Error: Normal and highlight image for menu items " + \
"should be the same size"
sys.exit(1)
self.image = self.normalimage
self.activity = activity
self.pos = (-self.rect.width,-self.rect.height)
self.pos = (-self.rect.width, -self.rect.height)
def run(self, screen):
self.activity(screen).run()
def place(self, x, y):
self.pos = (x, y)
self.rect.center = self.pos
def highlight(self, state):
if state:
self.image = self.highlightimage
else:
self.image = self.normalimage
class Finger(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('finger.png')
def update(self):
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
class MenuActivity(Activity):
def __init__(self, screen, items):
Activity.__init__(self, screen)
self.finger = Finger()
self.Cursor = pygame.sprite.RenderPlain((self.finger))
self.pos = None
self.highlightedactivity = None
def setup_background(self):
self.background.fill((200,200,200))
self.background.fill((200, 200, 200))
def handle_events(self):
event = pygame.event.wait()
return
elif event.type == MOUSEBUTTONDOWN:
if self.highlightedactivity != None:
pygame.mouse.set_pos(self.screen.get_width() / 2, self.screen.get_height() / 2)
pygame.mouse.set_pos(self.screen.get_width() / 2,
self.screen.get_height() / 2)
self.highlightedactivity.run(self.screen)
self.pos = pygame.mouse.get_pos()
#Figure out which menu item should be highlighted - i.e. which one the
#finger is pointing at.
newhighlight = None
for x in pygame.sprite.spritecollide(self.finger, self.activities, False):
for x in pygame.sprite.spritecollide(self.finger, self.activities,
False):
newhighlight = x
break
self.Cursor.draw(self.screen)
self.mprev = self.pos

Archive Download the corresponding diff file

Branches