FAQ: Python in Pokitto

I usually use primitives for drawing UI elements.
Boxes to draw text on come in very handy.
(Fill a rect in a background colour, draw an outline of a foreground colour,
put text in the resulting empty space.)

Also shape drawing is dynamically scalable, bitmaps aren’t (unless you repeat part of the image).

And shapes are good placeholders for when you can’t be bothered to do art.

I second this, although with a rectangle primitive I could manage.

Drawpixel would be most important for me

@Hanski is it possible to not get always the same random numbers?

Now that’s interesting, a random number generator not generating random numbers. Maybe you need a function to reset it?

Yeah I need a way to set the seed based on the time or something

Just use a splash screen to seed your random numbers:


def splashScreen():
    while True:
        random.getrandbits(30)
        eventtype = upygame.event.poll()
        if eventtype != upygame.NOEVENT and eventtype.type == upygame.KEYUP:
            return
        screen.blit( splash.bmp, 0, 0 )       
        upygame.display.flip()

This calls random.getrandbits every frame, until the player presses a button. Since the amount of time until that happens isn’t constant, you get a different sequence of random numbers from then on.

7 Likes

Deviously clever. I love it.

2 Likes

This also has the advantage to open a predictable way to control the RNG, which is really interesting for speedrunning :slight_smile:

1 Like

Exactly how I do it in Noggin. A very good method.

1 Like

@spinal the goal in Noggin is to flip all tiles right? Also is it possible to make so that the game knows when there’s no moves possible and display a game over? My guess is that it would be a bit complicated but I had to ask. I am enjoying the game a lot either way.

That’s the goal alright. As for detecting no further moves, I had though about that, but I really can’t think of a way to do it. I would imagine some sort of flood-fill or path-finding routine might be involved, but I’m not sure how possible it could be.

Well in this case I guess you would have to run a check that check the tile you are on and see if you can move that many times. And if you don’t have any ‘!’ it result in a game over. So yeah basically what you said I guess would work.

Technically you get the same sequence, but at a different point in the sequence.

It won’t matter though, as long as nobody is trying to do encryption with it,and as long as the use of random numbers isn’t deterministic.
(E.g. if it was being used to generate the next piece in tetris then you could theoretically predict the next piece.)

Or just edit the code to use a specific seed, nobody would notice. :P

If I knew the rules I could have a go at figuring out how to do that.
Most likely it would involve a graph, a stack or a tree,
and also RAM might be a limiting factor.

Is there a way to make upython store some data in Flash.
So like a tuple that contains game data not alterable?

Bytes literal (b’’)goes to ROM. There are also a const keyword. More info here: http://docs.micropython.org/en/latest/reference/constrained.html

1 Like

Is there a way to build a surface defining also x and y?

now upg.surface.Surface(w, h, pixels) to like upg.surface.Surface(x, y, w, h, pixels) ?

I can return a Rect with surface.get_rect() but it seems like an immutable object, so not really useful to store some data.

Hmm…why would you like to have that?

Associating a surface with a position isn’t a good idea because you might want to use the same surface for more than one object and each object would have a different position.

Just write a class that combines a surface with a position:

class Object:
	def __init__(self, x, y, surface):
		self.x = x
		self.y = y
		self.surface = surface
		
	def draw(self, transparent_index = 0):
		surface.blit(self.surface, self.x, self.y, transparent_index)
		
	def get_rectangle(self):
		rect = self.surface.get_rect()
		rect.x = self.x
		recy.y = self.y
		return rect

(I’m assuming this is right, I don’t know much about the semantics of Python, so I don’t know if surface.get_rect() returns a copy or a reference.)

Come to think of it, if the x and y of surface.get_rect() will always be 0,
maybe get_width() and get_height() would be more appropriate?
Unless it’s done that way so there’s only one function instead of two,
I would presume function calls have a larger overhead in Python.

1 Like

Just popped to my mind that @jonne has said that io pins are mapped to the memory address, just like in the computers of the eighties. So if you know the right address, you can read the keys directly using memNN() functions. Not tested thought.

1 Like

Maybe I’m just confused about surface use. Considering it can return a rectangle I’ve supposed it contains all rectangle data (x,y,w,h) so it can be used as a Tile.
Otherwise it could be better to just create a class Tile that contains all rectangle data and a reference to the pixel data.
@Pharap proposed class is just what I’m using now, but it looks like a waste of space to define x and y again and not use the surface position instead.