Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - rli

Pages: 1
1
Thumby / Re: Iterating through a dictionary on Micro Python question
« on: October 31, 2021, 02:03:52 PM »
It looks good!

You're right about the comment, thanks for catching that. It is a dict.

2
Thumby / Re: Iterating through a dictionary on Micro Python question
« on: October 30, 2021, 09:29:55 PM »
Hi Brian,

There are a few changes I think you need to make. Here's my interpretation of what you want as well as a few other options:
Code: [Select]
sprite_1 = (...) # Some 8x8 sprite data

sprite_invader_a = (...) # Some other 8x8 sprite data

inv = [] # A Python list
# sprite number, x, y
inv.append({'s':sprite_invader_a, 'x':40, 'y':28})
inv.append({'s':sprite_1, 'x':50, 'y':28})
inv.append({'s':sprite_1, 'x':60, 'y':28})
inv.append({'s':sprite_1, 'x':70, 'y':28})
inv.append({'s':sprite_1, 'x':80, 'y':28})

while(1):
   thumby.display.fill(0)
   for invader in inv:
      thumby.display.blit(invader['s'],invader['x'], invader['y'], 8, 8, 0)

To remove an element from inv:
Code: [Select]
# By index:
del inv[0] # removes invader_a
del inv[0] # removes {'s':sprite_1, 'x':50, 'y':28}

# By value:
inv.remove({'s':sprite_invader_a, 'x':40, 'y':28}) # the whole dict needs to be passed in, which might not make sense for you

# This probably won't do what you expect (deleting all elements) because the indices change as you delete elements:
for invader_i in range(len(inv)):
    del inv[invader_i]


Other methods:
I'm not sure what the rest of your game code will look like, but for a space invaders style game, I would consider a pre-allocated list or using a dict to store information about game objects.

If you're unsure of the index of any particular object and you want to remove an element a little more efficiently than searching through the list and removing by index, I would recommend using a Python dict like this:
Code: [Select]
sprite_1 = (...) # Some 8x8 sprite data

sprite_invader_a = (...) # Some other 8x8 sprite data

inv = {} # A Python dict
# sprite number, x, y
inv[0] = {'s':sprite_invader_a, 'x':40, 'y':28} # The key/id (0) can be any immutable object. Usually it's an integer as shown here or a string like 'a', 'b', 'c'
inv[1] = {'s':sprite_1, 'x':50, 'y':28}
inv[2] = {'s':sprite_1, 'x':60, 'y':28}
inv[3] = {'s':sprite_1, 'x':70, 'y':28}
inv[4] = {'s':sprite_1, 'x':80, 'y':28}

while(1):
   thumby.display.fill(0)
   for inv_id in inv:
      invader = inv[inv_id]
      thumby.display.blit(invader['s'],invader['x'], invader['y'], 8, 8, 0)

# Removing an object:
del inv[3] # Removes {'s':sprite_1, 'x':70, 'y':28}
del inv[4] # Removes {'s':sprite_1, 'x':80, 'y':28}
del inv[0] # Removes {'s':sprite_invader_a, 'x':40, 'y':28}
Note the difference in what's removed compared to removal by index in the list version above.

If you pre-allocate a list instead, you can use the value None to mark an object as deleted:
Code: [Select]
inv = [None] * 5 # a List
inv[0] = {'s':sprite_invader_a, 'x':40, 'y':28}
inv[1] = {'s':sprite_1, 'x':50, 'y':28}
inv[2] = {'s':sprite_1, 'x':60, 'y':28}
inv[3] = {'s':sprite_1, 'x':70, 'y':28}
inv[4] = {'s':sprite_1, 'x':80, 'y':28}

# Removal:
inv[3] = None

Same thing:
Code: [Select]
inv = [ # List
    {'s':sprite_invader_a, 'x':40, 'y':28},
    {'s':sprite_1, 'x':50, 'y':28},
    {'s':sprite_1, 'x':60, 'y':28},
    {'s':sprite_1, 'x':70, 'y':28},
    {'s':sprite_1, 'x':80, 'y':28}
]

# Removal:
inv[3] = None

Good luck!

Edit: left an incorrect comment in there. Good catch

3
Thumby / Re: Option to Turn the Thumby Emulator Sideways
« on: October 22, 2021, 04:40:44 PM »
For a quick hack while prototyping right now, you can rotate the whole HTML element using the transform property. See my attachment for details.

Obviously, this won't rotate the d-pad mapping or how Screen X and Y are defined.

4
Thumby / Re: Thumby development Discord server or something similar?
« on: October 21, 2021, 09:36:25 AM »
Exciting!

