~facundo/enjuewemela/trunk

13 by facundobatista
Lot of reordering, starting to play about showing stats, and
1
# This code is part of the 'enjuewemela' game
2
# License: GPLv3
3
# Main author: Facundo Batista
50 by facundo at com
Changed URL
4
# Code, bug tracker, etc:
5
#   https://launchpad.net/enjuewemela/
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
6
#
29 by facundo at com
More lint issues
7
"""Gems for the game."""
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
8
9
from __future__ import division
10
11
import os
70.1.6 by facundo at com
First playable crazy version.
12
import random
57.1.1 by facundo at com
Basic magic gems set up
13
67.1.1 by facundo at com
Reorder back :(
14
import cocos
57.1.1 by facundo at com
Basic magic gems set up
15
from cocos.particle_systems import Galaxy
16
from cocos.particle import Color
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
17
67.1.1 by facundo at com
Reorder back :(
18
import config
19
20
21
class _Gem(cocos.sprite.Sprite):
27 by facundo at com
Lot of lint issues
22
    """Gem wrapper around a sprite."""
55.2.3 by facundo at com
Working on the logic to explode.
23
    def __init__(self, filename, pos_id, explosive=False):
67.1.1 by facundo at com
Reorder back :(
24
        cocos.sprite.Sprite.__init__(self, filename)
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
25
        # piece name is the file name without the .png
26
        self.name = filename[:-4]
54 by facundo at com
Shows the explosive gem (still doesn't use it)
27
        self.pos_id = pos_id
57.1.2 by facundo at com
5-match give us a magic ball
28
        self.is_explosive = explosive
29
        self.is_magic = False
54 by facundo at com
Shows the explosive gem (still doesn't use it)
30
31
    def get_explosive(self):
32
        """Return an explosive version of self."""
55.1.1 by facundo at com
Now we can start with a custom sceneraio
33
        if self.name[:4] == 'expl':
70.1.10 by facundo at com
Explosives
34
            fname = '%s.png' % self.name
55.1.1 by facundo at com
Now we can start with a custom sceneraio
35
        else:
70.1.10 by facundo at com
Explosives
36
            fname = 'expl-%s.png' % self.name
37
        g = _Gem(fname, self.pos_id, explosive=True)
54 by facundo at com
Shows the explosive gem (still doesn't use it)
38
        g.scale = self.scale
39
        return g
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
40
41
    def __str__(self):
42
        return self.name
43
54 by facundo at com
Shows the explosive gem (still doesn't use it)
44
    def __repr__(self):
45
        return "<Gem %s at %d>" % (self.name, id(self))
46
55.2.3 by facundo at com
Working on the logic to explode.
47
57.1.2 by facundo at com
5-match give us a magic ball
48
class _Magic(Galaxy):
49
    """Magic ball."""
50
    def __init__(self):
51
        Galaxy.__init__(self)
52
        self.size = 20
53
        self.live = 10.0
54
        self.speed = 30
55
        self.radial_accel = -100
56
        self.tangential_accel = 30
57
        self.total_particles = 60
58
        self.start_color = Color(.7, .7, 1, 1)
59
        self.end_color = Color(1, 0, 0, .7)
60
61
        self.name = 'magic'
62
        self.pos_id = 13
63
        self.is_magic = True
64
        self.is_explosive = False
65
66
    def __str__(self):
67
        return self.name
68
69
    def __repr__(self):
70
        return "<Gem %s at %d>" % (self.name, id(self))
71
57.1.5 by facundo at com
Handle magic scale correctly
72
    def _set_scale(self, scale):
92.1.10 by facundo at com
Pause the magic piece.
73
        """Set the scale."""
57.1.5 by facundo at com
Handle magic scale correctly
74
        self._scale = scale * 5
72.1.2 by facundo at com
pep8 ftw
75
57.1.5 by facundo at com
Handle magic scale correctly
76
    def _get_scale(self):
92.1.10 by facundo at com
Pause the magic piece.
77
        """Get the scale."""
57.1.5 by facundo at com
Handle magic scale correctly
78
        return self._scale / 5
72.1.2 by facundo at com
pep8 ftw
79
57.1.5 by facundo at com
Handle magic scale correctly
80
    scale = property(_get_scale, _set_scale)
81
92.1.10 by facundo at com
Pause the magic piece.
82
    def _set_opacity(self, value):
83
        """Set the opacity."""
84
        self.active = bool(value)
85
86
    opacity = property(fset=_set_opacity)
87
57.1.2 by facundo at com
5-match give us a magic ball
88
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
89
class Gems(object):
29 by facundo at com
More lint issues
90
    """A gem representation."""
91
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
92
    def __init__(self):
93
        # load all the gems
92.2.4 by facundo at com
Code aesthetics.
94
        jewels_path = os.path.join(config.BASEDIR, "jewels")
95
        jewels = [x for x in os.listdir(jewels_path)
96
                  if not x.startswith('expl') and not x.startswith('crazy')]
97
        self.pngs = sorted(x for x in jewels if x.endswith('.png'))
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
98
        if len(self.pngs) != 7:
29 by facundo at com
More lint issues
99
            raise ValueError("The gems (png files) in the 'gems' directory "
100
                             "must be 7! (found %d)" % len(self.pngs))
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
101
29 by facundo at com
More lint issues
102
        __, max_both = self._get_gems_size()
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
103
104
        # calculate the resize factor
105
        self.resize_factor = config.PIECE_SIZE / max_both
106
70.1.6 by facundo at com
First playable crazy version.
107
        # are we crazy? see the setter below for how these are used
108
        self._crazy_mode = False
109
        self._crazy_style = None
110
        self._crazy_fixed = None
111
112
    def _get_crazy(self):
113
        """Stupid getter for crazy mode."""
114
        return self._crazy_mode
115
116
    def _set_crazy(self, crazy):
117
        """Setter for craziness that also calculates other values."""
118
        if not crazy:
119
            self._crazy_mode = False
120
            self._crazy_style = None
121
            self._crazy_fixed = None
122
            return
123
124
        # go crazy!
125
        self._crazy_mode = True
126
127
        # style! 0 is 'all colors the same', 1 is 'all shapes the same'
128
        self._crazy_style = random.randint(0, 1)
129
130
        # which is the fixed value for the "sameness" in colors or shapes
131
        self._crazy_fixed = random.randint(0, 6)
132
133
    crazy = property(_get_crazy, _set_crazy)
134
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
135
    def _get_gems_size(self):
29 by facundo at com
More lint issues
136
        """Return the gems and the max size in both axis."""
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
137
        # convert pngs into sprites
67.1.1 by facundo at com
Reorder back :(
138
        all_gems = [cocos.sprite.Sprite(x) for x in self.pngs]
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
139
140
        # get the maximum height or width of all of them
29 by facundo at com
More lint issues
141
        max_w = max(x.width for x in all_gems)
142
        max_h = max(x.height for x in all_gems)
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
143
        max_both = max(max_w, max_h)
29 by facundo at com
More lint issues
144
        return all_gems, max_both
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
145
14 by facundobatista
Almost done the stats part.
146
    def get_all(self, size=None):
29 by facundo at com
More lint issues
147
        """Get all the gems, resized if indicated."""
148
        all_gems, max_both = self._get_gems_size()
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
149
14 by facundobatista
Almost done the stats part.
150
        if size is not None:
151
            # resize the sprites accordingly
152
            resize_factor = size / max_both
29 by facundo at com
More lint issues
153
            for gem in all_gems:
27 by facundo at com
Lot of lint issues
154
                gem.scale = resize_factor
29 by facundo at com
More lint issues
155
        return all_gems
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
156
55.1.1 by facundo at com
Now we can start with a custom sceneraio
157
    def get_magic_ball(self):
158
        """Return a magic ball gem."""
57.1.5 by facundo at com
Handle magic scale correctly
159
        sp = _Magic()
160
        sp.scale = self.resize_factor
161
        return sp
55.1.1 by facundo at com
Now we can start with a custom sceneraio
162
92.2.1 by facundo at com
Working replayer.
163
    def by_name(self, name):
164
        """Return a gem by name."""
92.2.3 by facundo at com
Support magic balls.
165
        if name == 'magic':
166
            return self.get_magic_ball()
92.2.1 by facundo at com
Working replayer.
167
        sp = _Gem(name + '.png', None)
168
        sp.scale = self.resize_factor
169
        return sp
170
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
171
    def __getitem__(self, pos):
70.1.6 by facundo at com
First playable crazy version.
172
        if self._crazy_mode:
173
            if self._crazy_style:
174
                # all shapes the same
175
                gempath = 'crazy-c%02d-s%02d.png' % (pos, self._crazy_fixed)
176
            else:
177
                # all colors the same
178
                gempath = 'crazy-c%02d-s%02d.png' % (self._crazy_fixed, pos)
179
            sp = _Gem(gempath, pos)
180
181
        else:
182
            sp = _Gem(self.pngs[pos], pos)
183
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
184
        sp.scale = self.resize_factor
185
        return sp
186
70.1.6 by facundo at com
First playable crazy version.
187
13 by facundobatista
Lot of reordering, starting to play about showing stats, and
188
gems = Gems()