Create your own game
eng   рус

Hello World in Python SDL2

Previous tutorial: SDL2 in Python Programming Language
Next tutorial: SDL2 loading images (bmp, jpg, png)
Python SDL2 Hello World

In this tutorial we'll examine window Hello World program in Python programming language using SDL 2 package.

SDL - is a Simple DirectMedia Layer software library that allows to create video games. There are many other software for creating video games. For example PyGame (it's in Python too and it uses SDL under the cover).

SDL is written on C/C++ programming language and it's cross-platform - it means that program written for Windows OS will work on OSX or Ubuntu. Moreover SDL will work on iOS and Android. We'll use SDL2 in this series of tutorials

To work with SDL in Python language we need to use pysdl2 module. In this tutorial we'll create a so-called Hello World window application using SDL2 and in addition we'll color pixels in the window with random colors.

Python programming language with SDL 2 - Hello World

First let's create just a window with header "Hello World". Here is the code:

import sdl2.ext sdl2.ext.init() window = sdl2.ext.Window("Hello World!", size=(1600, 1000)) processor = sdl2.ext.TestEventProcessor()

Firstly, we import sdl2 module. For now we'll use ext namespace that contains additional features (and make things easier).

Before use any of sdl2 functions we need to call init() - it initializes library to work with operating system.

On next line we create window object with the sdl2.ext method Window(). First argument - title of window - it will be visible in the title bar. size argument defines dimensions of window as tuple: width and height. If you have small monitor you need to change these values to smaller ones. statement renders window on screen.

The last two lines are needed to make the window stay on screen. If we try to run the program without them (without the last one more precisely) the window will appear for a moment and will be closed immediately. We'll discuss event processors in following tutorials.

If you run the program now, you'll see standard window for your operating system. You can drag it, close it, minimize. We get this features without any effort. There is some issue with the content of the window. You'll see that window contains everything what was beneath the window before it was started. It's happening because we didn't make any drawing.

SDL2 window with noise

In next example we'll fill the window with colored noise: just pixels with random color. Let's see the code:

import sdl2.ext from random import randint sdl2.ext.init() width = 1600 height = 1000 window = sdl2.ext.Window("Hello World!", size=(width, height)) renderer = sdl2.ext.Renderer(window) for y in range(height): for x in range(width): r = randint(0,255) g = randint(0,255) b = randint(0,255) renderer.draw_point([x,y], sdl2.ext.Color(r,g,b)) renderer.present() processor = sdl2.ext.TestEventProcessor()

Here we additionally imported randint from random module to generate integer random numbers for colors. Also I've put width and height of the window in separate variables as now dimensions of window are referenced in two different places and variables allow to change the size of screen in one place. Now let's examine new code:

renderer = sdl2.ext.Renderer(window) for y in range(height): for x in range(width): r = randint(0,255) g = randint(0,255) b = randint(0,255) renderer.draw_point([x,y], sdl2.ext.Color(r,g,b)) renderer.present()

We create object renderer. By passing window variable to the constructor we tell the SDL that we want to draw on that window. In next tutorials we'll learn how to work with surfaces in sdl2 and you'll understand what happens here internally.

Then we iterate all pixels of window line by line. With 1600*1000 pixels it took too much time so to make a screenshot I've use the window with size 400*400 pixels.

r = randint(0,255) g = randint(0,255) b = randint(0,255)

In these lines we generate random values for each color channel (red, green blue) of pixel we render at this moment. Each channel can have value from zero to 255.

renderer.draw_point([x,y], sdl2.ext.Color(r,g,b))

This line draws the pixel. First argument is the list with coordinates of points. In our case we draw only one point at a time. Second argument is color of the point. Though, at this moment the dot is not visible on the screen.


present method of sdl2.ext.Renderer object is used to show the point on the user screen.

When you run this program, initially you'll see the window with content that was beneath the window. And the program will start to fill the pixels from the top-left corner to bottom-right corner with random colors line by line


In this tutorial we encountered the first windowed program. Different operating system have theirs own procedures to create windows, but for us it's all hidden under SDL2 method. That allows us to start work with graphics right away instead of learning different aspects of internals of operating system. And Python programming language due it's simplicity gives us quick start compared to C/C++ or Java.


May 18, 2021, 12:37 a.m.
4 Guest
change to width = 400 height = 25 and unindent renderer.present() at least once. That is, run renderer.present() inside first loop and outside second loop
May 18, 2021, 12:38 a.m.
5 Guest
I mean height = 250
Nov. 12, 2020, 6:43 a.m.
1 Guest
Thanks for the Hello World tutorial! Got it to work...but god-awful slow on 20.40 Xubuntu? Regardless...thanks for the help! nivofhir
Dec. 1, 2020, 11:25 p.m.
2 Guest
Unindent that renderer.present() so it's not called each time around the loop and it'll run faster. But still very slow when the image is large. For 100x100 it's okay. Not sure how to make it faster - is it just a slow job to set those pixels?
Dec. 2, 2020, 8:51 a.m.
3 Guest
100x100 it's already 10000 operations. And Python is not good in speed. So, the only way to make it faster is to set all pixels in texture in the beginning and then use renderer.copy
May 18, 2021, 3:28 p.m.
6 Guest
check this out def mathematic(width,height): for p in range([width,height])): x = p % width float_y = (p-x) / width y = int(float_y) return "{0} + {1} = {2}".format(x,y,p) def iterative(width,height): for y in range(height): for x in range(width): p = x + (y * width) return "{0} + {1} = {2}".format(x,y,p) width = 800 height = 400 mathematic(width,height)==iterative(width,height)
May 18, 2021, 3:29 p.m.
7 Guest
this returns True meaning these two algorithm are interchangable
May 18, 2021, 3:33 p.m.
8 Guest
I am using this instead <code> for p in range([width,height])): x = p % width y = int((p-x) / width) r = randint(0,255) g = randint(0,255) b = randint(0,255) renderer.draw_point([x,y], sdl2.ext.Color(r,g,b))</code>