Skip to content

Commit 9758243

Browse files
committed
- Fixed some typos
- Added hello World framework example - fixe helpers.py to work as a module
1 parent 79dc954 commit 9758243

File tree

6 files changed

+72
-9
lines changed

6 files changed

+72
-9
lines changed

cocos-test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def step(self, dt):
3535
# Set the object's velocity.
3636
self.target.velocity = (velocity_x, velocity_y)
3737

38-
# Main class
38+
# Main method of class
3939
def main():
4040
global keyboard # Declare this as global so it can be accessed within class methods.
4141
# Initialize the window
@@ -60,12 +60,12 @@ def main():
6060

6161
# Set the sprite's movement class and run some actions.
6262
molecule.do(actions.Repeat(scale + actions.Reverse(scale)))
63-
63+
6464
label.do(Me())
6565

6666
# Rotate the entire player_layer (includes ALL nodes, will rotate ONCE)
6767
player_layer.do(actions.RotateBy(360, duration=10))
68-
68+
6969
# Create a scene and set its initial layer.
7070
main_scene = scene.Scene(player_layer)
7171

framework/helloWorld.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env python3
2+
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\' ?')
8+
9+
# import helper Class and methods
10+
import helpers
11+
12+
# Set text on screen
13+
msg = "Hello, world! This is text!!!!111!!11!!!1"
14+
15+
# Set font size
16+
fontSize = 42
17+
18+
# Set screen size
19+
size = (800,600)
20+
21+
# our main function
22+
def main():
23+
# We initialize the director, that takes care of our main window
24+
cocos.director.director.init(width=size[0], height=size[1], autoscale=True, resizable=True)
25+
26+
# We instantiate hello_layer with our HelloWorld() class
27+
hello_layer = helpers.HelloWorld(msg=msg, fontSize=fontSize)
28+
# Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
29+
main_scene = cocos.scene.Scene(hello_layer)
30+
# All setup now. Let's run our main_scene
31+
cocos.director.director.run(main_scene)
32+
# The above could have been compacted to:
33+
# cocos.director.director.run(cocos.scene.Scene(helpers.HelloWorld()))
34+
35+
if __name__ == '__main__': main()

framework/helpers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# try to import the cocos module
2+
try:
3+
import cocos
4+
except:
5+
print('Cannot import cocos. Did you run \'pip install cocos2d\' ?')
6+
7+
# define a simple HelloWorld() class that inherits from cocos.layer.Layer (making it a sub-class of it)
8+
class HelloWorld(cocos.layer.Layer):
9+
# __init__ is run every time you instantiate the class
10+
def __init__(self, msg='Hello, world!', fontName='Times New Roman', fontSize=32, color='white'):
11+
# Always call super in the constructor
12+
super(HelloWorld, self).__init__()
13+
# label will become an object with all the necessary to display a text, with font TimesNewRoman and it will be center-anchored
14+
# Note: .Label is a subclass of CocosNode
15+
label = cocos.text.Label(msg , font_name=fontName, font_size=fontSize, anchor_x='center', anchor_y='center')
16+
# set the position of our text to x:320 y:240
17+
label.position = (320, 240)
18+
# add our label as a child. It is a CocosNode object, which know how to render themselves.
19+
self.add(label)
20+
21+
def main():
22+
print("This is a module, please import me")
23+
24+
if __name__ == '__main__':
25+
main()

handling_events.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import sys
77
import os
88
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
9-
#
109

1110
import cocos
1211
from cocos.director import director
@@ -21,7 +20,6 @@ class KeyDisplay(cocos.layer.Layer):
2120
is_event_handler = True #: enable pyglet's events
2221

2322
def __init__(self):
24-
2523
super(KeyDisplay, self).__init__()
2624

2725
self.text = cocos.text.Label("", x=100, y=280)
@@ -30,13 +28,15 @@ def __init__(self):
3028
self.keys_pressed = set()
3129
self.update_text()
3230
self.add(self.text)
31+
print("Initialiazed KeyDisplay()")
3332

3433
# This method of the class updates the text
3534
def update_text(self):
3635
key_names = [pyglet.window.key.symbol_string(k) for k in self.keys_pressed]
3736
text = 'Keys: ' + ','.join(key_names)
3837
# Update self.text
3938
self.text.element.text = text
39+
print("Called update_text in KeyDisplay()")
4040

4141
def on_key_press(self, key, modifiers):
4242
"""This function is called when a key is pressed.
@@ -50,6 +50,7 @@ def on_key_press(self, key, modifiers):
5050
"""
5151
self.keys_pressed.add(key)
5252
self.update_text()
53+
print("Called on_key_press in KeyDisplay()")
5354

5455
def on_key_release(self, key, modifiers):
5556
"""This function is called when a key is released.
@@ -70,6 +71,7 @@ def on_key_release(self, key, modifiers):
7071
"""
7172
self.keys_pressed.discard(key)
7273
self.update_text()
74+
print("Called on_key_release in KeyDisplay()")
7375

7476

7577
class MouseDisplay(cocos.layer.Layer):
@@ -86,6 +88,7 @@ def __init__(self):
8688
self.posy = 240
8789
self.text = cocos.text.Label('No mouse events yet', font_size=18, x=self.posx, y=self.posy)
8890
self.add(self.text)
91+
print("Initialiazed MouseDisplay()")
8992

9093
def update_text(self, x, y):
9194
text = 'Mouse @ %d,%d' % (x, y)

helloWorld.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ def __init__(self):
1717
label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
1818
# set the position of our text to x:320 y:240
1919
label.position = (320, 240)
20-
# add our label as a child. It is a CocosNode object, which know how to render themselves.
20+
# add our label as a child. It is a CocosNode object, which know how to render themselves.
2121
self.add(label)
2222

23-
# our main funciton
23+
# our main function
2424
def main():
2525
# We initialize the director, that takes care of our main window
2626
cocos.director.director.init()

helloWorldActions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self):
1919
label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
2020
# set the position of our text to x:320 y:240
2121
label.position = (320, 240)
22-
# add our label as a child. It is a CocosNode object, which know how to render themselves.
22+
# add our label as a child. It is a CocosNode object, which know how to render themselves.
2323
self.add(label)
2424
# sprite becomes a .Sprite object with our molecule picture
2525
sprite = cocos.sprite.Sprite('sprites/molecule.png')
@@ -36,7 +36,7 @@ def __init__(self):
3636
# Repeat the reverse of our scale action plus the scale action to our sprite
3737
sprite.do(Repeat(Reverse(scale) + scale))
3838

39-
# our main funciton
39+
# our main function
4040
def main():
4141
# We initialize the director, that takes care of our main window
4242
cocos.director.director.init()

0 commit comments

Comments
 (0)