How to fix Python Pygame image error in window










1















I was trying to load a png file to python by pygame and it doesn't work
this is my code:



import pygame
from pygame.locals import *
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()
carImage = pygame.image.load('you.png')
def car(x,y):
gameDisplay.blit(carImage,(x,y))
x = (display_width * 0.45)
y = (display_height * 0.8)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill(white)
car(x,y)
pygame.display.update()
clock.tick(24)
pygame.quit()
quit()


and it says:




Traceback (most recent call last):



File "C:/Users/Dawn/PycharmProjects/snakegame/snake.py", line 13, in
carImage = pygame.image.load('you.png')



pygame.error: Couldn't open you.png




Please help me I don't know why this keep showing.



I'm using window 10 now and I did the C: ....you.png method
but it still doesn't work.










share|improve this question




























    1















    I was trying to load a png file to python by pygame and it doesn't work
    this is my code:



    import pygame
    from pygame.locals import *
    pygame.init()
    display_width = 800
    display_height = 600
    black = (0,0,0)
    white = (255,255,255)
    red = (255,0,0)

    gameDisplay = pygame.display.set_mode((display_width,display_height))
    pygame.display.set_caption("Game")
    clock = pygame.time.Clock()
    carImage = pygame.image.load('you.png')
    def car(x,y):
    gameDisplay.blit(carImage,(x,y))
    x = (display_width * 0.45)
    y = (display_height * 0.8)
    crashed = False
    while not crashed:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    crashed = True
    gameDisplay.fill(white)
    car(x,y)
    pygame.display.update()
    clock.tick(24)
    pygame.quit()
    quit()


    and it says:




    Traceback (most recent call last):



    File "C:/Users/Dawn/PycharmProjects/snakegame/snake.py", line 13, in
    carImage = pygame.image.load('you.png')



    pygame.error: Couldn't open you.png




    Please help me I don't know why this keep showing.



    I'm using window 10 now and I did the C: ....you.png method
    but it still doesn't work.










    share|improve this question


























      1












      1








      1








      I was trying to load a png file to python by pygame and it doesn't work
      this is my code:



      import pygame
      from pygame.locals import *
      pygame.init()
      display_width = 800
      display_height = 600
      black = (0,0,0)
      white = (255,255,255)
      red = (255,0,0)

      gameDisplay = pygame.display.set_mode((display_width,display_height))
      pygame.display.set_caption("Game")
      clock = pygame.time.Clock()
      carImage = pygame.image.load('you.png')
      def car(x,y):
      gameDisplay.blit(carImage,(x,y))
      x = (display_width * 0.45)
      y = (display_height * 0.8)
      crashed = False
      while not crashed:
      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      crashed = True
      gameDisplay.fill(white)
      car(x,y)
      pygame.display.update()
      clock.tick(24)
      pygame.quit()
      quit()


      and it says:




      Traceback (most recent call last):



      File "C:/Users/Dawn/PycharmProjects/snakegame/snake.py", line 13, in
      carImage = pygame.image.load('you.png')



      pygame.error: Couldn't open you.png




      Please help me I don't know why this keep showing.



      I'm using window 10 now and I did the C: ....you.png method
      but it still doesn't work.










      share|improve this question
















      I was trying to load a png file to python by pygame and it doesn't work
      this is my code:



      import pygame
      from pygame.locals import *
      pygame.init()
      display_width = 800
      display_height = 600
      black = (0,0,0)
      white = (255,255,255)
      red = (255,0,0)

      gameDisplay = pygame.display.set_mode((display_width,display_height))
      pygame.display.set_caption("Game")
      clock = pygame.time.Clock()
      carImage = pygame.image.load('you.png')
      def car(x,y):
      gameDisplay.blit(carImage,(x,y))
      x = (display_width * 0.45)
      y = (display_height * 0.8)
      crashed = False
      while not crashed:
      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      crashed = True
      gameDisplay.fill(white)
      car(x,y)
      pygame.display.update()
      clock.tick(24)
      pygame.quit()
      quit()


      and it says:




      Traceback (most recent call last):



      File "C:/Users/Dawn/PycharmProjects/snakegame/snake.py", line 13, in
      carImage = pygame.image.load('you.png')



      pygame.error: Couldn't open you.png




      Please help me I don't know why this keep showing.



      I'm using window 10 now and I did the C: ....you.png method
      but it still doesn't work.







      python pygame






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 4:43









      Aqueous Carlos

      352314




      352314










      asked Nov 14 '18 at 3:14









      yeomyung Kimyeomyung Kim

      61




      61






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Based on this answer, it's recommended to use relative paths instead. It's always better to do so, since you don't have to care about '', '/' or OS (someone already did it for us :v).



          The problem seems to be it, because the code below works well for me. It's been considered you have an images_store folder to store your images at same father directory as your .py file (of course, you can change it any way you want).



          import pygame
          import os.path as osp
          from pygame.locals import *


          pygame.init()

          display_width, display_height = 800, 600
          black = (0,0,0)
          white = (255,255,255)
          red = (255,0,0)

          current_path = osp.dirname(__file__) # Where your .py file is located
          image_path = osp.join(current_path, 'images_store') # The image folder path
          carImage = pygame.image.load(osp.join(image_path, 'you.png'))


          gameDisplay = pygame.display.set_mode((display_width, display_height))
          pygame.display.set_caption("Game")
          clock = pygame.time.Clock()

          def car(x,y):
          gameDisplay.blit(carImage, (x, y))

          x = (display_width * 0.45)
          y = (display_height * 0.8)
          crashed = False
          while not crashed:
          for event in pygame.event.get():
          if event.type == pygame.QUIT:
          crashed = True
          gameDisplay.fill(white)
          car(x,y)
          pygame.display.update()
          clock.tick(24)
          pygame.quit()
          quit()


          p.s.1 - See more information about os.path here.



          p.s.2 - I'm using MacOS.






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292682%2fhow-to-fix-python-pygame-image-error-in-window%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Based on this answer, it's recommended to use relative paths instead. It's always better to do so, since you don't have to care about '', '/' or OS (someone already did it for us :v).



            The problem seems to be it, because the code below works well for me. It's been considered you have an images_store folder to store your images at same father directory as your .py file (of course, you can change it any way you want).



            import pygame
            import os.path as osp
            from pygame.locals import *


            pygame.init()

            display_width, display_height = 800, 600
            black = (0,0,0)
            white = (255,255,255)
            red = (255,0,0)

            current_path = osp.dirname(__file__) # Where your .py file is located
            image_path = osp.join(current_path, 'images_store') # The image folder path
            carImage = pygame.image.load(osp.join(image_path, 'you.png'))


            gameDisplay = pygame.display.set_mode((display_width, display_height))
            pygame.display.set_caption("Game")
            clock = pygame.time.Clock()

            def car(x,y):
            gameDisplay.blit(carImage, (x, y))

            x = (display_width * 0.45)
            y = (display_height * 0.8)
            crashed = False
            while not crashed:
            for event in pygame.event.get():
            if event.type == pygame.QUIT:
            crashed = True
            gameDisplay.fill(white)
            car(x,y)
            pygame.display.update()
            clock.tick(24)
            pygame.quit()
            quit()


            p.s.1 - See more information about os.path here.



            p.s.2 - I'm using MacOS.






            share|improve this answer



























              1














              Based on this answer, it's recommended to use relative paths instead. It's always better to do so, since you don't have to care about '', '/' or OS (someone already did it for us :v).



              The problem seems to be it, because the code below works well for me. It's been considered you have an images_store folder to store your images at same father directory as your .py file (of course, you can change it any way you want).



              import pygame
              import os.path as osp
              from pygame.locals import *


              pygame.init()

              display_width, display_height = 800, 600
              black = (0,0,0)
              white = (255,255,255)
              red = (255,0,0)

              current_path = osp.dirname(__file__) # Where your .py file is located
              image_path = osp.join(current_path, 'images_store') # The image folder path
              carImage = pygame.image.load(osp.join(image_path, 'you.png'))


              gameDisplay = pygame.display.set_mode((display_width, display_height))
              pygame.display.set_caption("Game")
              clock = pygame.time.Clock()

              def car(x,y):
              gameDisplay.blit(carImage, (x, y))

              x = (display_width * 0.45)
              y = (display_height * 0.8)
              crashed = False
              while not crashed:
              for event in pygame.event.get():
              if event.type == pygame.QUIT:
              crashed = True
              gameDisplay.fill(white)
              car(x,y)
              pygame.display.update()
              clock.tick(24)
              pygame.quit()
              quit()


              p.s.1 - See more information about os.path here.



              p.s.2 - I'm using MacOS.






              share|improve this answer

























                1












                1








                1







                Based on this answer, it's recommended to use relative paths instead. It's always better to do so, since you don't have to care about '', '/' or OS (someone already did it for us :v).



                The problem seems to be it, because the code below works well for me. It's been considered you have an images_store folder to store your images at same father directory as your .py file (of course, you can change it any way you want).



                import pygame
                import os.path as osp
                from pygame.locals import *


                pygame.init()

                display_width, display_height = 800, 600
                black = (0,0,0)
                white = (255,255,255)
                red = (255,0,0)

                current_path = osp.dirname(__file__) # Where your .py file is located
                image_path = osp.join(current_path, 'images_store') # The image folder path
                carImage = pygame.image.load(osp.join(image_path, 'you.png'))


                gameDisplay = pygame.display.set_mode((display_width, display_height))
                pygame.display.set_caption("Game")
                clock = pygame.time.Clock()

                def car(x,y):
                gameDisplay.blit(carImage, (x, y))

                x = (display_width * 0.45)
                y = (display_height * 0.8)
                crashed = False
                while not crashed:
                for event in pygame.event.get():
                if event.type == pygame.QUIT:
                crashed = True
                gameDisplay.fill(white)
                car(x,y)
                pygame.display.update()
                clock.tick(24)
                pygame.quit()
                quit()


                p.s.1 - See more information about os.path here.



                p.s.2 - I'm using MacOS.






                share|improve this answer













                Based on this answer, it's recommended to use relative paths instead. It's always better to do so, since you don't have to care about '', '/' or OS (someone already did it for us :v).



                The problem seems to be it, because the code below works well for me. It's been considered you have an images_store folder to store your images at same father directory as your .py file (of course, you can change it any way you want).



                import pygame
                import os.path as osp
                from pygame.locals import *


                pygame.init()

                display_width, display_height = 800, 600
                black = (0,0,0)
                white = (255,255,255)
                red = (255,0,0)

                current_path = osp.dirname(__file__) # Where your .py file is located
                image_path = osp.join(current_path, 'images_store') # The image folder path
                carImage = pygame.image.load(osp.join(image_path, 'you.png'))


                gameDisplay = pygame.display.set_mode((display_width, display_height))
                pygame.display.set_caption("Game")
                clock = pygame.time.Clock()

                def car(x,y):
                gameDisplay.blit(carImage, (x, y))

                x = (display_width * 0.45)
                y = (display_height * 0.8)
                crashed = False
                while not crashed:
                for event in pygame.event.get():
                if event.type == pygame.QUIT:
                crashed = True
                gameDisplay.fill(white)
                car(x,y)
                pygame.display.update()
                clock.tick(24)
                pygame.quit()
                quit()


                p.s.1 - See more information about os.path here.



                p.s.2 - I'm using MacOS.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 4:23









                roncharoncha

                112




                112



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292682%2fhow-to-fix-python-pygame-image-error-in-window%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                    Museum of Modern and Contemporary Art of Trento and Rovereto