Let's doodle with Python (Introduction of new commands of Pyxel)

This article contains outdated information as of 2019. For the latest information, please refer to here.


This article is the English version of the previous post.

Have you ever wanted to create a little drawing or animation in Python?

In the Pyxel, a retro game engine for Python, new commands to draw easily has been added since version 1.2.

By using the new commands, you can create the following image with about 10 lines of Python code.

In this article, I would like to introduce how to use new drawing commands and examples of creative coding using them.

About Pyxel 1.2

Pyxel is a game engine for easily creating pixel-art-type retro games in Python.

Features of Pyxel and its detailed usage instructions are described in the manual, but if you would like to use it quickly, enter the following command.

[Windows]

In the environment where Python is installed:

pip install pyxel

[Mac]

In the environment where Homebrew is installed:

brew install python3 sdl2 sdl2_image
pip3 install pyxel

Pyxel has been upgraded since the first official release on July 30, 2018. In the version 1.2 released last month (August 2019), show and flip, the commands enable to draw graphics quickly in Python, and the Pyxel Packager tool, makes stand-alone program which can be executed without Python, are added.

About show and flip commands

The show and flip commands were added for the purpose of making it easy to create animations and interactive art works (creative coding) with Python + Pyxel, and to post them on Twitter.

show is a command that waits until the ESC key is pressed after drawing the screen, and is used to create a still image.

For example, if you'd like to create a bar chart image,

import pyxel

data = [40, 70, 50, 20, 100, 50, 40, 20]  # data for a bar chart

pyxel.init(128, 128)  # create the screen as 128x128 size

for i, d in enumerate(data):
    # draw a rectangle with different height while changing color
    pyxel.rect(i * 14 + 10, 120 - d, 10, d, 8 + i) 

pyxel.line(0, 120, 127, 120, 7)  # draw a line below the bar chart

pyxel.show()  # show the screen

by writing the above Python code, the following screen will be displayed:

flip is a command to continue processing after drawing the screen once and is used to create animation.

For example, if the animation is a ball moving around the screen,

import pyxel

x = y = 30  # the position of the ball
v = w = 3  # the speed of the ball

pyxel.init(160, 120)  # create the screen as 160x120 size

while True:
    pyxel.cls(1)  # erase the screen with color number 1 (blue)

    # process the movement of the ball
    x += v
    y += w

    if x <= 7 or x >= 152:
        x = min(max(x, 7), 152)
        v = -v

    if y <= 7 or y >= 112:
        y = min(max(y, 7), 112)
        w = -w

    pyxel.circ(x, y, 7, pyxel.frame_count % 16)  # draw the ball with different colors

    pyxel.flip()  # draw the screen

by writing the above Python code, you can create the following animation:

How to save images and animations

Created images and animations can be saved as image files using Pyxel's screenshot saving function.

If it is a still image (screenshot), press ALT + 1 (or OPTION + 1 for Mac) to save the PNG file on the desktop.

If it's a movie, ALT + 3 (OPTION + 3 for Mac) saves the latest animation of up to 30 seconds on the desktop as a GIF file.

If you want to change the recording start timing, you can also specify it with ALT + 2.

If you want to distribute an interactive program with key operations, enter this command:

pyxelpackager target-Python-file-name

Then, a file that can be executed by itself without installing Python or Pyxel will be created under the dist folder.

Try short coding

You can post your code directly to Twitter by shortening the Python code and clearing the Twitter character limit (280 letters).

For example, with a short alias like this:

import pyxel as p

The following tweet, the shorten bar graph code, can be made:

I'm not very familiar with it, but I think it's possible to expand the possibilities by applying various Python one-liner techniques.

Try creative coding with Pyxel

Based on the contents so far, I will challenge creative coding with a length that can be tweeted on Twitter.

I made a Python code like this:

from math import sin, sqrt
from pyxel import circ, cls, flip, init

a=0
init(128,128)
while 1:
 cls(1)
 for x in range(0,128,4):
  for y in range(0,128,4):
   d=sqrt((x-64)**2+(y-64)**2)
   b=sin(d*0.2+a)*4
   c=(15-d*0.2)%16
   circ(x+b,y+sin(b/4)*4,1,c)
 a+=0.2
 flip()

I noticed that the Python import sentence is long. So I secretly omitted the description of the import statement.

Here are the results for Twitter:

Why don't you try creative coding and short coding with Pyxel?