Sunday 15 February 2015

python - I can't load images to my pygame program -


I'm new to Pygm and was copying a simple tutorial. I am using Python 3.4.2 but I am running in many issues. This is my code:

  import pygame pygame.init () screen = pygame.display.set_mode ((640,480)) Class game (object): def main (self, screen): clock = Pygame.time.Clock () image = pygame.image.load ('player.png'). Convert () image_x = 320 image_y = 240 while true: clock.tick (30) for events in pygame.event.get (): if event.type == pygame.QUIT: pygame.quit () if event.type = = Pygame.KEYDOWN and event.key == pygame.K_ESCAPE: pygame.quit () image_x + = 10 screen.phil ((200,200,200)) screen. Blunt (image, (320,240)) screen. Bleet (image, (image_x, image_y)) pygame.display.flip () if __name__ == '__main__': pygame.init () screen = pygame.display.set_mode ((640,480)) game (). Main (screen)  

The first problem is that I get an error in opening "error player player". Folder as my .py game program. Second, when I try to get out of the game, the Panjam window freeze and stops responding.

It seems that the little debugging image file was actually in a subfolder to load the image , You must change this line to provide another more accurate path to the file

  image = pygame.image.load ('player.png'). ()  

The same is:

  image = pygame.image.load ('subfolder' + os.sep + 'player.png') Convert Do not forget to add a  import os  line at the top of your file to use  

os.sep Command, which creates a cross platform for the directory separator.


No comments:

Post a Comment