5

Whenever I run my code the Python Window that shows up does not respond.
Is there something wrong with my code or do I have to re-install pygame and python?
I get a black pygame window and then it turns white and says not responding?
Also I am new to this so please make this as simple as possible. I tried looking everywhere for the answer but could not get it in a way that I could understand.
Please help me out. Thanks :)

1 - Import library

import pygame
from pygame.locals import *

2 - Initialize the game

pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))

3 - Load Images

player = pygame.images.load("resources/images/dude.png")

4 - keep looping through

while 1:
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    # 6 - draw the screen elements
    screen.blit(player, (100,100))
    # 7 - update the screen
    pygame.display.flip()
    # 8 - loop through the events
    for event in pygame.event.get():
        # check if the event is the X button
        if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit()
            exit(0)
9
  • 1
    try putting print statements in to find out where it crashes, a good start would be to put one before the while loop to see if it is getting there or not before it stops working. Commented Nov 24, 2013 at 18:08
  • If if event.type--pygame.QUIT: is actually in your code, you do have a problem. You want to use == instead of --. Commented Nov 24, 2013 at 18:15
  • i changed it from -- to == but it still doesn't work. What should I do? Commented Nov 24, 2013 at 18:32
  • Does it give you an error message? Commented Nov 24, 2013 at 18:35
  • 2
    BTW: should be pygame.image.load in place of pygame.images.load - image without s Commented Nov 24, 2013 at 18:41

3 Answers 3

2

Don't import pygame.locals. It is actually unnecessary, since you are already importing pygame.

Also, as @furas said, it should be:

player = pygame.image.load("resources/images/dude.png")

Not:

player = pygame.images.load("resources/images/dude.png")

This will clear up some of the problems in your code.

Sign up to request clarification or add additional context in comments.

Comments

2

From my personal experience,if you run pygame code from IDLE it often does not respond at all.Try saving your project as a .py file and then run it with python.exe.It always works for me. And as furas said use

player = pygame.image.load("resources/images/dude.png")

instead of

player = pygame.images.load("resources/images/dude.png")

1 Comment

Best Answer! You must write pygame code and save it to a .py file. Then it works. It kept freezing using jupyter notebook and also frooze when using python shell. Running the .py file did the trick!
2

Replace that for loop with this

 while not done:
   for event in pygame.event.get():
     if event.type == pygame.QUIT:
       done = True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.