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
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: Is there any way to measure time?
A: Not currently
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?
urandom.getrandbits(4) to get a value between 0 and 15 (inclusive).
Q: Where is this module documented?
A: Here: [Wiki] Pokitto gaming API for Python reference (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.
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_us() # The main game loop while True: ... # Fps if( frameNum % 50 == 0 ): now = umachine.time_us() fps = 50000 // (now-lastTimeFps) lastTimeFps = now umachine.draw_text(0, 0, str(fps), 2); frameNum += 1 ...