def setting_adjust(setting, label, min_val, max_val, delta=1): """ Used in the options menu, lets the user change the default options with the UP and DOWN keys. The actual setting, its label, value interval (and granularity) should be given as parameters. """ draw.logo() pyconio.textbackground(pyconio.RESET) pyconio.textcolor(pyconio.RESET) pyconio.gotoxy(20, 20) pyconio.write("{}: {:3}".format(label, setting), flush=True) pyconio.gotoxy(15, 29) pyconio.write("Irányítás: ↑ ↓ ENTER ESC") initial = setting pyconio.rawmode() key = pyconio.getch() while key != pyconio.ENTER: if key == pyconio.UP: if setting < max_val: setting += delta elif key == pyconio.DOWN: if setting > min_val: setting -= delta elif key == pyconio.ESCAPE: setting = initial break pyconio.gotoxy(20, 20) pyconio.write("{}: {:3}".format(label, setting), flush=True) key = pyconio.getch() return setting
def print(self): """ Prints the teromino to the screen. This method is required because of the newline character in the string. Using this print, the tetrominoes' new lines will be written to their proper position, not to the beginning of the new line. """ newline = 0 newrow = 0 start_x = self.pos[0] * 2 start_y = self.pos[1] pyconio.gotoxy(start_x, start_y) for c in str(self): if c == "\n": newline += 1 newrow = 0 pyconio.gotoxy(start_x, start_y + newline) elif c == " ": newrow += 1 pyconio.gotoxy(start_x + newrow, start_y + newline) else: pyconio.textcolor(self.color) pyconio.write(c)
def valsection(field, val, posy, label): """ Prints a section that is used for displaying the points and levels, as both the label and value should be given as parameters, in addition to the y position of the box. The box scales with the value's length, and has a height of 1 unit. """ pyconio.textcolor(pyconio.WHITE) pyconio.gotoxy(len(field) + len(field[0]) - len(label), posy + 1) pyconio.write(label) pyconio.gotoxy(len(field) + len(field[0]), posy) pyconio.write("╔" + "═" * len(str(val)) + "╗") pyconio.gotoxy(len(field) + len(field[0]), posy + 1) pyconio.write("║{}║".format(val)) pyconio.gotoxy(len(field) + len(field[0]), posy + 2) pyconio.write("╚" + "═" * len(str(val)) + "╝")
def logo(file="logo.txt"): """ Prints the content of the given file under logos/ (or logo.txt as default), used for printing the title Tetris (in Russian), with the original colors. To use different colors for printing, a pipe character must be used as separator between different sections. """ pyconio.gotoxy(0, 5) colors = [ pyconio.RED, pyconio.BROWN, pyconio.YELLOW, pyconio.GREEN, pyconio.CYAN, pyconio.MAGENTA ] with open("logos/{}".format(file), "rt", encoding="utf-8") as logo: for line in logo: letters = line.rstrip("\n").split("|") for letter in range(len(letters)): pyconio.textcolor(colors[letter]) pyconio.write(letters[letter]) pyconio.write("\n") pyconio.flush() pyconio.textbackground(pyconio.RESET) pyconio.textcolor(pyconio.RESET)
def menu(buttons, data=None): """ Common menu mechanism, including the indication of the selected button and navigation. Returns None if the user selected quit (either by pressing ESC or selecting the option), or executes the selected function otherwise, with optional data. """ pyconio.textcolor(pyconio.WHITE) buttons[0].active = True draw.logo() while True: for btn in buttons: if btn.active: pyconio.textbackground(pyconio.WHITE) pyconio.textcolor(pyconio.BLACK) else: pyconio.textbackground(pyconio.RESET) pyconio.textcolor(pyconio.RESET) pyconio.gotoxy(20, 20 + buttons.index(btn)) pyconio.write(btn) pyconio.gotoxy(15, 29) pyconio.textcolor(pyconio.RESET) pyconio.textbackground(pyconio.RESET) pyconio.write("Irányítás: ↑ ↓ ENTER ESC") pyconio.flush() pyconio.rawmode() key = pyconio.getch() active = buttons.index([x for x in buttons if x.active][0]) if key == pyconio.ENTER: return select(buttons[active].function, data) elif key == pyconio.DOWN: buttons[active].active = False if active == len(buttons) - 1: buttons[0].active = True else: buttons[active + 1].active = True elif key == pyconio.UP: buttons[active].active = False if active == 0: buttons[-1].active = True else: buttons[active - 1].active = True elif key == pyconio.ESCAPE: return select("quit")
def select(func, data=None): """ List of available functions in the menus. In some cases the menu outputs are handled locally, when that is needed for the functionality. (When working with local variables that are not available in this module.) """ pyconio.clrscr() # Used in main menu if func == "start": if data is not None: return ("new", data.size, data.level) else: return ("new", 20, 1) pyconio.clrscr() elif func == "toplist": return list_scores(get_scores(), (20, 20), data) elif func == "options": return options((20, 20), data) elif func == "load": return ("load", 20, 1) pyconio.clrscr() # Common quit mechanism elif func == "quit": pyconio.textbackground(pyconio.RESET) pyconio.textcolor(pyconio.RESET) pyconio.clrscr() return None # Options menu elif func == "size": return (func, setting_adjust(data.size, "Méret", 20, 50, 2)) elif func == "level": return (func, setting_adjust(data.level, "Kezdő szint", 1, 10)) # Pause, save, topscore save menu else: return func
def ground(field): """ Prints the playing field (matrix) in its current state. Boundaries are indicated with box-drawing characters, and tetrominos with block elements. """ pyconio.textcolor(pyconio.WHITE) pyconio.gotoxy(1, 0) pyconio.write("╔" + "═" * len(field) + "╗") for line in range(len(field)): pyconio.gotoxy(1, line + 1) pyconio.textcolor(pyconio.WHITE) pyconio.write("║") for row in range(len(field[line])): if field[line][row] != 0: pyconio.textcolor(get_color(field[line][row])) pyconio.write("█" * 2) else: pyconio.write(" " * 2) pyconio.textcolor(pyconio.WHITE) pyconio.write("║") pyconio.gotoxy(1, len(field) + 1) pyconio.write("╚" + "═" * len(field) + "╝")
import pyconio import time # Init pyconio.settitle("pyconio test") # Positioning pyconio.clrscr() pyconio.gotoxy(0, 0) pyconio.textcolor(pyconio.LIGHTGREEN) pyconio.write("Hello") pyconio.gotoxy(10, 0) pyconio.textbackground(pyconio.LIGHTBLUE) pyconio.write("world!") print() # Printing color codes print(pyconio.backgroundcolors[pyconio.RESET], end="") print("{}Hello {}world!".format(pyconio.textcolors[pyconio.RED], pyconio.textcolors[pyconio.GREEN])) # Color combinations for b in range(0, 16): pyconio.gotoxy(5, 5+b) for t in range(0, 16): pyconio.textcolor(t) pyconio.textbackground(b) pyconio.write(" X ") pyconio.write("\n") print() # Raw input
def list_scores(scorelist, pos, data=None): """ Lists the scores from the given scorelist to the given position. Adds numbering and colors (for the top 3) to the list elements, and prints out the proper errors when needed. Returns to the main menu, with optional data if passed. """ pyconio.clrscr() draw.logo() pyconio.gotoxy(pos[0], pos[1]) pyconio.textcolor(pyconio.RESET) if scorelist is None: pyconio.gotoxy(pos[0] - 15, pos[1]) pyconio.write("A file nem található, biztosan játszottál már?") elif scorelist == -1: pyconio.gotoxy(pos[0] - 10, pos[1]) pyconio.write("Hibás file, ellenőrizd a pontszámokat!") else: for i in range(len(scorelist)): if i + 1 == 1: pyconio.textcolor(pyconio.YELLOW) elif i + 1 == 2: pyconio.textcolor(pyconio.LIGHTGRAY) elif i + 1 == 3: pyconio.textcolor(pyconio.BROWN) else: pyconio.textcolor(pyconio.RESET) pyconio.gotoxy(pos[0], pos[1] + i) pyconio.write("{}. {}".format(i + 1, scorelist[i])) pyconio.gotoxy(pos[0] + 2, pos[1] + pos[1] // 2) pyconio.textcolor(pyconio.RESET) pyconio.write("Vissza: ESC") pyconio.flush() pyconio.rawmode() key = pyconio.getch() while key != pyconio.ESCAPE: key = pyconio.getch() pyconio.clrscr() return main_menu(data)
import pyconio pyconio.clrscr() print("Use cursor keys to control the asterisk") x = 40 y = 12 pyconio.rawmode() while True: pyconio.gotoxy(x, y) pyconio.textcolor(pyconio.LIGHTGREEN) pyconio.write("*") pyconio.gotoxy(80, 24) key = pyconio.getch() pyconio.gotoxy(x, y) pyconio.textcolor(pyconio.BLUE) pyconio.write(".") if key == pyconio.UP: y = max(y - 1, 1) elif key == pyconio.DOWN: y = min(y + 1, 23) elif key == pyconio.LEFT: x = max(x - 1, 0) elif key == pyconio.RIGHT: x = min(x + 1, 79) elif key == pyconio.ESCAPE: break pyconio.normalmode()