DEV Community

Kajiru
Kajiru

Posted on

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

Creating the Game Screen

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

The complete code is shown at the end of this article.

1. Prepare a Working Folder

First, create a working folder and add a file named main.py.

For details on how to create and save a file, please refer to

Saving a Program to a File.

Your folder structure should look like this:

# Folder structure
working_folder/
 └ main.py  # File where we write the program
Enter fullscreen mode Exit fullscreen mode

From here on, we will write all code inside the main.py file.

2. The Basic Pyxel Pattern

In main.py, we first import the Pyxel engine and any required modules.
All game logic will be implemented inside the Game class.

# main.py (excerpt)

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 """
        pass

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

    def draw(self):
        """ Drawing logic """
        pass

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

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

3. Starting Pyxel

Inside the constructor of the Game class, we initialize the game screen.

Specifically, we call pyxel.init() with the following values:

  • Game screen width
  • Game screen height
  • Game window title

Next, we pass two methods to pyxel.run():

  • The update method
  • The draw method
# main.py (excerpt)

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

From now on:

  • Character movement and collision detection will be written in self.update()
  • Rendering characters and graphics will be written in self.draw()

4. Background Color

At the beginning of the self.draw() method, we fill the screen with a single color.

By passing a number from 0 to 15 to pyxel.cls(),

the entire screen will be cleared (filled) with that color.

# main.py (excerpt)

def draw(self):
    """ Drawing logic """
    pyxel.cls(0)
Enter fullscreen mode Exit fullscreen mode

Please refer to the following image for the color palette numbers:

Complete Code

Below is the complete code with all features implemented so far.

# main.py

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()
Enter fullscreen mode Exit fullscreen mode

When you run this program, the result will look like this:

Coming Up Next...

Thank you for reading this chapter.
The next title will be “Preparing Resource Files.”

See you in the next part!!

Top comments (0)