0

I'm creating a card game in pygame for my college project, and a large aspect of the game is how the game's AI reacts to the current situation. I have a function to randomly generate a number within 2 parameters, and this is how long I want the program to wait. All of the code on my ai is contained within an if statement, and once called I want the program to wait generated amount of time, and then make it's decision on what to do. Originally I had:

pygame.time.delay(calcAISpeed(AIspeed))

This would work well, if it didn't pause the rest of the program whilst the AI is waiting, stopping the user from interacting with the program. This means I cannot use while loops to create my timer either.

What is the best way to work around this without going into multi-threading or other complex solutions? My project is due in soon and I don't want to make massive changes. I've tried using pygame.time.Clock functions to compare the current time to the generated one, but resetting the clock once the operation has been performed has proved troublesome.

Thanks for the help and I look forward to your input.

2 Answers2

0

The easiest way around this would be to have a variable within your AI called something like "wait" and set it to a random number (of course it will have to be tweaked to your program speed... I'll explain in the code below.). Then in your update function have a conditional that waits to see if that wait number is zero or below, and if not subtract a certain amount of time from it. Below is a basic set of code to explain this...

class AI(object):
def __init__(self):
    #put the stuff you want in your ai in here
    self.currentwait = 100
    #^^^ All you need is this variable defined somewhere
    #If you want a static number as your wait time add this variable
    self.wait = 100 #Your number here

def updateAI(self):
    #If the wait number is less than zero then do stuff
    if self.currentwait <= 0:
        #Do your AI stuff here

    else:
        #Based on your game's tick speed and how long you want
        #your AI to wait you can change the amount removed from
        #your "current wait" variable
        self.currentwait -= 100 #Your number here

To give you an idea of what is going on above, you have a variable called currentwait. This variable describes the time left the program has to wait. If this number is greater than 0, there is still time to wait, so nothing will get executed. However, time will be subtracted from this variable so every tick there is less time to wait. You can control this rate by using the clock tick rate. For example, if you clock rate is set to 60, then you can make the program wait 1 second by setting currentwait to 60 and taking 1 off every tick until the number reaches zero.

Like I said this is very basic so you will probably have to change it to fit your program slightly, but it should do the trick. Hope this helps you and good luck with your project :)

0

The other option is to create a timer event on the event queue and listen for it in the event loop: How can I detect if the user has double-clicked in pygame?

Community
  • 1
  • 1
marienbad
  • 1,461
  • 1
  • 9
  • 19