1
1
#!/usr/bin/env python3
2
2
3
- import cocos
3
+ # try to import the cocos module
4
+ try :
5
+ import cocos
6
+ except :
7
+ print ('Cannot import cocos. Did you run \' pip install cocos2d\' ?' )
4
8
9
+ # define a simple HelloWorld() class that inherits from cocos.layer.Layer (making it a sub-class of it)
5
10
class HelloWorld (cocos .layer .Layer ):
11
+ # __init__ is run every time you instantiate the class
6
12
def __init__ (self ):
13
+ # Always call super in the constructor
7
14
super (HelloWorld , self ).__init__ ()
15
+ # label will become an object with all the necessary to display a text, with font TimesNewRoman and it will be center-anchored
16
+ # Note: .Label is a subclass of CocosNode
8
17
label = cocos .text .Label ('Hello, world' , font_name = 'Times New Roman' , font_size = 32 , anchor_x = 'center' , anchor_y = 'center' )
9
- label .position = 320 , 240
18
+ # set the position of our text to x:320 y:240
19
+ label .position = (320 , 240 )
20
+ # add our label as a child. It is a CocosNode object, which know how to render themselves.
10
21
self .add (label )
11
22
23
+ # our main funciton
12
24
def main ():
25
+ # We initialize the director, that takes care of our main window
13
26
cocos .director .director .init ()
27
+ # We instantiate hello_layer with our HelloWorld() class
14
28
hello_layer = HelloWorld ()
29
+ # Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
15
30
main_scene = cocos .scene .Scene (hello_layer )
31
+ # All setup now. Let's run our main_scene
16
32
cocos .director .director .run (main_scene )
33
+ # The above could have been compacted to:
34
+ # cocos.director.director.run(cocos.scene.Scene(HelloWorld()))
17
35
18
36
if __name__ == '__main__' : main ()
0 commit comments