I use this folder design:
- main.py
- func
- window.py
Then I import the file with the window code in the main.py file
import pygame as pg from func.window import * Window((400, 400))
In the func folder I will put the window.py module with this script
import pygame as pg class Window: def __init__(self, windowsize): self.windowsize = windowsize self.start() def start(self): screen = pg.display.set_mode(self.windowsize) timer = pg.time.Clock() def quit(e): "Check if you press x button or escape key" quit = e.type == pg.QUIT escape = e.type == pg.KEYDOWN and e.key == pg.K_ESCAPE game = 0 if quit or escape else 1 return game game = 1 while game: for e in pg.event.get(): game = quit(e) screen.fill((0, 0, 0)) pg.display.update() timer.tick(60) pg.quit()
Notice I made a function to handle the quit button and the escape key to close the window. In a future post I will post other code to make a modular approach to a pygame game like a platformer or some other 2d game.
Twitter: @pythonprogrammi - python_pygame