import os import random import sys import time if os.name == "nt": import msvcrt else: import select import termios import tty SIRKA = 20 VYSKA = 15 RYCHLOST = 0.12 class Had: def __init__(self, x, y, delka=3): self.x = x self.y = y self.delka = delka self.ocas = [{"x": x - i, "y": y} for i in range(delka, 0, -1)] self.dx = 1 self.dy = 0 def vytvor_ovoce(had, existujici_ovoce): while True: pozice = { "x": random.randint(0, SIRKA - 1), "y": random.randint(0, VYSKA - 1), } if ( pozice != {"x": had.x, "y": had.y} and pozice not in had.ocas and pozice not in existujici_ovoce ): return pozice def herni_krok(had, ovoce): """Provede jeden krok herní logiky. Vrací False při nárazu, jinak True.""" return True def cteni_klavesy(): if os.name == "nt": if not msvcrt.kbhit(): return None znak = msvcrt.getch() if znak == b"\x03": # Ctrl+C return "q" if znak in (b"\x00", b"\xe0"): sipky = {b"H": "w", b"P": "s", b"K": "a", b"M": "d"} return sipky.get(msvcrt.getch()) return znak.decode("utf-8", errors="ignore").lower() else: if not select.select([sys.stdin], [], [], 0)[0]: return None znak = sys.stdin.read(1) if znak == "\x1b": if select.select([sys.stdin], [], [], 0.005)[0]: if sys.stdin.read(1) == "[" and select.select([sys.stdin], [], [], 0.005)[0]: sipky = {"A": "w", "B": "s", "D": "a", "C": "d"} return sipky.get(sys.stdin.read(1)) return None return znak.lower() def vykresli(had, ovoce): radky = [ f"Skóre: {had.delka - 3} [WASD / Šipky | Q = konec]", "┌" + "──" * SIRKA + "┐", ] for y in range(VYSKA): radek = ["│"] for x in range(SIRKA): pozice = {"x": x, "y": y} if x == had.x and y == had.y: radek.append("██") elif pozice in had.ocas: radek.append("▓▓") elif pozice in ovoce: radek.append("()") else: radek.append(" ") radek.append("│") radky.append("".join(radek)) radky.append("└" + "──" * SIRKA + "┘") sys.stdout.write("\033[H" + "\n".join(radky) + "\n") sys.stdout.flush() def main(): if os.name == "nt": os.system("") stare_nastaveni = None else: fd = sys.stdin.fileno() stare_nastaveni = termios.tcgetattr(fd) tty.setcbreak(fd) sys.stdout.write("\033[?25l\033[2J") sys.stdout.flush() try: had = Had(x=SIRKA // 2, y=VYSKA // 2, delka=3) ovoce = [] for _ in range(3): ovoce.append(vytvor_ovoce(had, ovoce)) posledni_krok = time.time() zmenen_smer = False vykresli(had, ovoce) while True: klavesa = cteni_klavesy() if klavesa == "q": break if not zmenen_smer and klavesa: if klavesa == "w" and had.dy != 1: had.dx, had.dy = 0, -1 zmenen_smer = True elif klavesa == "s" and had.dy != -1: had.dx, had.dy = 0, 1 zmenen_smer = True elif klavesa == "a" and had.dx != 1: had.dx, had.dy = -1, 0 zmenen_smer = True elif klavesa == "d" and had.dx != -1: had.dx, had.dy = 1, 0 zmenen_smer = True if time.time() - posledni_krok >= RYCHLOST: posledni_krok = time.time() zmenen_smer = False if not herni_krok(had, ovoce): vykresli(had, ovoce) print(f"\nKONEC HRY! Finální skóre: {had.delka - 3}") break vykresli(had, ovoce) time.sleep(0.01) finally: sys.stdout.write("\033[?25h") sys.stdout.flush() if stare_nastaveni is not None: termios.tcsetattr(sys.stdin.fileno(), termios.TCSADRAIN, stare_nastaveni) if __name__ == "__main__": main()