forked from los-cocos/cocos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_layers.py
More file actions
58 lines (44 loc) · 1.43 KB
/
multiple_layers.py
File metadata and controls
58 lines (44 loc) · 1.43 KB
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
#
# cocos2d
# http://python.cocos2d.org
#
# This code is so you can run the samples without installing the package
from __future__ import division, print_function, unicode_literals
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
import cocos
from cocos.director import director
from pyglet import gl
# Defining a new layer type...
class Square(cocos.layer.Layer):
"""Square (color, c, y, size=50) : A layer drawing a square at (x,y) of
given color and size"""
def __init__(self, color, x, y, size=50):
super(Square, self).__init__()
self.x = x
self.y = y
self.size = size
self.layer_color = color
def draw(self):
super(Square, self).draw()
gl.glColor4f(*self.layer_color)
x, y = self.x, self.y
w = x + self.size
h = y + self.size
gl.glBegin(gl.GL_QUADS)
gl.glVertex2f(x, y)
gl.glVertex2f(x, h)
gl.glVertex2f(w, h)
gl.glVertex2f(w, y)
gl.glEnd()
gl.glColor4f(1, 1, 1, 1)
if __name__ == "__main__":
director.init()
# Create a large number of layers
layers = [Square((0.03 * i, 0.03 * i, 0.03 * i, 1), i * 20, i * 20) for i in range(5, 20)]
# Create a scene with all those layers
sc = cocos.scene.Scene(*layers)
# You can also add layers to a scene later:
sc.add(Square((1, 0, 0, 0.5), 150, 150, 210), name="big_one")
director.run(sc)