Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/ecomundoactivity.py
blob: 5d4ef821ef2f30271990364021db27e3d59a0053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python

### Simulacion de un ecosistema, en el que existe pasto, conejos y zorros.
### By Gonzalo Odiard, 2006 godiard at gmail.com
### GPL License - http://www.gnu.org/copyleft/gpl.html

import gobject, gtk , cairo
import math, random
import os
import hippo

import pango
import logging
from gettext import gettext as _
import World,Animals,Green
import sugar
from sugar.activity import activity
from sugar.graphics import style
from sugar.graphics.toolbutton import ToolButton


world = World.World()

class Tile:
    def __init__ (self,x,y):
        self.X = x
        self.Y = y
        self.STATE = 0;


def drawGrid(ctx):    
    ctx.set_line_width(2)
    ctx.set_source_rgb(0,0,0)
    for n in range(0,World.CANT_TILES):
        ctx.move_to(World.MARGEN,World.MARGEN +(World.SIZE_TILE*n))
        ctx.line_to(World.SIZE_TILE * (World.CANT_TILES-1) + World.MARGEN,
            World.MARGEN +(World.SIZE_TILE*n))
        ctx.move_to(World.MARGEN +(World.SIZE_TILE*n),World.MARGEN)
        ctx.line_to(World.MARGEN +(World.SIZE_TILE*n),
            World.SIZE_TILE * (World.CANT_TILES-1)+ World.MARGEN)
        ctx.stroke()

def initWorld(): 
    world.state = []
    world.animals = []
    for n in range(0,World.CANT_TILES):
        world.state.append([])
        for p in range(0,World.CANT_TILES):
            tile = Tile(n,p)
            world.state[n].append(tile)
            

def drawStateWorld(ctx):
    #print "drawStateWorld"
    ctx.move_to(0,0)
    ctx.rectangle(0,0,World.SIZE_WORLD+(2*World.MARGEN),World.SIZE_WORLD+(2*World.MARGEN))
    #ctx.set_source_rgb(style.COLOR_PANEL_GREY.get_gdk_color())
    ctx.set_source_rgb(192.0/256.0,192.0/256.0,192.0/256.0)    
    ctx.fill()


    for n in range(0,World.CANT_TILES-1):
        for p in range(0,World.CANT_TILES-1):
            ctx.save()
            Green.getImagesGreen(world,ctx,n,p)
            #Green.getColorTile(world,ctx,n,p)
            ctx.restore()
    for n in range(len(world.animals)):
        #print "Animal",n
        animal = world.animals[n]
        ctx.save()
        animal.draw(ctx)
        ctx.restore()

def initGreen():
    for n in range(world.initialGreen):
        x = int(random.random()*World.CANT_TILES)
        y = int(random.random()*World.CANT_TILES)
        world.state[x][y].STATE = 2

def initAnimals():
    for n in range(world.initialRabbits):
        x = int(random.random()*(World.CANT_TILES-1))
        y = int(random.random()*(World.CANT_TILES-1))
        #print "Init Rabbit",x,y
        animal = Animals.Rabbit(x,y,world)
        world.animals.append(animal)
    for n in range(world.initialFoxs):
        x = int(random.random()*(World.CANT_TILES-1))
        y = int(random.random()*(World.CANT_TILES-1))
        #print "Init Fox",x,y
        animal = Animals.Fox(x,y,world)
        world.animals.append(animal)
                                        

def updateState(drawingarea):    
    #print "update State"
    if (world.playState):
        Green.grow(world)
        n = 0
        
        while (n < len(world.animals)):
            animal = world.animals[n]
            animal.move(world)
            if (not animal.checkLive()):
                cantAnimals = len(world.animals)
                world.animals[n] = cantAnimals
                world.animals.remove(cantAnimals)
            else :
                #Actualizo donde esta
                x1 = World.MARGEN+(World.SIZE_TILE*animal.posX)
                y1 = World.MARGEN +(World.SIZE_TILE*animal.posY)
                n = n+1
        drawingarea.queue_draw_area(0, 0,World.SIZE_WORLD, World.SIZE_WORLD)
        source_id = gobject.timeout_add(1000, updateState,drawingarea)    


