-1

main.py:

import keyboard
import ui
import os

os.system("cls")
ui.play[ui.counter] = "> " + ui.play[ui.counter] + " <"
ui.navmenuprint(ui.play)

while True:
    while ui.state == "play":
        keypressed = keyboard.read_key()
        while keyboard.is_pressed("down"): pass
        while keyboard.is_pressed("up"): pass
        while keyboard.is_pressed("enter"): pass

        if keypressed == "up":
            os.system("cls")
            ui.navup(ui.play, ui.play2)
            ui.navmenuprint(ui.play)
        if keypressed == "down":
            os.system("cls")
            ui.navdown(ui.play, ui.play2)
            ui.navmenuprint(ui.play)
        if keypressed == "enter":
            if ui.counter == 0:
                ui.switchstate("shop")
            if ui.counter == 1:
                ui.switchstate("shop")
            if ui.counter == 2:
                ui.switchstate("shop")
            if ui.counter == 3:
                ui.switchstate("shop")

    while ui.state == "shop":
        keypressed = keyboard.read_key()
        while keyboard.is_pressed("down"): pass
        while keyboard.is_pressed("up"): pass
        while keyboard.is_pressed("enter"): pass
        
        if keypressed == "up":
            os.system("cls")
            ui.navup(ui.shop, ui.shop2)
            ui.navmenuprint(ui.shop)
        if keypressed == "down":
            os.system("cls")
            ui.navdown(ui.shop, ui.shop2)
            ui.navmenuprint(ui.shop)
        if keypressed == "enter":
            if ui.counter == 0:
                ui.switchstate("play")
            if ui.counter == 1:
                ui.switchstate("play")
            if ui.counter == 2:
                ui.switchstate("play")
            if ui.counter == 3:
                ui.switchstate("play")
            if ui.counter == 4:
                ui.switchstate("play")

ui.py:

import os
from termcolor import cprint

state = "play"
counter = 0
play = ["TOSHOP", "TOSHOP", "TOSHOP","TOSHOP"]
play2 = ["TOSHOP", "TOSHOP", "TOSHOP","TOSHOP"]
shop = ["TOPLAY", "TOPLAY","TOPLAY","TOPLAY","TOPLAY"]
shop2 = ["TOPLAY", "TOPLAY","TOPLAY","TOPLAY","TOPLAY"]

def switchstate(fromwhere):
    global state, counter
    if fromwhere == "play":
        counter = 0
        state = fromwhere
        play = play2.copy()
        os.system("cls")
        play[counter] = "> " + play[counter] + " <"
        navmenuprint(play)
    if fromwhere == "shop":
        counter = 0
        state = fromwhere
        shop = shop2.copy()
        os.system("cls")
        shop[counter] = "> " + shop[counter] + " <"
        navmenuprint(shop)

def navup(list1, list2):
    global counter
    if counter != 0:
        list1[counter] = list2[counter]
        counter -= 1
        list1[counter] = "> " + list1[counter] + " <"
    else:
        list1[counter] = list2[counter]
        counter -= 1
        list1[counter] = "> " + list1[counter] + " <"
        counter = len(list1) - 1
    print (counter)

def navdown(list1,list2):
    global counter
    if counter != len(list1) - 1:
        list1[counter] = list2[counter]
        counter += 1
        list1[counter] = "> " + list1[counter] + " <"
    else:
        list1[counter] = list2[counter]
        counter = 0
        list1[counter] = "> " + list1[counter] + " <"
    print (counter)

def navmenuprint(list):
    global counter
    for i in list:
        print(i)

This code is an extract from my little homemade console game project, I tried to delete all unnecessary code, I successfully made a working interactive menu which means I want to achieve navigation with up and down arrow in menu and currently selected item show as > item <, handle error if list out of index, state handling (for switching screens). Unfortunately I had to make a few ugly workaround to make this happen or I just too beginner to figure it out. Python 3.11, I don't want to use additional modules.

The problem:

  1. Go down to 4th item (counter variable value will be 3)
  2. Press Enter
  3. Go down to 5th item (counter variable value will be 4)
  4. Press Enter
  5. Press down

Actual:

TOSHOP    
> TOSHOP <
TOSHOP    
> TOSHOP <

Expected:

TOSHOP    
> TOSHOP <
TOSHOP    
TOSHOP

I understand my code and spent many hours to solve this issue but I have no idea why it's faulty. I think counter variable value is good everywhere. I make sure to reset "play" and "shop" list to original form and counter variable to 0.

Sedus
  • 1
  • 3
  • Unrelated suggestion: your `navup` and `navdown` counter wrapping could be simplified to `counter = (counter ± 1) % len(list1)` – Dash Nov 26 '22 at 01:15

1 Answers1

0

I had to expand global variables with lists inside switchstate function:

def switchstate(fromwhere):
    global state, counter, play, play2, shop, shop2
Sedus
  • 1
  • 3
  • Note that [global variables are evil](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil). It's much better to put your code in a class and use class variables instead. It's very easy, avoids all this trouble, and makes your code better maintainable and reusable in the future. (For example, if you would extend your game to become a multi-player game and you would need multiple state machines, you would have to completely rewrite your code when using global variables, while you could simply create a new object when using classes). – wovano Nov 26 '22 at 12:11