FAQ: Python in Pokitto

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 :slight_smile: . 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.

3 Likes

I noticed none of the examples show how to print text to the screen. Might be good to add that to the FAQ.

Added to FAQ

1 Like

Could you elaborate more the use of:

random.getrandbits(8)

to generate random values. Could be unclear for beginners. Other method seems unsupported right? Probably because float is not available?

https://docs.python.org/2/library/random.html#random.getrandbits

random.getrandbits(k)
Returns a python long int with k random bits. This method is supplied with the MersenneTwister generator and some other generators may also provide it as an optional part of the API. When available, getrandbits() enables randrange() to handle arbitrarily large ranges.

I wonder if the Pokitto version also uses a mersenne twister?
Mersenne twisters are excellent PRNGs, but they’re also relatively heavy.


Side note: one of the things I most dislike about Python is that the libraries all use drastically different naming schemes, sometimes even within the same module.

Micro Python seems to use this:
http://www.literatecode.com/yasmarang

Would I be correct in thinking a working way to get a random number between 0 and 15 would be…

random.getrandbits(k)%15

Never heard of it.
The fact it has multiple parts to the internal state is promising.
I hope the actual thing isn’t using static function variables though.
static function variables aren’t assigned until the first execution of the function.

You could probably just use random.getrandbits(4).
4 bits will give you a value between 0 and 15 (inclusive) automatically.

cool.

Anyone know if there are any drawing functions other than sprites? like rects, lines etc?

I believe the only primitives supported are filled rectangle.
(I feel like I’m copying at my exams right now… )

2 Likes

Is it possible to detect whether a button is held down?

The button is held if there is “down” event , but not yet “up” event.

1 Like

Can we access the header pins from micropython? I’d like to use my own button routines (https://os.mbed.com/users/spinal/code/HelloWorld_buttontest/) if possible. I’m having a couple of button issues and I’m curious if this would solve them.

Sure, in the slide set there is an example, how to read a “line following” sensor. That is how you can read any digital pin.

Of course, works only on HW.

@Hanski could you please add this to the FAQ at begin of the thread?
Can you confirm other random methods (like randrange(star,stop) , randint(start,stop)) are not supported?

Also it seems like the seed is never updated. At least in the sim this code give me always the same result:

import urandom as random
print(random.getrandbits(8))
1 Like

hmmm, it looks like only pins brought out to the PEX header are usable, is this right? I need some of the internal pins…

_aPin = Pin(Pin.P1_9, Pin.IN)
_bPin = Pin(Pin.P1_4, Pin.IN)
_cPin = Pin(Pin.P1_10, Pin.IN)
_upPin = Pin(Pin.P1_13, Pin.IN)
_downPin = Pin(Pin.P1_3, Pin.IN)
_leftPin = Pin(Pin.P1_25, Pin.IN)
_rightPin = Pin(Pin.P1_7, Pin.IN)

Added to the FAQ. The getrandbits() is the only function for getting random numbers.

1 Like

I could probably add these.

I’d probably be the only one who uses them. If it’s more than a couple of seconds work don’t bother.

1 Like

How do you draw a rectangle on the screen? In fact where can you see which functions are available?