My thoughts on how it would differentiate between the forum, Github, KS comments, etc is primarily:
  • Having a real-time way to chat. The forum, github, and KS comments aren't really good for having casual conversations
  • Having a lower friction way for new developers to get started. My experience is that many people getting into a new "thing" will find a Discord server for it and ask for help there because chatrooms tend to be friendlier to novices since the conversations are more ephemeral.
  • As a result, it keeps the forum tidier for longer-form posts, discussions, releases, and tutorials/guides

5
Thumby / Re: Thumby development Discord server or something similar?
« on: October 18, 2021, 06:31:05 PM »
I'd be in favor of a simple Discord server as well, official or unofficial.

I know the TinyCircuits team is small and has a lot of responsibilities already so I'm not sure they want to tack on chatroom moderation to that.

@TinyCircuits, are you planning to create some sort of TinyCircuits Discord soon? If not, maybe we can create an unofficial one?

6
Thumby / Re: Is the Random Function broken for anyone else?
« on: October 17, 2021, 02:43:03 PM »
Do you have example code? I think I was able to reproduce the issue you're describing.

I believe the "problem" is that the random number generator's seed doesn't change between runs of the emulator.

Here's my test:
Code: [Select]
import random
import time

l = [0,1,2,3]

for i in range(20):
    r = random.choice(l)
    print(f"{i}: {r}")

# Outputs:
# 0: 1
# 1: 0
# 2: 1
# 3: 3
# 4: 0
# 5: 3
# 6: 3
# 7: 3
# 8: 1
# 9: 1
# 10: 3
# 11: 3
# 12: 3
# 13: 1
# 14: 1
# 15: 2
# 16: 1
# 17: 2
# 18: 3
# 19: 1

The output is the same each time.

Relevant documentation: https://docs.micropython.org/en/latest/library/random.html?highlight=random#random.seed

Some usual tricks for initializing a seed for less deterministic results is passing in the time, but because the emulator has no true Real Time Clock(RTC) or this MICROPY_PY_URANDOM_SEED_INIT_FUNC setting enabled, all the random number functions will act deterministically.

In practice, you might be fine with passing the time.ticks_ms() or time.ticks_us() as the seed (https://docs.micropython.org/en/latest/library/time.html?highlight=time#time.ticks_ms):
Code: [Select]
import random
import time

random.seed(time.ticks_us()) # Relies on minor inconsistencies to differ a little each time

l = [0,1,2,3]

for i in range(20):
    r = random.choice(l)
    print(f"{i}: {r}")

It's not great and depending on how early on in the execution you're getting random numbers, it might get the same seed more often than not.


EDIT: I'm just stating what appears to be happening in the emulator. I don't know if the actual hardware will implement the micropython option or at least have working RTC so we can have some source of noise. Maybe the engineers/devs can answer?

7
Thumby / Re: My Game Mockups
« on: October 17, 2021, 03:57:27 AM »
I like the mockups designs, especially the pacman!

Holding the console sideways for tetris makes me a little ergonomically worried lol. I think if you wanted to try your hand at implementing this one, you could start by modifying the TinyBlocks game to work in your proposed screen orientation, then add support for smaller fonts for the score and add support for different tetromino sprites.

For the solitaire game, how do you intend to show the values of the cards? The screen real estate looks pretty tight.

For the calculator, I imagine the UI would involve a small white dot that acts as a cursor? Appearing in the corner of a button when you move the cursor to it? And then maybe you could invert the button when you press on it?
What would the "RN" and "|" buttons do, btw?

8
Thumby / Magic 8-Ball App
« on: October 17, 2021, 03:06:43 AM »
Hacked together a simple app today: https://github.com/raymond-li/thumby_magic_8_ball

Gif of it in action:


Anyone interested can change the sprite and the messages by modifying the code at the top of the file!


I'm unfamiliar with micropython and haven't done very much game programming so I had to spend some time writing code just to test simple functionality I wanted to put in the end product. The lack of feedback when something goes wrong in the emulator forced me to write code and test very incrementally. The sprite also took many iterations to get to something I liked.
Edit: It turns out you can open the Chrome console (Ctrl + Shift + J on Windows/Linux) and get the usual stdout and stderr along with other activity on the page!

You can see a bit of my development process in this version of the code: https://github.com/raymond-li/thumby_magic_8_ball/blob/main/magic_8_ball_dev.py

Attached as well (from commit 1ea840b02a1ef8536832fe7b5a8d0cddd4723378)

Pages: 1
SMF spam blocked by CleanTalk