Re: how would you pause game?

228 views
Skip to first unread message

Winston Wolff

unread,
Mar 6, 2013, 5:33:01 PM3/6/13
to pyglet...@googlegroups.com
I don't know off the top of my head but I know that cocos2d ( http://cocos2d.org ) implements pause, and it uses Pyglet. You could investigate that.

-ww

On Mar 6, 2013, at 2:21 PM, Joseph Clark <joecl...@gmail.com> wrote:

> Quick question: is there an easy way to pause and unpause the game/clock/scheduler with pyglet? If now, how would you program this?
>
> --
> You received this message because you are subscribed to the Google Groups "pyglet-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users...@googlegroups.com.
> To post to this group, send email to pyglet...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Petr Viktorin

unread,
Mar 6, 2013, 5:52:10 PM3/6/13
to pyglet...@googlegroups.com
You can use something like this:

class AdjustableClock(pyglet.clock.Clock):
__time = 0
speed = 1

def __init__(self):
pyglet.clock.Clock.__init__(self, time_function=self.get_time)
pyglet.clock.schedule(self.advance)

def advance(self, time):
self.__time += time * self.speed

def get_time(self):
return self.__time

..and use an instance of that clock everywhere (except e.g. animations
in the pause menu).
To pause, set the clock's speed to zero.

Bartosz Zabołotny

unread,
Aug 29, 2018, 4:39:30 PM8/29/18
to pyglet-users
I've tried to use it, but function scheduling seems to not be working with that custom clock.
Used following code, but while pyglet.clock scheduled events occur, TestClock scheduled ones do not...
What's wrong with it?

import pyglet

class TestClock(pyglet.clock.Clock):
    __time = 0
    speed = 1.0


    def __init__(self):
        pyglet.clock.Clock.__init__(self, time_function=self.get_time)
        pyglet.clock.schedule(self.advance)

    def advance(self, time):
        self.__time += time * self.speed

    def get_time(self):
        return self.__time
   
    def set_speed(self, dt=0, speed=1.0):
        print "Set speed", speed
        self.speed = speed

class A(object):
    def __init__(self):
        self.clk = TestClock()
        self.clk.schedule_interval(self.te, 0.3)
    def te(dt=0.0):
        print "TEST", dt
    def up(self, dt):
        print 'up'
 
window = pyglet.window.Window()
label = pyglet.text.Label('Hello, world',
                          font_name='Times New Roman',
                          font_size=36,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')
@window.event
def on_draw():
    window.clear()
    label.draw()
       
def x():
    print "X"
       
testobj = A()

pyglet.clock.schedule_interval(testobj.up, 1/6.0)
testobj.clk.schedule_once(x, 2)

pyglet.clock.schedule_once(testobj.clk.set_speed, 5, speed=10.0)

pyglet.app.run()

BZab

unread,
Sep 2, 2018, 11:12:16 AM9/2/18
to pyglet-users
Changed 'advance' function to:
    def advance(self, time):
        self.__time += time * self.speed
        self.tick()

Now works
Reply all
Reply to author
Forward
0 new messages