FAQ: Python in Pokitto


#1

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

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.

Micro Python

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?
A: Call urandom.getrandbits(4) to get a value between 0 and 15 (inclusive).

uPyGame

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


[Release & Competition] Online Python Editor for Pokitto!
[Release & Competition] Online Python Editor for Pokitto!
#2

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


#3

Added to FAQ


#4

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?


#5

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.


#6

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


#7

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

random.getrandbits(k)%15


#8

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.


#9

cool.

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


#10

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


#11

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


#12

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


#13

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.


#14

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.


#15

@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))

#16

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)

#17

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


#18

I could probably add these.


#19

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


#20

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