Skip to content

Commit d5ce0e5

Browse files
committed
- Comments, module check
1 parent f975c1f commit d5ce0e5

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

helloWorld.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
#!/usr/bin/env python3
22

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\' ?')
48

9+
# define a simple HelloWorld() class that inherits from cocos.layer.Layer (making it a sub-class of it)
510
class HelloWorld(cocos.layer.Layer):
11+
# __init__ is run every time you instantiate the class
612
def __init__(self):
13+
# Always call super in the constructor
714
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
817
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.
1021
self.add(label)
1122

23+
# our main funciton
1224
def main():
25+
# We initialize the director, that takes care of our main window
1326
cocos.director.director.init()
27+
# We instantiate hello_layer with our HelloWorld() class
1428
hello_layer = HelloWorld()
29+
# Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
1530
main_scene = cocos.scene.Scene(hello_layer)
31+
# All setup now. Let's run our main_scene
1632
cocos.director.director.run(main_scene)
33+
# The above could have been compacted to:
34+
# cocos.director.director.run(cocos.scene.Scene(HelloWorld()))
1735

1836
if __name__ == '__main__': main()

0 commit comments

Comments
 (0)