[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Problem clock.tick() and Threads



On 24/10/05, Juju Cece <jujucece@xxxxxxxxx> wrote:
Hi,

I'm developping a Racing Top View Game (pyRacerz) and I want to add a
Network Game Feature.

To do that, I've created 2 Threads: 1 to receive datas and 1 to send.

On my main Loop I use:
clock.tick (100)
To limit the FPS (but I update the Frame 1 time on 2)

The problem is, when I use "clock.tick(100)", the Threads don't run,
because I think, there's not enough CPU.

If I substitute " clock.tick(100)" by "pygame.time.wait(10)", the
Threads work well...

But I want a precise FPS to have a smooth and playable game.

The problem comes, I think, from the pygame.time.delay" function which
is surely used by the clock.tick(100), and which consumes a high
amount of CPU.

Do you have an idea on how I can solve my problem ?

(Perhaps twisted will solve the problem, but I don't wanttoo much dependencies)

Thanks in advance,
Jujucece

You could use this tiny module that I wrote if you like.
# Copyright (C) 2005  Adam Bark apb_4@xxxxxxxxxxxxxxxxxxxxx

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""To use this module call start() at the start of
your main loop and at the end call stop(n), where
n is the frame rate you want."""

import time

def start():
    global t
    t = time.time()
        
def stop(rate):
    global t
    frmlen = 1.0/rate
    try:
        time.sleep(frmlen - (time.time() - t))
    except:
        IOError
        
if __name__ == "__main__":
    import pygame, math
    clock = pygame.time.Clock()
    for n in range(100):
        start()
        time.sleep(0.000001)
        clock.tick()
        stop(50)
    print clock.get_fps()