User avatar
sav25
Authorised Reseller
Authorised Reseller
Posts: 379
Joined: Thu Aug 30, 2012 7:18 pm
Location: Southend-on-Sea, Essex, UK
Contact: Website Twitter

PyGame not initialising without pressing Ctrl+C

Wed Nov 04, 2015 10:06 pm

Hi

I have a script for a robot which uses PyGame to enable the use of a 2.4Ghz remote control.

When I first boot up and run my script, it runs through the various imports, initialises PyGame and then starts. That's fine.

When I exit that script, and then start the script again, it hangs on the PyGame initialisation :x . I know this because I put a 'print' after each part of startup to let me debug things like this.

The specific part it struggles with is:

Code: Select all

pygame.display.init()
Nothing happens, it simply doesn't get past this line. I've also tried the following alternative, but the same issue occurs:

Code: Select all

pygame.init()
Has anyone experienced this? Why does it work the first time, then need a keyboard press the second time? I can get it to work now with my plugged in keyboard by pressing Ctrl+C (seems to make it continue), but when I move to only the 2.4Ghz wireless keyboard I won't be able to get past this error.

Thanks in advance

User avatar
sav25
Authorised Reseller
Authorised Reseller
Posts: 379
Joined: Thu Aug 30, 2012 7:18 pm
Location: Southend-on-Sea, Essex, UK
Contact: Website Twitter

Re: PyGame not initialising without pressing Ctrl+C

Wed Nov 04, 2015 10:14 pm

Once again posting a question and then finding what appears to be the answer straight away :D I added a 'time.sleep(0.5)' between the various PyGame setup lines and now it seems cured.

Like this:

Code: Select all

pygame.display.init() #or pygame.init()  ....but that hangs. BOTH HANG!
print "1"
time.sleep(0.5)
width = 320
print "2"
time.sleep(0.5)
height = 240
print "3"
time.sleep(0.5)
display = pygame.display.set_mode((width,height))
print "4"
time.sleep(0.5)
black = (0,0,0)
print "5"
time.sleep(0.5)
white = (255,255,255)
print "pygame initialised"
time.sleep(0.5)
Perhaps Python was tripping up over itself, if that happens? :?:

User avatar
sav25
Authorised Reseller
Authorised Reseller
Posts: 379
Joined: Thu Aug 30, 2012 7:18 pm
Location: Southend-on-Sea, Essex, UK
Contact: Website Twitter

Re: PyGame not initialising without pressing Ctrl+C

Thu Nov 05, 2015 1:11 pm

Just in case anyone else finds this with the same problem, I found another reason why it may not have been initialising properly.

I had been exiting my code without shutting down PyGame using 'pygame.quit()' - this supposedly can cause problems.

I now have my code in a try: except: block and all is well.

Hooray! :P

Return to “Python”