~facundo/enjuewemela/trunk

« back to all changes in this revision

Viewing changes to enjuewemela/cocos/samples/multiple_layers.py

  • Committer: facundo at com
  • Date: 2011-05-14 18:13:25 UTC
  • mfrom: (67.1.4 v3rel)
  • Revision ID: facundo@taniquetil.com.ar-20110514181325-614h8kjz32w5cmoy
Refactor back

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# cocos2d
 
3
# http://cocos2d.org
 
4
#
 
5
# This code is so you can run the samples without installing the package
 
6
import sys
 
7
import os
 
8
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
 
9
 
 
10
 
 
11
import cocos
 
12
from cocos.director import director
 
13
 
 
14
from pyglet import gl
 
15
 
 
16
# Defining a new layer type...
 
17
class Square(cocos.layer.Layer):
 
18
    """Square (color, c, y, size=50) : A layer drawing a square at (x,y) of
 
19
    given color and size"""
 
20
    def __init__(self, color, x, y, size=50):
 
21
        super( Square, self ).__init__()
 
22
 
 
23
        self.x = x
 
24
        self.y = y
 
25
        self.size = size
 
26
        self.layer_color = color
 
27
                
 
28
    def draw(self):
 
29
        super(Square,self).draw()
 
30
 
 
31
        gl.glColor4f(*self.layer_color)
 
32
        x, y = self.x, self.y
 
33
        w = x+self.size; h=y+self.size
 
34
        gl.glBegin(gl.GL_QUADS)
 
35
        gl.glVertex2f( x, y )
 
36
        gl.glVertex2f( x, h )
 
37
        gl.glVertex2f( w, h )
 
38
        gl.glVertex2f( w, y )
 
39
        gl.glEnd()
 
40
        gl.glColor4f(1,1,1,1) 
 
41
        
 
42
if __name__ == "__main__":
 
43
    director.init()
 
44
    # Create a large number of layers
 
45
    layers = [ Square((0.03*i,0.03*i,0.03*i,1) , i*20, i*20) for i in range(5,20) ]
 
46
    # Create a scene with all those layers
 
47
    sc = cocos.scene.Scene(*layers)
 
48
    # You can also add layers to a scene later:
 
49
    sc.add( Square((1,0,0,0.5), 150,150, 210 ), name="big_one" )
 
50
    director.run( sc )