Új hozzászólás Aktív témák

  • sztanozs

    veterán

    No, nem annyira nehéz, csak meg kellett találni a megfelelő modult (meg, hogy windows-on is működjön).
    from os import system
    from sys import platform
    from pynput import keyboard
    from pynput.keyboard import Key

    def print_at_x(y, x, text):
    print(f"\033[{y};{x}H{text}")

    def print_at_w(r, c, s):
    h = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
    windll.kernel32.SetConsoleCursorPosition(h, COORD(c, r))
    prev_codepage = windll.kernel32.GetConsoleOutputCP()
    windll.kernel32.SetConsoleOutputCP(65001)
    c = s.encode("UTF-8")
    windll.kernel32.WriteConsoleA(h, c_char_p(c), len(c), None, None)
    windll.kernel32.SetConsoleOutputCP(prev_codepage)

    if platform == "linux" or platform == "linux2":
    # linux
    system('clear')
    print_at = print_at_x

    elif platform == "darwin":
    # MacOS, talán működik itt is
    system('clear')
    print_at = print_at_x

    elif platform == "win32":
    # Windows...
    from ctypes import *

    class COORD(Structure):
    pass

    STD_OUTPUT_HANDLE = -11

    COORD._fields_ = [("X", c_short), ("Y", c_short)]
    system('cls')

    print_at = print_at_w


    print_at(3, 15, 'Próbálj bent maradni a négyzetben!')

    for n in range(5,50):
    print_at(5, n, '*')
    print_at(20, n, '*')

    for n in range(5,20):
    print_at(n, 5, '*')
    print_at(n, 50, '*')

    x, y = 25, 7
    print_at(y, x, 'O')

    def on_press(key):
    global x, y
    if key == keyboard.Key.esc:
    return False
    elif key == Key.up:
    dx, dy = 0, -1
    elif key == Key.left:
    dx, dy = -1, 0
    elif key == Key.down:
    dx, dy = 0, 1
    elif key == Key.right:
    dx, dy = 1, 0
    else:
    return
    print_at(y, x, ' ')
    x, y = x + dx, y + dy
    print_at(y, x, 'O')
    if not (5 < x < 50) or not (5 < y < 20):
    return False

    with keyboard.Listener(on_press=on_press) as listener:
    try:
    listener.join()
    finally:
    listener.stop()
    print_at(20, 55, 'A játéknak vége !')

Új hozzászólás Aktív témák