Search code examples
pythonpygame

Pygame simple score system


I have created a simple score system for my pygame. but it's pausing the game. I know it's because of time.sleep but I don't how to sort it out.

The score system is to +100 every 5 seconds while start is true, code:

while start == True:
   time.sleep(5)
   score = score + 100 

Full code with indentation: http://pastebin.com/QLd3YTdJ code at line : 156-158

Thank you


Solution

  • If i understand you correctly you dont want the while True: score += 100 loop to block your entire program?

    You should solve it by moving the score adding to a seperate function and use the intervalfunction of APScheduler http://packages.python.org/APScheduler/intervalschedule.html

    from apscheduler.scheduler import Scheduler
    
    # Start the scheduler
    sched = Scheduler()
    sched.start()
    
    # Schedule job_function to be called every 5 seconds
    @sched.interval_schedule(seconds=5)
    def incr_score():
        score += 100
    

    This will result in APScheduler creating a thread for you running the function every 5 seconds.

    you might need to do some changes to the function to make it work but it should get you started at least :).