class EcomundoActivity(activity.Activity):
    
    def __init__(self,handle):
        activity.Activity.__init__(self,handle)
        
        self.set_title("Ecomundo")
        print "Init activity Ecomundo"
        #print os.path.abspath(__file__)

        toolbox = activity.ActivityToolbox(self)
        self.toolbar = gtk.Toolbar()

        toolbox.add_toolbar(_('Ecomundo'), self.toolbar)
        self.toolbar.show()
        self.set_toolbox(toolbox)
        toolbox.show()

        self.btnNew = ToolButton('reload')
        self.btnNew.connect('clicked', self.onBtNewClicked)
        self.toolbar.insert(self.btnNew, -1)
        self.btnNew.show()

        self.btPlay = ToolButton('next')
        self.btPlay.connect('clicked', self.onBtPlayClicked)
        self.toolbar.insert(self.btPlay, -1)
        self.btPlay.show()

        self.btStop = ToolButton('process-stop')
        self.btStop.connect('clicked', self.onBtStopClicked)
        self.toolbar.insert(self.btStop, -1)
        self.btStop.show()

        self.btStop.props.sensitive = False;
        self.btPlay.props.sensitive = True;

        toolbox.set_current_toolbar(1)

        hBox = gtk.HBox(False, 0)
        self.set_canvas(hBox)
        
        self.drawingarea1 = gtk.DrawingArea()
        self.drawingarea1.set_size_request(World.SIZE_WORLD+(2*World.MARGEN),World.SIZE_WORLD+(2*World.MARGEN))
        self.drawingarea1.show()

        hBox.pack_start(self.drawingarea1, False, True, 5)
        
        table = gtk.Table(rows=4, columns=2, homogeneous=False)
        hBox.pack_start(table, False, False, 5)

        label_attributes = pango.AttrList()
        label_attributes.insert(pango.AttrSize(14000, 0, -1))
        label_attributes.insert(pango.AttrForeground(65535, 65535, 65535, 0, -1))

        lbTitle = gtk.Label()
        lbTitle.set_attributes(label_attributes)
        lbTitle.set_text(_('Initial Values'))
        #table.attach(lbTitle, 0, 1, 0, 1,yoptions=gtk.SHRINK,xpadding=5) 
        table.attach(lbTitle, 0, 2, 0, 1,yoptions=gtk.SHRINK,xpadding=10) 

        lbGreen = gtk.Label()
        lbGreen.set_attributes(label_attributes)
        lbGreen.set_text(_('Green'))
        table.attach(lbGreen, 0, 1, 1, 2,yoptions=gtk.SHRINK,xoptions=gtk.SHRINK,xpadding=10) 

        adjGreen = gtk.Adjustment(10, 1, 400, 1, 1, 0)
        self.spbGreen = gtk.SpinButton(adjustment=adjGreen, climb_rate=1.0, digits=2)
        table.attach(self.spbGreen, 1, 2, 1, 2,yoptions=gtk.SHRINK,xoptions=gtk.SHRINK,ypadding=10) 

        lbRabbit = gtk.Label()
        lbRabbit.set_attributes(label_attributes)
        lbRabbit.set_text(_('Rabbits'))
        table.attach(lbRabbit, 0, 1, 2, 3,yoptions=gtk.SHRINK,xoptions=gtk.SHRINK,xpadding=10)

        adjRabbit = gtk.Adjustment(10, 1, 400, 1, 1, 0)
        self.spbRabbit = gtk.SpinButton(adjustment=adjRabbit, climb_rate=1.0, digits=2)
        table.attach(self.spbRabbit, 1, 2, 2, 3,yoptions=gtk.SHRINK,xoptions=gtk.SHRINK,ypadding=10)

        lbFox = gtk.Label()
        lbFox.set_attributes(label_attributes)
        lbFox.set_text(_('Foxs'))
        table.attach(lbFox, 0, 1, 3, 4,yoptions=gtk.SHRINK,xoptions=gtk.SHRINK,xpadding=10)
        
        adjFox = gtk.Adjustment(10, 1, 400, 1, 1, 0)
        self.spbFox = gtk.SpinButton(adjustment=adjFox, climb_rate=1.0, digits=2)
        table.attach(self.spbFox, 1, 2, 3, 4,yoptions=gtk.SHRINK,xoptions=gtk.SHRINK,ypadding=10)


        print "test resize"
        print "antes de initWorld"
        initWorld()
        print "antes de init Green"
        initGreen()
        print "antes de init Animals"
        initAnimals()

        hBox.resize_children()
        hBox.show_all()

        self.drawingarea1.connect('expose-event', self.onDrawingAreaExposed)


    def onDrawingAreaExposed(self,da, event):
        #print "drawingarea exposed"
        x, y, width, height = da.allocation
        ctx = da.window.cairo_create()
        drawStateWorld(ctx)        
        drawGrid(ctx)

    def onBtPlayClicked(self,widget):
        self.btStop.props.sensitive = True;
        self.btPlay.props.sensitive = False;
        world.playState = True
        source_id = gobject.timeout_add(2000, updateState,self.drawingarea1)    
        # http://www.pygtk.org/pygtk2tutorial-es/ch-TimeoutsIOAndIdleFunctions.html#sec-Timeouts

    def onBtStopClicked(self,widget):
        self.btStop.props.sensitive = False;
        self.btPlay.props.sensitive = True;
        world.playState = False

    def onBtNewClicked(self,widget):
        initWorld()
        world.initialGreen = self.spbGreen.get_value_as_int()
        world.initialRabbits = self.spbRabbit.get_value_as_int()
        world.initialFoxs = self.spbFox.get_value_as_int()
        initGreen()
        initAnimals()
        # Despues de esto hay que recargar la pantalla
        drawingarea1 = self.drawingarea1
        ctx = drawingarea1.window.cairo_create()
        drawStateWorld(ctx)        
        drawGrid(ctx)