Pygame mess up

+1 Jesse Cox · December 1, 2014
Been following this tutorial:



Can't get the little man to show up. Here's my code:

import pygame

class Game(object):
    def main(self, screen):
        
        clock = pygame.time.Clock()
        
        image = pygame.image.load("player.png")
        
        while 1:
            clock.tick(30)
            
            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)


Thanks to anyone for their help. :)

Post a Reply

Replies

Oldest  Newest  Rating
+1 Jesse Cox · December 1, 2014
OMG, thanks. Feel like an idiot :(
+1 Doug Fresh · December 1, 2014
No problem, don't feel like an idiot. We have all been there. Failures are infinitely more instructive than successes. 
0 Doug Fresh · December 1, 2014
When you say, "Can't get the little man to show up", do you have the "player.png" image in the same folder as your program?
0 Doug Fresh · December 1, 2014
Assuming your image is in the correct folder, the only other issue you may be having is with indentation.

try this:

import pygame

class Game(object):
    def main(self, screen):
        
        clock = pygame.time.Clock()
        
        image = pygame.image.load("player.png")
        image_x = 320
        image_y = 240
        
        while 1:
            clock.tick(30)
            
            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)

Note the indentation starting at screen.fill(200,200,200). Because this info was never in the while 1 loop, it was never called, therefore never shown.
0 Jesse Cox · December 3, 2014
You may.
  • 1

Python

107,210 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator