1

I'm trying to create a simple game using pygame and everything was ok so far. The last couple of days though I realized that a problem occurred with the time.clock(). I read the documentation and the function should count the time of the game since it starts. I wanted to spawn an alien group every 8 seconds and it worked(I'm working on debian os) but as I mentioned the last 2 days it doesn't count properly. The system needs about 20 seconds in real time in order for time.clock to print 8.0 and the aliens to spawn and at first I thought that I messed up with the counters but how can this be, It worked fine in the beginning so I tried to run the same code on the windows partition and it was also fine. So is this a problem with the system clock or anything else? I replaced the time.clock on debian with time.time and also worked fine. Did anyone in the past run into the same problem? Can you help me check if something else is the problem(both operating systems run python 3.6)? If you don't understand something or need anything more just ask me.

Thank you for your time

here is a portion of the time.clock use in the game:

sergeant_spawn_time_limit = 8.0
sergeant_spawn_time = 0.0
if game_stage == 2 or game_stage == 3 or score >= 400:
    if time.clock() - sergeant_spawn_time > sergeant_spawn_time_limit:
        for spawn_sergeant in range(5):
            sergeant = AlienSergeant(display_width, display_height, 50, 88)
            all_sprites_list.add(sergeant)
            alien_sergeant_list.add(sergeant)
        sergeant_spawn_time = time.clock()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
M2kk
  • 79
  • 2
  • 10

1 Answers1

1

The behaviour of time.clock() is platform dependend:

time.clock()
On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name.

On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

Deprecated since version 3.3: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.

So it's really the wrong tool to use here. Either use time.time() or pygame's clock or it's build-in event system. You'll find a lot of examples, like here or here.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • Thank you, I just found it weird that it worked all this time but suddenly started to behave like this, I guess it was luck all this time. – M2kk Nov 04 '18 at 21:46