Reputation: 209
Whenever I start up the program the shell works, but the pygame window instantly stops responding. I have drawn things on pygame before, but this is the first time this has happened. There is no error message, just when i hover my cursor over it the is the loading symbol, and after a while windows asks my to wait or stop the program. (I used the code snippet in javascript but the code is in python. Sorry for my newbie coding layout.)
import pygame
from time import sleep
def multitype(string, whole_str_time):
len_of_str = len(string)
no_of_character = 0
secs_bet_let = whole_str_time / len_of_str
while no_of_character != len_of_str - 1:
print(string[no_of_character], end = '')
no_of_character += 1
sleep(secs_bet_let)
print(string[no_of_character])
pygame.init()
black = (0,0,0)
white = (255,255,255)
playercoords_a = (275,425)
playercoords_b = (275,475)
playercoords_c = (225,475)
playercoords_d = (225,425)
playertotalcoords = (playercoords_a, playercoords_b, playercoords_c, playercoords_d)
windowSurface = pygame.display.set_mode((500, 500),0,32)
windowSurface.fill((0,0,0,))
xmod = 0
ymod = 0
pygame.draw.polygon(windowSurface,white,((275,425),(275,475),(225,475),(225,425)))
pygame.display.update()
sleep(5)
multitype('type up to go up, left to go left, right to go right and down to go down',2)
while True:
command = input()
if command == 'up':
xmod += 50
print('updated')
elif command == 'right':
ymod += 50
print('updated')
elif command == 'left':
ymod -= 50
print('updated')
elif command == 'down':
xmod -= 50
print('updated')
else :
print('invalid instruction')
pygame.draw.polygon(windowSurface,white,((275 + xmod,425 + ymod),(275 + xmod,475 + ymod),(225 + xmod,475 + ymod),(225 + xmod,425 + ymod)))
pygame.display.update()
Upvotes: 1
Views: 458
Reputation: 11
I think the problem is pygame code alway need this loop at the end but you can put it with the last part.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Upvotes: 0
Reputation: 365657
When you do this:
sleep(5)
… you're putting the entire interpreter to sleep for 5 seconds, during which time it can't respond to any events, update the screen, or do anything else.
And then you call this function:
multitype('type up to go up, left to go left, right to go right and down to go down',2)
… which also does a bunch of sleep
s for seconds at a time without processing events.
And then, you go into a loop that waits on input()
. As long as it's waiting for keyboard input, it's again not processing events.
After a few seconds of ignoring events and updates, Windows will detect that the program is non-responsive and ask you whether you want to wait or stop the program.
If you want your app to be responsive, you need to process events, and call pygame.display.update()
, at least a few times per second. That means breaking up long sleep
s.
If you work through any PyGame tutorial, there will be a section on ways to delay your game logic that don't involve hanging the whole program. The two main options are to write a frame loop (which updates X times per second no matter what), or an event loop (which updates whenever the OS says there are events). Then, instead of sleep(5)
, you have to keep track of when you want to do the next thing, keep looping, and do the next thing when you reach that time.
And it means input
and PyGame usually don't play well together. You can force them to play well, by using a background thread, or by using nonblocking stdin, or in other ways, but there's no trivial fix for that. (Most games, and most GUIs, take their input from within the graphical windows, not from the console.)
Upvotes: 1