Getting Started with 2D Games Using Pyxel (Part 2): Creating the Game Screen

Published: (January 5, 2026 at 07:21 PM EST)
2 min read
Source: Dev.to

Source: Dev.to

Creating the Game Screen

In this chapter we create a basic game screen using Pyxel.

First, create a working folder and add a file named main.py.
Your folder structure should look like this:

working_folder/
 └ main.py  # File where we write the program

All code will be written inside main.py. Begin by importing the Pyxel engine and any required modules.

import pyxel   # Pyxel game engine
import math    # Math module
import random  # Random number module

W, H = 160, 120  # Game screen width and height

Game class

# Game
class Game:
    def __init__(self):
        """Constructor"""
        # Start Pyxel
        pyxel.init(W, H, title="Hello, Pyxel!!")
        pyxel.run(self.update, self.draw)

    def update(self):
        """Update logic"""
        pass

    def draw(self):
        """Drawing logic"""
        pyxel.cls(0)

The constructor initializes the game screen by calling pyxel.init() with the screen width, height, and window title, then starts the main loop with pyxel.run(self.update, self.draw).

  • self.update() will handle character movement and collision detection.
  • self.draw() will handle rendering characters and graphics.

Background Color

At the beginning of the draw() method we clear the screen with a single color.
Passing a number from 0 to 15 to pyxel.cls() fills the entire screen with that color.

def draw(self):
    """Drawing logic"""
    pyxel.cls(0)   # Fill screen with color 0

Refer to the Pyxel color palette for the corresponding color numbers.

Complete Code

import pyxel   # Pyxel game engine
import math    # Math module
import random  # Random number module

W, H = 160, 120  # Game screen width and height

# Game
class Game:
    def __init__(self):
        """Constructor"""
        # Start Pyxel
        pyxel.init(W, H, title="Hello, Pyxel!!")
        pyxel.run(self.update, self.draw)

    def update(self):
        """Update logic"""
        pass

    def draw(self):
        """Drawing logic"""
        pyxel.cls(0)

def main():
    """Main entry point"""
    Game()

if __name__ == "__main__":
    main()

Running this program displays a blank screen cleared with color 0.

Thank you for reading this chapter. See you in the next part!

Back to Blog

Related posts

Read more »