Initializing the pygame window
Let’s start by initializing the game window so that we can start with the game dev.
Initialize the pygame moduleWe can start the pygame module just by calling the init function.
Open the game window
main.pyfile that we created in the last part.
import pygame pygame.init() SCREEN_SIZE: tuple = 800, 600 # width, height screen = pygame.display.set_mode(SCREEN_SIZE) is_window_open = True while is_window_open: for event in pygame.event.get(): if event.type == pygame.QUIT: is_window_open = False
import pygame, we are importing all the modules of pygame package.
pygame.init()is used to initialize all those models. Then we are defining a tuple that we need to initialize the window. First up is the
SCREEN_SIZEwhich defines the width and height of the screen window. We set those values using
fillmethods. You should be able to see the window even with the following code for just a few seconds. But we are going to add a few more lines of code to run the window indefinitely.
is_window_openis used to run that indefinite loop. Inside the loop, we wait for the
QUITevent of which occurs when you hit the close button on the created window.
Run the codeAfter running these few lines of code, we will be able to see the black
pygamewindow and it will stay open till you hit that red close button.
Please share your Feedback:
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!