|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
3 |
| -# try to import the cocos module |
| 3 | +# try to import the cocos module and import to our namespace the .actions from the cocos module |
4 | 4 | try:
|
5 | 5 | import cocos
|
| 6 | + from cocos.actions import * |
6 | 7 | except:
|
7 | 8 | print('Cannot import cocos. Did you run \'pip install cocos2d\' ?')
|
8 | 9 |
|
9 |
| -# define a simple HelloWorld() class that inherits from cocos.layer.Layer (making it a sub-class of it) |
10 |
| -class HelloWorld(cocos.layer.Layer): |
| 10 | +# define a simple HelloWorld() class that inherits from cocos.layer.ColorLayer (making it a sub-class of it) |
| 11 | +# Difference between .Layer and .ColorLayer is, well, you can Colorize |
| 12 | +class HelloWorld(cocos.layer.ColorLayer): |
11 | 13 | # __init__ is run every time you instantiate the class
|
12 | 14 | def __init__(self):
|
13 |
| - # Always call super in the constructor |
14 |
| - super(HelloWorld, self).__init__() |
| 15 | + # Always call super in the constructor, we taint the layer with r:64 g:64 b:224 alpha:255 |
| 16 | + super(HelloWorld, self).__init__(64,64,224,255) |
15 | 17 | # label will become an object with all the necessary to display a text, with font TimesNewRoman and it will be center-anchored
|
16 | 18 | # Note: .Label is a subclass of CocosNode
|
17 | 19 | label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
|
18 | 20 | # set the position of our text to x:320 y:240
|
19 | 21 | label.position = (320, 240)
|
20 | 22 | # add our label as a child. It is a CocosNode object, which know how to render themselves.
|
21 | 23 | self.add(label)
|
| 24 | + # sprite becomes a .Sprite object with our molecule picture |
| 25 | + sprite = cocos.sprite.Sprite('sprites/molecule.png') |
| 26 | + # set the position of our sprite to x:320 y;240 the default position is x:0 y:0 |
| 27 | + sprite.position = (320,240) |
| 28 | + # scale the sprite 2x |
| 29 | + sprite.scale = 2 |
| 30 | + # add our sprite as a child and make sure it is on top by defining the z-axis, default is z: 0 |
| 31 | + self.add(sprite, z=1) |
| 32 | + # create a ScaleBy action object that scales our sprite 3x during 2 seconds |
| 33 | + scale = ScaleBy(3, duration=2) |
| 34 | + # Repeat the scale action plus the revers of the scale action to our label |
| 35 | + label.do(Repeat(scale + Reverse(scale))) |
| 36 | + # Repeat the reverse of our scale action plus the scale action to our sprite |
| 37 | + sprite.do(Repeat(Reverse(scale) + scale)) |
22 | 38 |
|
23 | 39 | # our main funciton
|
24 | 40 | def main():
|
25 | 41 | # We initialize the director, that takes care of our main window
|
26 | 42 | cocos.director.director.init()
|
27 | 43 | # We instantiate hello_layer with our HelloWorld() class
|
28 | 44 | hello_layer = HelloWorld()
|
| 45 | + # All CocosNode objects can execute actions, so we can execute ONE rotate action on ALL of our layers. 360deg-turn in 10 seconds |
| 46 | + hello_layer.do(RotateBy(360, duration=10)) |
29 | 47 | # Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
|
30 | 48 | main_scene = cocos.scene.Scene(hello_layer)
|
31 | 49 | # All setup now. Let's run our main_scene
|
|
0 commit comments