-2

import pygame pygame.init() ``prozor = pygame.display.set_mode((200, 200)) ``prozor.fill(pygame.Color("white")) ``pygame.draw.rect(prozor, pygame.Color("black"), (20, 20, 160, 160), 5) ``pygame.display.update() ``pygame.time.wait(3000) ``pygame.quit() I want to make it so that the window never disappears. What can I do to make it happen?

Thank you

I tried removing the pygame.time.wait(3000) line as a first, but that didn't do anything. Then I tried to change the value to 0. Same thing. By "not do anything" I mean it made the window not appear at all, in both cases.

3 Answers3

0

There is no while-loop, and your program will just be created and instantly quit

I suggest watching the tutorials from Clear Code on YT. They are pretty good for python!

I made my own code, if you'd like to use it:

import pygame as pg
import sys

renderR = renderW, renderH = 640, 360


class Main:
    ### the program gets initiated here, add your initialisation code in this function ###
    def __init__(self) -> None:
        ### Some presets for the window resolution ###
        # self.display = pg.display.set_mode(renderR, pg.SCALED | pg.FULLSCREEN)
        self.display = pg.display.set_mode(renderR, pg.SCALED)
        # self.display = pg.display.set_mode(renderR)
        
        self.clock = pg.time.Clock()

    ### Tick your game (maybe physics, player controlls... ) ###
    def tick(self) -> None:
        ...

    ### Draw your graphics here (only a suggestion, you can also draw in "tick") ###
    def draw(self) -> None:
        ...

    ### Event loop, currently only quitting the window
    def eventLoop(self):
        for i in pg.event.get():
            if i.type == pg.QUIT:
                pg.quit()
                sys.exit()

    ### Runs your program at the framerate in the line "dt = self.clock.tick( here )", leave it blank for no time wait ###
    def run(self) -> None:
        while True:
            dt = self.clock.tick()
            self.eventLoop()

            pg.display.update()
            pg.display.set_caption(f"Framerate: {self.clock.get_fps():.5f} Time: {dt}ms")


### __name__ == "__main__" is for only being executed when it is this file, and not just an import ###
if __name__ == "__main__":
    while True:
        app = Main()
        app.run()

LuigiBro
  • 1
  • 3
  • Since app.run contains a `while True:`, you shouldn't need to have the `while True:` in the `if __name__ == "__main__":` block – Malcolm Aug 12 '23 at 20:54
  • It is there, to easily restart the game, by just stopping the while loop inside the object – LuigiBro Aug 14 '23 at 15:38
0

here is a simple code I made that closes when the user turns it off:

import pygame as pg
import pygamebg
(w, h) = (400, 400)
prozor = pygamebg.open_window(w, h, "Pygame")
prozor.fill(pg.Color("white"))
pg.draw.line(prozor, pg.Color("black"), (100, 100), (300, 300), 5)

# This line prevents the window from closing by itself
pygamebg.wait_loop() 

The code also draws a simple line.

Sorry if the code is not clear, this is the first time I tried to answer a question on this site, you can ask me to explain it more in detail if needed.

LazaPro
  • 1
  • 2
-1

While you will definitely want an event loop driven solution for your game as @LuigiBro provided, there are some other ways to be able to run your code without it exiting immediately, that you should be aware of.

The first and easiest is to run python interactively at a command prompt, or equivalently in a python console if your IDE provides one. There you can just type the commands one by one and see what happens with everything waiting for you to run the next command. For example, here is a screen capture from my terminal.

Both of these are useful even if you aren't using pygame.

$ python3
>>> import pygame
pygame 2.1.2 (SDL 2.0.20, Python 3.10.12)
Hello from the pygame community. https://www.pygame.org/contribute.html
>>> pygame.init()
(5, 0)
>>> prozor = pygame.display.set_mode((200, 200))
>>> 

At this point I have a 200x200 pygame window open on my desktop awaiting my next python command.

If you really want or need to be running from a script (and you haven't started code that captures your keyboard input), you can add

x = input()

to the end of your script. This will block until you hit the Enter key.

Both of these will be useful even if you aren't using pygame.

Malcolm
  • 461
  • 2
  • 10
  • x = input() Sweet. I am kind of ashamed I didn't learn this yet. It seems pretty elementary – pipppup Aug 13 '23 at 00:47
  • Do not use `input` in pygame. See [Why is my pygame display not responding while waiting for input?](https://stackoverflow.com/questions/67111782/why-is-my-pygame-display-not-responding-while-waiting-for-input/67113040#67113040) and [How to wait some time in pygame?](https://stackoverflow.com/questions/18839039/how-to-wait-some-time-in-pygame) – Rabbid76 Aug 13 '23 at 04:48
  • @Rabbid76, that was why I included the parenthetic comment. Perhaps I should have been more clear that a working pygame program is definitely capturing your keyboard input. – Malcolm Aug 13 '23 at 17:15