You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/programming_guide/quickstart.txt
+36-36Lines changed: 36 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -35,20 +35,20 @@ Always call ``super`` in the constructor::
35
35
To display the text, we'll create a `Label`. Keyword arguments are used
36
36
to set the font, position and alignment of the label::
37
37
38
-
label = cocos.text.Label('Hello, world',
39
-
font_name='Times New Roman',
40
-
font_size=36,
41
-
halign='center', valign='center')
38
+
label = cocos.text.Label('Hello, world',
39
+
font_name='Times New Roman',
40
+
font_size=32,
41
+
anchor_x='center', anchor_y='center')
42
42
43
43
The label position will be the center of the screen::
44
44
45
45
label.position = 320,240
46
-
46
+
47
47
Since `Label` is a subclass of `CocosNode` it can be added
48
48
as a child. All `CocosNode` objects know how to render itself, perform actions
49
49
and transformations.
50
50
To add it as a layer's child, use the `CocosNode.add` method::
51
-
51
+
52
52
self.add( label )
53
53
54
54
After defining the ``HelloWorld`` class, we need to initialize and create a window.
@@ -80,7 +80,7 @@ Hello Actions
80
80
81
81
This example is very similar to example #1, with the difference that it introduces
82
82
us to the world of actions.
83
-
An action is like an order. You can tell **any** `CocosNode` object to execute an
83
+
An action is like an order. You can tell **any** `CocosNode` object to execute an
84
84
action.
85
85
You can find the entire program in the `samples/hello_world_actions.py` file.
86
86
@@ -89,12 +89,12 @@ Like in our previous example, we import the cocos package::
89
89
90
90
import cocos
91
91
92
-
If you're going to use several actions, you can import all
92
+
If you're going to use several actions, you can import all
93
93
the available actions into the namespace with this import::
94
94
95
95
from cocos.actions import *
96
96
97
-
We subclass `ColorLayer` to have a background color, and then
97
+
We subclass `ColorLayer` to have a background color, and then
98
98
we call super() with a blueish color::
99
99
100
100
class HelloWorld(cocos.layer.ColorLayer):
@@ -115,16 +115,16 @@ As in the previous example, we create and add a label::
115
115
116
116
In this example we also create and add an sprite as a child. In cocos2d
117
117
sprites are `Sprite` objects::
118
-
118
+
119
119
sprite = cocos.sprite.Sprite('grossini.png')
120
120
121
121
We place the sprite in the center of the screen. Default position is (0,0)::
122
-
122
+
123
123
sprite.position = 320,240
124
124
125
125
We set the scale attribute to 3. This means that our sprite will be 3 times bigger.
126
126
The default scale attribute is 1::
127
-
127
+
128
128
sprite.scale = 3
129
129
130
130
We add the sprite as a child but on top of the label by setting the z-value to 1, since
@@ -142,15 +142,15 @@ We tell the label to:
142
142
3. and we repeat these 2 actions forever
143
143
144
144
Notice that the '+' operator is the `Sequence` action::
145
-
145
+
146
146
label.do( Repeat( scale + Reverse( scale) ) )
147
147
148
148
And we tell the sprite to do the same actions but starting with the 'scale back' action::
149
-
149
+
150
150
sprite.do( Repeat( Reverse(scale) + scale ) )
151
151
152
152
Then we initialize the director, like in the previous example::
153
-
153
+
154
154
cocos.director.director.init()
155
155
hello_layer = HelloWorld ()
156
156
@@ -184,18 +184,18 @@ This demo has a scene with two layers; one shows which keys are currently presse
184
184
We start defining the KeyDisplay layer class. As always, we put some initialization on ``__init__`` and the code for displaying it in step::
185
185
186
186
class KeyDisplay(cocos.layer.Layer):
187
-
187
+
188
188
# If you want that your layer receives events
189
189
# you must set this variable to 'True',
190
190
# otherwise it won't receive any event.
191
191
is_event_handler = True
192
-
192
+
193
193
def __init__(self):
194
-
194
+
195
195
super( KeyDisplay, self ).__init__()
196
-
196
+
197
197
self.text = cocos.text.Label("", x=100, y=280 )
198
-
198
+
199
199
# To keep track of which keys are pressed:
200
200
self.keys_pressed = set()
201
201
self.update_text()
@@ -215,23 +215,23 @@ This class defines a key_pressed set, which should be the set of keys pressed at
215
215
'modifiers' is a bitwise or of several constants indicating which
216
216
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
217
217
"""
218
-
218
+
219
219
self.keys_pressed.add (key)
220
220
self.update_text()
221
-
221
+
222
222
def on_key_release (self, key, modifiers):
223
223
"""This function is called when a key is released.
224
-
224
+
225
225
'key' is a constant indicating which key was pressed.
226
226
'modifiers' is a bitwise or of several constants indicating which
227
227
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
228
-
228
+
229
229
Constants are the ones from pyglet.window.key
230
230
"""
231
-
231
+
232
232
self.keys_pressed.remove (key)
233
233
self.update_text()
234
-
234
+
235
235
def update_text(self):
236
236
key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed]
237
237
text = 'Keys: '+','.join (key_names)
@@ -243,12 +243,12 @@ With that code, the layer is now fully working. You can press and release keys o
243
243
Handling mouse input is similar. You have three events of interest: on_mouse_press, on_mouse_release and on_mouse_motion. With that, we can define our layer::
@@ -264,16 +264,16 @@ And then add event handlers to update the text when the mouse is moved, and chan
264
264
265
265
def on_mouse_motion (self, x, y, dx, dy):
266
266
"""This function is called when the mouse is moved over the app.
267
-
267
+
268
268
(x, y) are the physical coordinates of the mouse
269
269
(dx, dy) is the distance vector covered by the mouse pointer since the
270
270
last call.
271
271
"""
272
272
self.update_text (x, y)
273
-
273
+
274
274
def on_mouse_press (self, x, y, buttons, modifiers):
275
275
"""This function is called when any mouse button is pressed
276
-
276
+
277
277
(x, y) are the physical coordinates of the mouse
278
278
'buttons' is a bitwise or of pyglet.window.mouse constants LEFT, MIDDLE, RIGHT
279
279
'modifiers' is a bitwise or of pyglet.window.key modifier constants
@@ -295,7 +295,7 @@ You can now play to the demo and change it. Some things you can try are:
295
295
* Change the on_mouse_press handler and remove the mapping to virtual coordinates; note how it behaves strangely after resizing the window
296
296
* Note that the mouse coordinates on screen are physical coordinates, so their range changes when resizing the window; modify the demo to show virtual coordinates.
297
297
* Change the code to be able to move the mouse coordinates label when you drag the mouse (hint: a mouse drag is a sequence of button_press, several motions, and a button_release event)
298
-
* Change the code so the keyboard display also shows the modifiers set at each time
298
+
* Change the code so the keyboard display also shows the modifiers set at each time
299
299
300
300
Where to next?
301
301
--------------
@@ -311,11 +311,11 @@ the entire guide from start to finish.
311
311
312
312
To achieve optimal performance in your 2D
313
313
applications you'll need to work with OpenGL directly. The canonical
314
-
references for OpenGL are `The OpenGL Programming Guide`_ and
314
+
references for OpenGL are `The OpenGL Programming Guide`_ and
315
315
`The OpenGL Shading Language`_.
316
316
317
-
Since cocos2d uses pyglet you shall also check `pyglet Programming Guide`_
318
-
and `pyglet API Reference`_
317
+
Since cocos2d uses pyglet you shall also check `pyglet Programming Guide`_
318
+
and `pyglet API Reference`_
319
319
320
320
There are numerous examples of cocos2d applications in the ``samples/``
321
321
directory of the documentation and source distributions. Keep checking
0 commit comments