Remove curses and use os.get_window_size()

Signed-off-by: Skylar "The Cobra" Widulski <cobra@vern.cc>
This commit is contained in:
Skylar "The Cobra" Widulski 2023-06-17 15:56:54 -04:00
parent 02e7869ea2
commit eebbebdf7f
Signed by: cobra
GPG Key ID: 4FD8F812083FF6F9
2 changed files with 23 additions and 101 deletions

View File

@ -1,5 +1,3 @@
# snake.py
I made this a long time ago and thought it was lost, but when i was searching through my old phone's files, I found it.
I think I also took some of the window size code from stackoverflow (like I said, long time ago), but I don't remember who made it.

122
snake.py
View File

@ -2,14 +2,12 @@
import sys
import os
import curses
import tty
import termios
from select import select, error
from getopt import getopt, GetoptError
from math import floor
from subprocess import check_call
from random import randint, choice
from shlex import split
from struct import unpack
from time import sleep
STDIN_FILENO = sys.stdin.fileno()
@ -33,6 +31,19 @@ die_to_self=True
die_to_wall=True
score = 0
terminal_settings = termios.tcgetattr(STDIN_FILENO)
def __select(iwtd, owtd, ewtd):
while True:
try:
return select(iwtd, owtd, ewtd, 0)
except error:
err = sys.exc_info()[1]
if err.args[0] == errno.EINTR:
return([], [], [])
else:
raise
def hex_to_rgb(hexa):
try:
return tuple(int(hexa.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
@ -102,18 +113,10 @@ def cherry():
return (-1, -1)
def _help():
try:
curses.endwin()
except:
pass
print("snake [OPTIONS...]", end='\r\n')
exit()
def big_help():
try:
curses.endwin()
except:
pass
print(
"""python snake.py [OPTIONS...]
@ -151,104 +154,26 @@ OPTIONS:
exit()
def stop():
print('\033[0m', end='')
curses.curs_set(1)
curses.endwin()
print('\033[0m\033[?25h\033[?1049h', end='')
termios.tcsetattr(STDIN_FILENO, termios.TCSADRAIN, terminal_settings)
print(f'Score: {score}', end='\r\n')
exit()
def win():
print('\033[0m', end='')
curses.endwin()
print('\033[0m\033[?25h\033[?1049h', end='')
termios.tcsetattr(STDIN_FILENO, termios.TCSADRAIN, terminal_settings)
print('You win!', end='\r\n')
exit()
def get_terminal_size():
from platform import system
current_os = system()
tuple_xy = None
if current_os == 'Windows':
tuple_xy = _get_terminal_size_windows()
if tuple_xy is None:
tuple_xy = _get_terminal_size_tput()
if current_os in ['Linux', 'Darwin'] or current_os.startswith('CYGWIN'):
tuple_xy = _get_terminal_size_linux()
if tuple_xy is None:
print("default")
tuple_xy = (80, 25)
return tuple_xy
def _get_terminal_size_windows():
try:
from ctypes import windll, create_string_buffer
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
if res:
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom,
maxx, maxy) = unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
return sizex, sizey
except:
pass
def _get_terminal_size_tput():
try:
cols = int(check_call(['tput', 'cols']))
rows = int(check_call(['tput', 'lines']))
return (cols, rows)
except:
pass
def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
from fcntl import ioctl
from termios import TIOCGWINSZ
cr = unpack('hh', ioctl(fd, TIOCGWINSZ, '1234'))
return cr
except:
pass
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
def __select(iwtd, owtd, ewtd):
while True:
try:
return select(iwtd, owtd, ewtd, 0)
except error:
err = sys.exc_info()[1]
if err.args[0] == errno.EINTR:
return([], [], [])
else:
raise
try:
sx, sy = get_terminal_size()
sx, sy = os.get_terminal_size()
all_tuples = []
for i in range(sx):
for j in range(sy):
all_tuples.append((i, j))
direction = 0
curses.initscr()
curses.curs_set(0)
tty.setcbreak(STDIN_FILENO)
print('\033[?25l\033[?1049h', end='')
get_args()
for i in range(length):
x,y = floor(sx/2), floor(sy/2)+i
@ -258,8 +183,7 @@ try:
for i in range(len(cherries)):
cherries[i] = cherry()
while True:
r, w, e = __select([STDIN_FILENO], [], [])
if STDIN_FILENO in r:
if STDIN_FILENO in __select([STDIN_FILENO], [], [])[0]:
data=os.read(STDIN_FILENO, 1)
if len(data) == 0:
stop()