[Python] Make a game with Pyxel-Use an editor-

Previous article [Python] The first step to making a game with Pyxel

Introduction

Pyxel has an editor (Pyxel Editor) that allows you to draw pixel art. Here, we will introduce how to use the editor.

Launch the editor

Execute the following command.

pyxeleditor [Pyxel resource file]

The resource file is a file of the format .pyxres. If omitted, a new one will be created.

draw a picture

The editor looks like this. The frame on the right is the entire area (image bank) where you can draw a picture. Three image banks are available. The numbers are 0 to 2 and can be switched in the lower right frame. The frame on the left is an enlarged view of the selected 16x16 area of ​​the entire image bank. In the lower left frame, you can select the color to draw. Therefore, first select the range to draw the picture in the right frame, and actually write in the left frame. For the time being, draw a picture appropriately and save it.

Display the picture

In order to display the picture drawn by the editor on the game screen, it is necessary to load and draw resources. Write the code as follows.

import pyxel

class App:
    def __init__(self):
        pyxel.init(100, 100)

        #Read resource file
        pyxel.load("sample.pyxres")
        
    def run(self):
        pyxel.run(self.update, self.draw)

    def update(self):
        pass

    def draw(self):
        pyxel.cls(0)

        #Image drawing:(x, y, img, u, v, w, h, [colkey])
        # xy:Copy destination coordinates, img:Image bank number
        # uv:Coordinates of copy source, wh:Copy range, colkey:Transparent color
        pyxel.blt(0, 0, 0, 0, 0, 16, 16, 0)

App().run()
There are many arguments for `blt`. First, `x, y` is where to copy the image on the game screen. This time it was `(0,0)`, so it is copied to the upper left. `img` is the number of image banks to use. If you set it to `0`, it is the first one. `u, v` are the coordinates on the image bank. Select where to copy based. `w, h` is the width and height of the copy range. This time, `(u, v, w, h)` is changed to `(0,0,16,16)`, so the size of 16x16 is copied from the upper left. As a result, the white circle drawn in the upper left was drawn. I think it will be easier to understand if you change the value of the argument.
import pyxel

class App:
    def __init__(self):
        pyxel.init(100, 100)

        pyxel.load("sample.pyxres")
        
    def run(self):
        pyxel.run(self.update, self.draw)

    def update(self):
        pass

    def draw(self):
        pyxel.cls(0)

        pyxel.blt(0, 0, 0, 0, 0, 16, 16, 0)
        pyxel.blt(32, 48, 0, 16, 0, 16, 16, 0)
        pyxel.blt(64, 16, 0, 32, 16, 16, 16, 0)

App().run()

Draw the background

The background is created using the tile map editor. The background is created by pasting the image created with the image editor. First, the contents of the image bank are displayed in the lower right frame. The picture you drew earlier is displayed. Here, select the range you want to copy. Right now, 8x8 on the upper left is selected. Next, in the upper right frame, the entire area (tile map) where the background can be drawn is displayed. The overall size is 256 x 256. From here, select the area you want to edit. In the left frame, the selected area 16x16 of the tile map is displayed. An image bank area of ​​8x8 fits in one tile map. Eight tile maps are available, and the numbers are 0-7. You can switch at the "TILE MAP" at the bottom left. In the initial state, it is filled with the picture on the upper left of the image bank, so change it appropriately.

Display the background

import pyxel

class App:
    def __init__(self):
        #Change the screen size a little
        pyxel.init(128, 128)

        pyxel.load("sample.pyxres")
        
    def run(self):
        pyxel.run(self.update, self.draw)

    def update(self):
        pass

    def draw(self):
        pyxel.cls(0)

        #Background drawing:(x, y, tm, u, v, w, h, colkey)
        # xy:Coordinates of copy destination, tm:Tile map number
        # uv:Coordinates of copy source, wh:Copy range, col transparent color
        pyxel.bltm(0, 0, 0, 0, 0, 16, 16, 0)

        pyxel.blt(0, 0, 0, 0, 0, 16, 16, 0)
        pyxel.blt(32, 48, 0, 16, 0, 16, 16, 0)
        pyxel.blt(64, 16, 0, 32, 16, 16, 16, 0)

App().run()

Draw the background using bltm. The arguments are similar to blt, but note that for(u, v, w, h), you specify the coordinates in the tilemap editor. The number of dots is 128 x 128, but the area of ​​the tile map is 16 x 16. I was able to display the background. But it feels a little uncomfortable.

reference

https://github.com/kitao/pyxel/blob/master/README.ja.md Making a Pac-Man-like game with Pyxel Part 1 Making a Sokoban game with Pyxel (Part 1)

Recommended Posts

[Python] Make a game with Pyxel-Use an editor-
Let's make a shiritori game with Python
I want to make a game with Python
[Python] Make a simple maze game with Pyxel
Make a fortune with Python
Let's make a simple game with Python 3 and iPhone
Let's make a GUI with python.
Make a recommender system with python
Let's make a graph with python! !!
[Python] Make a simple maze game with Pyxel-Make enemies appear-
Let's make a voice slowly with Python
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
I made a roguelike game with Python
Let's make a web framework with Python! (2)
Make a squash game
Make a Twitter trend bot with heroku + Python
Make a monitoring device with an infrared sensor
Try to make a "cryptanalysis" cipher with Python
Make a rock-paper-scissors game in one line (python)
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Make a Tetris-style game!
I made a bin picking game with Python
Make one repeating string with a Python regular expression.
Try to make a command standby tool with python
I made a Christmas tree lighting game with Python
[Practice] Make a Watson app with Python! # 2 [Translation function]
[Practice] Make a Watson app with Python! # 1 [Language discrimination]
Make a simple Slackbot with interactive button in python
[Let's play with Python] Make a household account book
How to make a shooting game with toio (Part 1)
[Blender] Complement Blender's Python API with a text editor
Make a breakpoint on the c layer with python
Make a CSV formatting tool with Python Pandas PyInstaller
What is God? Make a simple chatbot with python
[Super easy] Let's make a LINE BOT with Python.
Creating an egg with python
Make Puyo Puyo AI with Python
Make a bookmarklet in Python
Create a directory with python
Let's make a rock-paper-scissors game
Make a fire with kdeplot
[Practice] Make a Watson app with Python! # 3 [Natural language classification]
I made a simple typing game with tkinter in Python
How to convert an array to a dictionary with Python [Application]
Associate Python Enum with a function and make it Callable
I want to make a blog editor with django admin
Create an LCD (16x2) game with Raspberry Pi and Python
Experiment to make a self-catering PDF for Kindle with Python
How to make a Python package (written for an intern)
How to make a simple Flappy Bird game with pygame
A story about an amateur making a breakout with python (kivy) ②
A story about an amateur making a breakout with python (kivy) ①
I made a puzzle game (like) with Tkinter in Python
[Python] The first step to making a game with Pyxel
I tried a stochastic simulation of a bingo game with Python
Turn an array of strings with a for statement (Python3)
Cut out an image with python
Solve ABC163 A ~ C with Python