This article is the English version of the previous post.
Have you ever wanted to create a little drawing or animation in Python?
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
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.
In the environment where Python is installed:
pip install pyxel
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),
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.
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's a movie,
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
If you want to distribute an interactive program with key operations, enter this command:
Then, a file that can be executed by itself without installing Python or Pyxel will be created under the
Try short coding
For example, with a short alias like this:
import pyxel as p
The following tweet, the shorten bar graph code, can be made:
Why don't you draw a graph with Pyxel?— Takashi Kitao (@kitao) July 24, 2019
import pyxel as p
data = [40,70,50,20,100,50,40,20]
for i,d in enumerate(data):
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:
a=0— Takashi Kitao (@kitao) September 2, 2019
for x in range(0,128,4):
for y in range(0,128,4):
flip()#python #pyxel #shortcoding pic.twitter.com/VsbXJmGaJi
Why don't you try creative coding and short coding with Pyxel?