Developing games using Cocos2d in Python
Introduction
Implementation of Cocos2d on Objective-C is widely used for developing games for the iPhone. According to the official website, the number of games on this engine already exceeds 1800. He was mentioned more than once on Habré. Other ports (cocos2d-x in C ++ and cocos2d-android in Java) are also known and gaining popularity. However, the progenitor of these engines, the original Cocos2d was undeservedly ignored. I will try to fill this gap.
Cocos2d written in Python using pyglet library. Due to its use of Python and its simple architecture, it is cross-platform and well suited for rapid prototyping and testing of game concepts. The engine is somewhat behind its descendants in terms of the implementation of some features, but in practice this does not cause difficulties. The problem with the engine is the poor quality of documentation: the official guide describes the most basic concepts, and then replete with permanent TODO plugs. The API also contains few comments. However, the kit includes examples and tests that allow you to familiarize yourself with the basic programming techniques.
So, install Cocos2d and get started.
Scenes and flow control
A scene (class
cocos.scene.Scene
) is one level or screen of your game. All game objects belonging to a particular level are placed on the corresponding scene. Singleton director ( cocos.director.director
) is used to control the scenes .
During the game, director controls the stack of scenes. A scene stack is a very convenient concept. When a new scene is placed on the stack, the director proceeds to execute it, and the previous scene is paused. When the top scene is removed from the stack, the director resumes the previous scene. Thus, for example, you can implement nested levels or in-game menus. For operations with the scene stack, the methods of the director are used , and . A call is equivalent to a consecutive call and .# задействуем cocos2d
import cocos
# инициализируем director; аргументами можно передать размер окна, заголовок и пр.
cocos.director.director.init()
# создаем новую пустую сцену и передаем ее директору, запуская таким образом основной цикл
cocos.director.director.run(cocos.scene.Scene())
push()
pop()
replace()
replace()
pop()
push()
Scene structure
A scene is a collection of objects (nodes) organized in a tree. The root of the tree is the scene itself. Transformations of the parent (position, angle, scale) in this model affect the transformation of children. In this way, you can build complex composite objects and easily control their display. The engine provides a wide range of ready-made node primitives (for example, layer (
cocos.layer.Layer
), sprite ( cocos.sprite.Sprite
), text ( cocos.text.Label
), particle system ( cocos.particle.ParticleSystem
), and many others). You can write your nodes, inheriting from cocos.cocosnode.CocosNode
or derived classes.
The scene structure is formed using node methods , and .import cocos
cocos.director.director.init()
# создаем новую пустую сцену
scene = cocos.scene.Scene()
# добавляем узел с текстом
scene.add(cocos.text.Label("Hello world!", position = (100, 200))
# исполняем сцену
cocos.director.director.run(scene)
add()
remove()
kill()
Objects can control their (and not only their) state using callback functions, which are quite simple to create. To illustrate, create a derived node with rotating text:
import cocos
class RotatingText(cocos.text.Label):
# инициализируем базовый класс и настраиваем обратный вызов
def __init__(self, text = "", position = (0, 0)):
super(RotatingText, self).__init__(text, position)
self.schedule(self.update)
# функция обратного вызова, модифицируем угол поворота в зависимости от прошедшего времени
def update(self, dt):
self.rotation += dt * 20
cocos.director.director.init()
scene = cocos.scene.Scene()
scene.add(RotatingText("Hello world!", (100, 200)))
cocos.director.director.run(scene)
Actions
The above is already enough to write a simple demo by creating objects and setting their behavior. However, what makes Cocos2d so flexible and powerful is its actions system, and now we’ll figure it out.
An action is an instruction to an object to change its state. Actions are instantaneous and interval. In addition, actions are absolute and relative. The list of standard actions includes:
import cocos
cocos.director.director.init()
scene = cocos.scene.Scene()
label = cocos.text.Label("Hello world!", position = (100, 200))
scene.add(label)
# текстовая метка будет перемещаться вправо на 200 пикселей в течение 5 секунд
label.do(cocos.actions.MoveBy((200, 0), duration = 5))
cocos.director.director.run(scene)
- displacement (instantaneous -
Place
, interval -MoveTo
,MoveBy
,JumpTo
,JumpBy
) - scaling (
ScaleTo
,ScaleBy
) - rotation (
RotateTo
,RotateBy
) - visibility control object (
Show
,Hide
,Blink
,ToggleVisibility
) - transparency control (
FadeIn
,FadeOut
,FadeTo
)
+
and |
actions can be combined. Actions combined by summation are executed sequentially one after another.
Actions combined through a disjunction are executed in parallel.
In addition, there are modifier actions that affect how the actions are applied. Standard modifier actions:# метка переместится вверх на 100 пикселей за 5 секунд и затем растворится за 3 секунды
label.do(cocos.actions.MoveBy((0, 100), 5) + cocos.actions.FadeOut(3))
# метка переместится вверх на 100 пикселей, одновременно поворачиваясь на 90 градусов, за 5 секунд
label.do(cocos.actions.MoveBy((0, 100), 5) | cocos.actions.RotateBy(90, 5))
- repeat (
Repeat
) - repeats the specified action endlessly - acceleration (
Accelerate
,AccelDeccel
) - change the acceleration of an action in its process - change of speed (
Speed
) - changes the execution time of an action - reverse action (
Reverse
) - this action will be performed in the opposite direction, if possible - other
# метка будет бесконечно перемещаться влево-вправо на 200 пикселей с периодом в 3 секунды
action = cocos.actions.MoveBy((200, 0), 3)
label.do(cocos.actions.Repeat(action + cocos.actions.Reverse(action)))
Debugging
Cocos2d contains a built-in Python interpreter that is invoked by default by pressing Ctrl-I. Using the interpreter, you can get a complete introspection of the game and conduct development interactively. Execution does not stop.
Conclusion
Unfortunately, many issues (such as user interaction, event handling, scene transitions, and effects) are outside the scope of this article. If the Habrovsk citizens are interested in these topics, I will try to present them in the next article.
Cocos2d official website