1

I'm an ameatur coder and I have recently started with pygame and I have been coding some examples and I have just come to one that won't load properly when executed. I am pretty sure I have written it correctly but I can't figure out why it won't run. I am running python 3.6 and Pygame 1.9.3.*

*I know the pygame version is 1.9 of sorts but I don't know the last digit.

Here's the code:

# This just imports all the Pygame modules
import pygame

# This MUST BE the first thing you put into a program!
pygame.init()
# This tells pygame to open a windo 640 pixels by 480
screen = pygame.display.set_mode((640, 480))

# This is called a while loop
# This is the simplest form of an event loop
# This line procceses the window: Such as telling it to close when the user     hits the Escape Key
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
# Y = DOWN
class Game(object):
    def main(self, screen):
        image = pygame.image.load('player.png')

        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key ==     pygame.K_ESCAPE:
                    return

            screen.fill((200, 200, 200))
            screen.blit(image, (320, 240))
            pygame.display.flip()

    if __name__ == '__main__':
        pygame.init()
        screen = pygame.display.set_mode((640, 480))
        Game().main(screen)
11
  • I may have added unneeded code by accident. Commented Jan 15, 2018 at 15:55
  • 1
    You used copy&paste programming without knowing what the code does. Take a step back and try to understand the code. Start with the first while loop. Commented Jan 15, 2018 at 15:59
  • Sorry I coded all of this from a video for a class but they broke everything up so I thought everything was supposed to be all in one file. Commented Jan 15, 2018 at 16:04
  • Because I understand what it says. I just don't know why it won't run. Commented Jan 15, 2018 at 16:05
  • I just now ran this but it still won't display what I want it to display: Commented Jan 15, 2018 at 16:06

1 Answer 1

2

I have modified your code so it will now run. The main issue that I detect is that you don't understand exactly what the code is doing when it is ran.

Issues fixed:

  • Removed a pointless while loop and pygame initializations that were done below in the beginning of the program's execution anyway.

  • Unindented the if __name__ == "__main__": branch. This branch is never part of a class. In a class you have methods and variables. That is it.

Other than those issues the code is alright but make sure you understand what it does before moving on.

I hope this answer helped you and if you have any further questions please feel free to post a comment below!

Revised code example:

# This just imports all the Pygame modules
import pygame

class Game(object):
    def main(self, screen):
        image = pygame.image.load('player.png')

        while 1:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    return
                if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                    return

            screen.fill((200, 200, 200))
            screen.blit(image, (320, 240))
            pygame.display.flip()



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((640, 480))
    Game().main(screen)
Sign up to request clarification or add additional context in comments.

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.