Setting up a blank screen

In this tutorial we will be creating the most simple project possible that just opens a blank window

Creating your first PyGame Python file

Creating a Python File in PyCharm is very simple. First right-click on the root folder of the project. Then, hover over "New" and then click on "Python File." This will prompt you for the name of the file. For now, simply use the name "window" and press enter.

Your root folder is the one that you named your project

Writing Python Code

First, in the code, you're going to import all the libraries used in the script. You can do this with a simple import statement. The following code block put all the pygame functions at your disposal.

import pygame

Then, we can create the main function:

def main():

This is not actually where the program enters when you run the script, calling it the main method is for semantics

To tell the function where to start use the following piece of code:

if name== "main": main()

Then, within the main function, initialize the pygame module with the following command. This will tell pygame that a game is being made and set up any properties before anything else.

pygame.init()

We now will create a window with the following command. The two arguments are the x and y scale for the canvas it will create

gameDisplay = pygame.display.set_mode((800,600))

For when you exit the game, you need a piece of code that will tell the window to close

#This is the code that exitrs the game if the x in the corner is hit
    done = False
    while not done:
            for event in pygame.event.get():
                    #pygame.QUIT is an event defined by the pygame lib
                    if event.type == pygame.QUIT:
                            done = True
                            #system function
                            exit()
            #dont worry about this, it just buffers the screen
            pygame.display.flip()

Up to now, we've written the following code. To run it, go to run in the top bar and press Run Module.

#Library that the project is based on
import pygame

#This is the main function which is where I'll write the majority of the code
def main():
    #this code initializes the code within the library
    pygame.init()
    #this bit of code sets the black screen display
    gameDisplay = pygame.display.set_mode((800,600))
    #This is the code that exitrs the game if the x in the corner is hit
    done = False
    while not done:
            for event in pygame.event.get():
                    #pygame.QUIT is an event defined by the pygame lib
                    if event.type == pygame.QUIT:
                            done = True
                            #system function
                            exit()
            #dont worry about this, it just buffers the screen
            pygame.display.flip()

#this is where the code enters and decides where to go
if __name__== "__main__":
  #the function that you want to start the game on
  main()

You should see a blank window appear on the screen that looks like this ->

Last updated

Was this helpful?