1

So basically I making a small card game using Python3 for a school project . I use curses library to create a simple main menu where I have Play,Rules,Exit as options . I can't seem to find out how to make the curses window return to terminal when I press Play and begin the game. PS. The game code is gonna be imported (my code is in a different py file but in the same location).

import time
import curses
from GameLogic import controler , show_ui, take_input

menu = ['Play', 'Rules', 'Exit']
exit_but = ['Yes', 'No']

def print_menu(window, selected_row_idx):
    window.clear()
    # get height and width of screen
    h, w = window.getmaxyx()

    for idx, row in enumerate(menu) :
        x = w//2 - len(row)//2
        y = h//2 - len(menu)//2 + idx
        if  idx == selected_row_idx:
            # activate the color pair scheme
            window.attron(curses.color_pair(1))
            # write something
            window.addstr(y, x, row)
            # deactivate the color pair scheme
            window.attroff(curses.color_pair(1))
        else :
            window.addstr(y, x, row)

def print_exit(window, exit_row):
    window.clear()
    text = "Are you sure you want to Exit?"
    h, w = window.getmaxyx()

    x = w//2 - len(text)//2 - 2
    y = h//2 - 2

    window.addstr(y, x, text)

    for idx, row in enumerate(exit_but) :
        x = w//2 - len(row)//2
        y = h//2 - len(exit_but)//2 + idx
        if idx == exit_row :
            window.attron(curses.color_pair(1))
            window.addstr(y, x, row)
            window.attroff(curses.color_pair(1))
        else :
            window.addstr(y, x, row)

def game_rules(window):
    window.clear()

    text =  "=== GAME RULES ==="
    h, w = window.getmaxyx()
    x = w//2 - len(text)//2 - 5
    y = h//2 - 5
    window.addstr(y, x, text)

    rules = [
        "Initially 7 cards are dealt to each player and one card stays down.",
        "Players must play one card on their turn. A card can be played if it has",
        "the same number or same suit with the down card. If a player cannot play a",
        "card, he should pull one from the deck and if he still can't play game",
        "carries on to next player"
    ]

    for i in range (1,len(rules)+1):
        x2 = w//2 - len(rules[i-1])//2 - (5-i)
        y2 = h//2 - (5-i)
        window.addstr(y2, x2, rules[i-1])



def main(window):
    #Turn off cursor blinking
    curses.curs_set(0)
    # create color pair scheme
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)

    current_row_idx = 0

    print_menu(window, current_row_idx)

    while 1:
        key = window.getch()

        window.clear()

        if  key == curses.KEY_UP and current_row_idx > 0 :
            current_row_idx -= 1
        elif key == curses.KEY_DOWN and current_row_idx < len(menu)-1 :
            current_row_idx += 1
        elif key == curses.KEY_ENTER or key in [10,13] :


            #IF PRESS RULES
            if current_row_idx == 1 :
                game_rules(window)

            #IF PRESS EXIT
            elif current_row_idx == len(menu)-1 :

                current_exit_row = 0
                print_exit(window, current_exit_row)

                while 1:
                    exit_key = window.getch()
                    window.clear()

                    if exit_key == curses.KEY_UP and current_exit_row > 0:
                        current_exit_row -= 1
                    elif exit_key == curses.KEY_DOWN and current_exit_row < len(exit_but)-1 :
                        current_exit_row += 1
                    elif exit_key == curses.KEY_ENTER or exit_key in [10,13] :
                        if current_exit_row == 0 :
                            quit()
                        else:
                            break
                        window.refresh()
                        window.getch()

                    print_exit(window,current_exit_row)
                    window.refresh()

            #IF PRESS PLAY
            elif current_row_idx == 0 :

            window.refresh()
            window.getch()   

        print_menu(window, current_row_idx)
        # update the screen
        window.refresh()


curses.wrapper(main)

This is where I'm suppose to make it happen.

#IF PRESS PLAY
elif current_row_idx == 0 :

Thanks in advance.

RazusX
  • 31
  • 5
  • Possible duplicate of https://stackoverflow.com/questions/2233006/where-do-stdout-and-stderr-go-when-in-curses-mode – tripleee Jan 06 '20 at 11:32
  • Does this answer your question? [Breaking out of nested while True loop](https://stackoverflow.com/questions/54064178/breaking-out-of-nested-while-true-loop) – stovfl Jan 06 '20 at 13:53

1 Answers1

0

Apparently , in the

#IF PRESS PLAY
elif current_row_idx == 0 :

All I had to do was add the edwin() and import the game file like this:

elif current_row_idx == 0 :
    curses.endwin()
    import play

And it worked as I wanted it to work.

RazusX
  • 31
  • 5