Forum Archive

Scene and turn-based game

vs212

I am making a turn-based game in which I have units (armies). How to set the sequential movement of armies in the Scene? Everything works in parallel in the Scene. And my armies are all moving together. I want them to move one by one. For example: unit1 and unit2 is moving together, not one by one. :((((
```python
actions = []
actions.append(A.move_by(50, 0, d))
actions.append(A.wait(p))
self.unit1.run_action(A.sequence(actions))
actions = []
actions.append(A.move_by(0, 50, d))
actions.append(A.wait(p))
self.unit2.run_action(A.sequence(actions))

mikael

@vs212, here is one example:


from functools import partial

from scene import *
from scene import Action as A

class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.unit1 = SpriteNode('spc:PlayerShip1Orange')
        self.unit1.position = self.size / 2
        self.add_child(self.unit1)
        self.unit2 = SpriteNode('spc:PlayerShip1Orange')
        self.unit2.position = self.size / 4
        self.add_child(self.unit2)

    def touch_began(self, touch):
        unit1_actions = A.sequence(
            A.move_by(0, 50),
            A.wait(1),
        )
        unit2_actions = A.sequence(
            A.move_by(0, 50),
            A.wait(1),
        )
        self.unit1.run_action(
            A.sequence(
                unit1_actions,
                A.call(partial(
                    self.unit2.run_action,
                    unit2_actions))
            )
        )

run(MyScene())

mikael

@vs212, but for this, I would suggest you pip install pythonista-scripter, which lets you do the following:


from scene import *

from scripter import *


class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.unit1 = SpriteNode('spc:PlayerShip1Orange')
        self.unit1.position = self.size / 2
        self.add_child(self.unit1)
        self.unit2 = SpriteNode('spc:PlayerShip1Orange')
        self.unit2.position = self.size / 4
        self.add_child(self.unit2)

        start_scripter(self.view)

    def touch_began(self, touch):
        self.move_units()

    @script
    def move_units(self):
        move_by(self.unit1, 0, 50)
        yield
        move_by(self.unit2, 0, 50)


run(MyScene())

mikael

(See docs at https://github.com/mikaelho/scripter.)

vs212

@mikael thank you. This great, but I have a lot of units. And the “call partial, call partial, call partial” will be very cumbersome. :((

vs212

@mikael thank you. That's what I need. I can’t install yet. But I will try.

vs212

@mikael Thank you very much. A very powerful package!

vs212

@mikael
move_by(self.unit1, 0, 50)
yield 0.5

it works great, but how to change the duration move_to from 0.5 to 0.2?
I set Scripter.default_duration but it didn’t affect :((

mikael

@vs212, always nice to hear when people find a use for it.

I think you found the duration parameter. See below an example for changing the global default as well.

from scene import *

import scripter as s

s.default_duration = 0.2


class MyScene (Scene):
    def setup(self):
        self.background_color = 'midnightblue'
        self.unit1 = SpriteNode('spc:PlayerShip1Orange')
        self.unit1.position = self.size / 2
        self.add_child(self.unit1)
        self.unit2 = SpriteNode('spc:PlayerShip1Orange')
        self.unit2.position = self.size / 4
        self.add_child(self.unit2)

        s.start_scripter(self.view)

    def touch_began(self, touch):
        self.move_units()

    @s.script
    def move_units(self):
        s.move_by(self.unit1, 0, 50, duration=1.0)
        yield
        s.move_by(self.unit2, 0, 50)


run(MyScene())

vs212

@mikael My mistake was - I didn't use ‘ duration=’ ))

Thank you!

vs212

@mikael It's not just nice. You saved me. I've already climbed into such abysses. And here everything is so elegant and simple. You are an excellent programmer.