• Login
Community
  • Login

Notepad++ NPPEXEC Unable to Properly Run Pygame Code

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
6 Posts 3 Posters 868 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A
    Ahmed Abuharthieh
    last edited by Aug 3, 2023, 8:14 PM

    I have nppexec set up with this command

    npp_save
    cd $(FULL_CURRENT_PATH)
    python -u $(FILE_NAME)
    

    It runs most python code perfectly fine, I even tested out some of the example code that comes with python. Files using the turtle module work perfectly with opening windows etc.

    However, when I run code using the Pygame module, everything works fine except that no window is opened. I see that the code is executing and I can print statements in the game loop and they show up in the console, but that is all.

    The program itself works perfectly fine when run from the idle or in vscode, I am not sure what’s causing this issue.

    For anyone who wants to test this, here is some simple code for pygame that initializes a window with a moving square.

    import sys
    
    import pygame
    
    
    SPEED = 4
    
    
    class Game:
        def __init__(self):
            pygame.init()
    
            pygame.display.set_caption('GAME')
            self.screen = pygame.display.set_mode((640,480))
    
            self.clock = pygame.time.Clock()
    
            self.pos = [10, 10]
            self.x_vel = SPEED
            self.y_vel = SPEED
    
        def run(self):
            while True:
                self.screen.fill((30,30,36))
                pygame.draw.rect(
                    self.screen,
                    (140, 230, 235),
                    pygame.Rect(*self.pos, 50, 50)
                )
                
                if self.pos[0] >= 640 - 50:
                    self.x_vel = -SPEED
                elif self.pos[0] <= 0:
                    self.x_vel = SPEED
    
                if self.pos[1] >= 480 - 50:
                    self.y_vel = -SPEED
                elif self.pos[1] <= 0:
                    self.y_vel = SPEED
                    
                self.pos[0] += self.x_vel
                self.pos[1] += self.y_vel
                
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()
    
                pygame.display.update()
                self.clock.tick(60)
    
    Game().run()
    

    If I try to run the code using F5 and put this into the “Program to run category”

    C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\python.exe "$(FULL_CURRENT_PATH)"
    

    The code runs fine, window opens and everything.

    The issue is that I want have the NPPEXEC terminal for debugging purposes rather than have it act as if I just ran the code from file explorer or desktop.

    P 1 Reply Last reply Aug 3, 2023, 8:29 PM Reply Quote 0
    • P
      PeterJones @Ahmed Abuharthieh
      last edited by Aug 3, 2023, 8:29 PM

      @Ahmed-Abuharthieh said in Notepad++ NPPEXEC Unable to Properly Run Pygame Code:

      I have nppexec set up with this command

      npp_save
      cd $(FULL_CURRENT_PATH)
      python -u $(FILE_NAME)
      

      …
      If I try to run the code using F5 and put this into the “Program to run category”

      C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\python.exe "$(FULL_CURRENT_PATH)"
      

      Your two commands are different in at least three ways that I can see.

      1. The npp_exec uses -u and the Run command does not. I don’t know why buffering or not would mess pygame up, but I don’t know enough about pygame to comment on that specifically
      2. The npp_exec does not put quotes around the filename (though if your filename had a space in it, I would think it wouldn’t run at all)
      3. You use the full path to python.exe in Run but not in npp_exec, so it may be that the python in the PATH that npp_exec can see is in a different directory than the one you run from the Run menu.
      A 1 Reply Last reply Aug 4, 2023, 2:05 AM Reply Quote 1
      • A
        Ahmed Abuharthieh @PeterJones
        last edited by Ahmed Abuharthieh Aug 4, 2023, 2:06 AM Aug 4, 2023, 2:05 AM

        @PeterJones said in Notepad++ NPPEXEC Unable to Properly Run Pygame Code:

        My current Script

        npp_save
        cd “$(FULL_CURRENT_PATH)”
        python “$(FILE_NAME)”

        Thank you for your response

        • I made adjustments to these lines so that there are quotes around the file paths
        • Tried with and without the -u
        • Tried putting in the full path to python

        I get the same result, except when I don’t use -u, I get a large delay before it starts printing to terminal. But still no window opening.

        1 Reply Last reply Reply Quote 0
        • A
          Ahmed Abuharthieh
          last edited by Aug 4, 2023, 2:56 AM

          Okay, I was able to get it to work. The issue was that NPPEXEC was somehow blocking window creation in the case of Pygame.

          • Using cmd/c python fixed the issue with the window
          • using python -u makes it so that the console does not wait for the program to finish to start printing the output

          here is the final command I used to get everything working well

          npp_save
          cd "$(FULL_CURRENT_PATH)"
          cmd /c python -u "$(FILE_NAME)"
          

          Here is a link to where I found the solution to these problems

          https://superuser.com/questions/381942/stop-nppexec-from-trapping-console-output-until-program-finishes

          V 1 Reply Last reply Aug 4, 2023, 9:01 AM Reply Quote 2
          • V
            Vitalii Dovgan @Ahmed Abuharthieh
            last edited by Aug 4, 2023, 9:01 AM

            Thank you for this information! Added to the NppExec Manual (the point “4. Running Pygame Code”) :
            https://htmlpreview.github.io/?https://github.com/d0vgan/nppexec/blob/develop/NppExec/doc/NppExec/NppExec_Manual/4.6.4.html

            A 1 Reply Last reply Aug 4, 2023, 4:00 PM Reply Quote 3
            • A
              Ahmed Abuharthieh @Vitalii Dovgan
              last edited by Aug 4, 2023, 4:00 PM

              @Vitalii-Dovgan
              Cool, I just checked out the link. Looks good!

              1 Reply Last reply Reply Quote 0
              5 out of 6
              • First post
                5/6
                Last post
              The Community of users of the Notepad++ text editor.
              Powered by NodeBB | Contributors