Copy Link
Add to Bookmark
Report

NULL mag Issue 07 24 Python Lightbar menu

eZine's profile picture
Published in 
null magazine
 · 26 Dec 2020

  




lets create a lightbar menu, with some special uses. the following
code assumes that you are using my pycrt.py library from here:

http://github.com/xqtr/pycrt/

with small changes in the readkey() function, you can easily use the
python_bbs library and convert it to a mystic MPY script.

our lightbar will have the following features:

1. each item, can be displayed anywhere in the screen. we can put the
items next to each other, below, diagonal or even in random
places. this way we can have a very interesting menu and not just
a "classic" vertical one.
2. each item, can have a low/normal attribute and a highlighted /
selected one. to achieve this, we can just use color attributes or
pipe codes inside the text description.
3. we will be able to select items, by using cursor keys but also by
using hotkeys, for more quick use of the menu.
4. each item will have a code, that it will be returned when the
specific item is selected with enter or the hotkey. it's more easy
to remember a string code, than index numbers.
5. we can use as many items we want. the only limitation is the size
of the screen... :p


just to make it easy on ourselves, we use the following function to
create an item. each item, is a dictionary, so instead of trying to
remember each value for the dictionary fields, we use this function,
which is straightforward.

def baradditem(x,y,low,high,lowtext,hightext,key,code):
return {'x':x,'y':y,'low':low,'high':high,'textlow':lowtext, \
'texthigh':hightext, 'key':key, 'code':code}

to create an item, we just do this:

item1 = baradditem(10,10,7,14,'|01[h]|03ello','|17|15[H]ELLO','hH','hello')

to hold all items information we just put each item inside a list like
this:

allitems.append(item1)
allitems.append(item2)
...
allitems.append(itemx)


now lets go to our main function... our function expects the list of
the items and if we want we can select the default selected item. by
default item index 0, will be selected. remember our list is zero
based, which means that if we have 5 items, their index number will be
0,1,2,3,4.

def lightbarmenu(items,sel=0):
# initialize our result variable
res = ""

# go into a loop
while True:

# display the normal/unselected string of each item
for i in range(len(items)):
writexypipe(items[i]['x'],items[i]['y'],items[i]['low'], \
mcilen(items[i]['textlow']),items[i]['textlow'])

# after we displayed all items, we also display the highlighted string
# of our selected item.
writexypipe(items[sel]['x'],items[sel]['y'],items[sel]['high'], \
mcilen(items[sel]['texthigh']),items[sel]['texthigh'])

# expect user input
ans = readkey()

# if user presses up/left cursor keys, decrease the selection index
if ans == "#up" or ans == "#left":
sel -= 1
if sel < 0: sel = len(items) - 1

# if user presses down/right cursor keys, increase the selection index
elif ans == "#down" or ans == "#right":
sel += 1
if sel > len(items) - 1 : sel = 0
# if esc is pressed, exit with an #esc code
elif ans == "#esc":
res == "#esc"
break
# if the user pressed enter, return the 'code' value of the selected
# item.
elif ans == "#enter":
res = items[sel]['code']
break
# if the user pressed something else, check to see if it's a hotkey. we
# check each items hotkey to see if matches the pressed key. if it is
# return the item 'code' field.
else:
for i in range(len(items)):
if ans in items[i]['key']:
res = items[i]['code']
return res
return res


that was it!!! simple and short! you can create very cool lightbar menus
to use in your bbs or a door game or just a simple local terminal game
or script.

below is the code for the main function, with no comments to easily copy
paste.

,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,./,.

def lightbarmenu(items,sel=0):
res = ""
while True:
for i in range(len(items)):
writexypipe(items[i]['x'],items[i]['y'],items[i]['low'], \
mcilen(items[i]['textlow']),items[i]['textlow'])
writexypipe(items[sel]['x'],items[sel]['y'],items[sel]['high'], \
mcilen(items[sel]['texthigh']),items[sel]['texthigh'])

ans = readkey()
if ans == "#up" or ans == "#left":
sel -= 1
if sel < 0: sel = len(items) - 1
elif ans == "#down" or ans == "#right":
sel += 1
if sel > len(items) - 1 : sel = 0
elif ans == "#esc":
res == "#esc"
break
elif ans == "#enter":
res = items[sel]['code']
break
else:
for i in range(len(items)):
if ans in items[i]['key']:
res = items[i]['code']
return res
return res



← previous
next →
loading
sending ...
New to Neperos ? Sign Up for free
download Neperos App from Google Play
install Neperos as PWA

Let's discover also

Recent Articles

Recent Comments

Neperos cookies
This website uses cookies to store your preferences and improve the service. Cookies authorization will allow me and / or my partners to process personal data such as browsing behaviour.

By pressing OK you agree to the Terms of Service and acknowledge the Privacy Policy

By pressing REJECT you will be able to continue to use Neperos (like read articles or write comments) but some important cookies will not be set. This may affect certain features and functions of the platform.
OK
REJECT