FAQ: Python in Pokitto
This is a FAQ about all things related to Python programming in Pokitto, like the online Pokitto Python Editor, Micro Python, etc.
The online Python Editor
Q: What kind of documentation there are?
A: Not yet much in addition to this FAQ, but the community here is ready help in any issues . Look at the video for a quick overview of the features of the editor: https://youtu.be/TsxH29Vb3tg. The Pokitto Magazine issue 1 contains a tutorial for making a simple game with the Online Python Editor.
Q: How do I restore a saved project or files back to the editor?
A: You can drag-and-drop the project zip file or a bunch of source files to the editor window. They are automatically added (or replaced) to the current project.
Q: Why is inline pixel editing is not supported in my image data?
A: The pixel editing requires that the variable name has the substring “pixels” somewhere. E.g.
laserPixels_deg90 = b'\
\x0f\x00\
\xf0\xf0\
\x0f\x00\
\x0f\x00\
'
Q: How to I stop the game?
A: Press the Esc
key on your keyboard.
Q: How to add sound effect data to the code?
A: Drag and drop an audio file to the code editor. All formats supported by your browser can be used (e.g. wav, ogg, mp3 ). Note that the size of the audio file is limited by the size of the Pokitto ROM for programs (about 200 kb)
Q: How to enable the TAS (“Tiles And Sprites”) mode?
A: In the TAS mode you can have more memory for your program, up to 4 KB more, but it imposes some limits for drawing (See uPyGame Reference - The Pokitto Guidebook for more info). TAS can be enabled in the bottom right view, in the Settings tab. There are four TAS related settings:
- Resolution: TAS High (220x176, 16 colors), TAS Low (110x88, 16 colors)
- TAS Tile width: A single tile width in pixels in the tilemap.
- TAS Tile height: A single tile height in pixels in the tilemap.
- TAS Max sprites: The maximum number of sprites (bitmaps) and characters per screen at a time.
There is also a ready made example under the Examples tab in the top bar: “Tilemap TAS”
Micro Python
Q: Is there any way to measure time?
A: There is: umachine.time_ms().
Q: Are floating point numbers supported?
A: No. Use integers instead, or get more precision by multiplying and dividing the values by e.g. 1000 (fixed point like).
Q: How do I print the amount of free RAM left for a Python program ?
A: Like this:
import gc
print("Hello world!")
# Collect all freed memory.
gc.collect()
# Print free memory amount
print ("free:",gc.mem_free())
Note: Do not call gc methods on every frame,as it slows down the execution. Do it just before the main loop etc.
Note: This works only in the integrated emulator, for HW you should use umachine.draw_text(), but the free RAM amount should be the same in both environments.
Q: How do I get random numbers?
A: Call urandom.getrandbits(4)
to get a value between 0 and 15 (inclusive).
Q: What is the version of MicroPython?
A: MicroPython is v1.9.2 (implements the subset of Python 3)
uPyGame
Q: Where is this module documented?
A: Here: [Wiki] Reference: Pokitto gaming API for Python (uPyGame and umachine)
Q: Does Micro Python support other than Mode 2 (110x88 pixels with 16 colors) with PyGame?
A: For simplicity, the online Python Editor supports only Mode 2. If Python is developed with C++ development environment (e.g. EmBitz or CodeBlocks) other modes can be used in PyGame. Note: there is a partial support for Hi-Res (220x176, 4 -color) mode but e.g. the inline gfx editor or Tilemap class do not support it.
Q: How do I print text on screen?
A: use umachine.draw_text( x, y, text, color_index ), e.g.
import umachine
umachine.draw_text(0,10,"HELLO WORLD!",1)
pygame.display.flip()
pygame.display.flip() # second flip() is needed only the first frame
Q: How do I get the FPS?
A: Like this:
import umachine
...
frameNum = 1
fps = 0
lastTimeFps = umachine.time_ms()
# The main game loop
while True:
...
# Fps
if( frameNum % 50 == 0 ):
now = umachine.time_ms()
fps = 50000 // (now-lastTimeFps)
lastTimeFps = now
umachine.draw_text(0, 0, str(fps), 2);
frameNum += 1
...
Note: There is also a build in FPS counter int the tabs window.
Q: How to play sound effects?
A: Add these two lines to your code:
gSound = Sound() # Call this on program initialization
..
gSound.play_sfx( coinSoundData, True) # Call this always when needed.
Note: The Pokitto emulator cannot play audio. You must have a real Pokitto HW to be able to hear the audio.