Skip to content

Commit 79dc954

Browse files
committed
Merge branch 'master' of github.com:SteveClement/cocos2d-python-examples
2 parents 023aa69 + 84d1d94 commit 79dc954

File tree

4 files changed

+145
-13
lines changed

4 files changed

+145
-13
lines changed

cocos-test.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
from cocos import actions, layer, sprite, scene
1616
from cocos.director import director
1717

18-
# Player class
18+
# Screen size
19+
size = (800,600)
1920

21+
# Arbitrary variable we include later in out text label, attention to the scope!
22+
deltaTime = 42
23+
24+
# Player class called Me() <--- Needs better name
2025
class Me(actions.Move):
2126

2227
# step() is called every frame.
@@ -29,32 +34,38 @@ def step(self, dt):
2934

3035
# Set the object's velocity.
3136
self.target.velocity = (velocity_x, velocity_y)
32-
# Main class
3337

38+
# Main class
3439
def main():
3540
global keyboard # Declare this as global so it can be accessed within class methods.
3641
# Initialize the window
37-
director.init(width=500, height=300, autoscale=True, resizable=True)
42+
director.init(width=size[0], height=size[1], autoscale=True, resizable=True)
3843

3944
# Create a layer and add a sprite to it.
4045
player_layer = layer.Layer()
41-
me = sprite.Sprite('sprites/molecule.png')
42-
player_layer.add(me)
43-
44-
# Add a Label, because we can
45-
label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
46-
label.position = 250, 150
46+
molecule = sprite.Sprite('sprites/molecule.png')
47+
molecule.scale = 2
48+
player_layer.add(molecule, z=1)
49+
scale = actions.ScaleBy(3, duration=2)
50+
51+
# Add a Label, because we can.
52+
label = cocos.text.Label('Hello, world@' + str(deltaTime), font_name='Times New Roman', font_size=32, anchor_x='left', anchor_y='center')
53+
label.position = 0, size[1]/2
4754
label.velocity = 0, 0
4855
player_layer.add(label)
4956

5057
# Set initial position and velocity.
51-
me.position = (100, 100)
52-
me.velocity = (0, 0)
58+
molecule.position = (size[0]/2, size[1]/2)
59+
molecule.velocity = (0, 0)
5360

54-
# Set the sprite's movement class.
55-
#me.do(Me())
61+
# Set the sprite's movement class and run some actions.
62+
molecule.do(actions.Repeat(scale + actions.Reverse(scale)))
63+
5664
label.do(Me())
5765

66+
# Rotate the entire player_layer (includes ALL nodes, will rotate ONCE)
67+
player_layer.do(actions.RotateBy(360, duration=10))
68+
5869
# Create a scene and set its initial layer.
5970
main_scene = scene.Scene(player_layer)
6071

handlinEvents.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
class KeyDisplay(cocos.layer.Layer):
4+
is_event_handler = True
5+
def __init__(self);
6+
super(KeyDisplay, self).__init__()
7+
self.text = cocos.text.Label("", x=100, y=280)
8+
9+
self.key_pressed = set()
10+
self.update_text()
11+
self.add(self.text)
12+
13+
def update(self):
14+
key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed]
15+
text = 'Keys: ' + ','.join(key_names)
16+
self.text.element.text = text
17+
18+
def on_key_press(self, key, modifiers):
19+
self.keys_pressed.add(key)
20+
self.update.text()
21+
22+
def on_key_release(self, key, modifiers):
23+
self.keys_pressed.remove(key)
24+
self.update.text()
25+
26+
def update_text(self):
27+
key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed]
28+
text = 'Keys: ' + ','.join(key_names)
29+
self.text.element.text = text
30+
31+

helloWorld.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
# define a simple HelloWorld() class that inherits from cocos.layer.Layer (making it a sub-class of it)
10+
class HelloWorld(cocos.layer.Layer):
11+
# __init__ is run every time you instantiate the class
12+
def __init__(self):
13+
# Always call super in the constructor
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
17+
label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
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.
21+
self.add(label)
22+
23+
# our main funciton
24+
def main():
25+
# We initialize the director, that takes care of our main window
26+
cocos.director.director.init()
27+
# We instantiate hello_layer with our HelloWorld() class
28+
hello_layer = HelloWorld()
29+
# Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
30+
main_scene = cocos.scene.Scene(hello_layer)
31+
# All setup now. Let's run our main_scene
32+
cocos.director.director.run(main_scene)
33+
# The above could have been compacted to:
34+
# cocos.director.director.run(cocos.scene.Scene(HelloWorld()))
35+
36+
if __name__ == '__main__': main()

helloWorldActions.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
3+
# try to import the cocos module and import to our namespace the .actions from the cocos module
4+
try:
5+
import cocos
6+
from cocos.actions import *
7+
except:
8+
print('Cannot import cocos. Did you run \'pip install cocos2d\' ?')
9+
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):
13+
# __init__ is run every time you instantiate the class
14+
def __init__(self):
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)
17+
# label will become an object with all the necessary to display a text, with font TimesNewRoman and it will be center-anchored
18+
# Note: .Label is a subclass of CocosNode
19+
label = cocos.text.Label('Hello, world', font_name='Times New Roman', font_size=32, anchor_x='center', anchor_y='center')
20+
# set the position of our text to x:320 y:240
21+
label.position = (320, 240)
22+
# add our label as a child. It is a CocosNode object, which know how to render themselves.
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))
38+
39+
# our main funciton
40+
def main():
41+
# We initialize the director, that takes care of our main window
42+
cocos.director.director.init()
43+
# We instantiate hello_layer with our HelloWorld() class
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))
47+
# Now we create a .Scene and pass our HelloWorld() object stored in hello_layer, as a child
48+
main_scene = cocos.scene.Scene(hello_layer)
49+
# All setup now. Let's run our main_scene
50+
cocos.director.director.run(main_scene)
51+
# The above could have been compacted to:
52+
# cocos.director.director.run(cocos.scene.Scene(HelloWorld()))
53+
54+
if __name__ == '__main__': main()

0 commit comments

Comments
 (0)