Rearrange code, and improve xterm compatibility
This commit is contained in:
parent
b22a6e2fea
commit
b88c3b648a
81
worm.py
81
worm.py
|
@ -9,12 +9,6 @@ import timeout_decorator
|
||||||
import os
|
import os
|
||||||
from blessings import Terminal
|
from blessings import Terminal
|
||||||
|
|
||||||
term = Terminal()
|
|
||||||
|
|
||||||
height = term.height
|
|
||||||
width = term.width
|
|
||||||
|
|
||||||
last_dir = 'x'
|
|
||||||
|
|
||||||
class QuitGameError(BaseException):
|
class QuitGameError(BaseException):
|
||||||
pass
|
pass
|
||||||
|
@ -23,19 +17,6 @@ class QuitGameError(BaseException):
|
||||||
class RanIntoSomethingError(QuitGameError):
|
class RanIntoSomethingError(QuitGameError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if len(sys.argv) == 2:
|
|
||||||
try:
|
|
||||||
size = int(sys.argv[1])
|
|
||||||
if size <= 0 or size >= width-13:
|
|
||||||
size = 7
|
|
||||||
except ValueError:
|
|
||||||
size = 7
|
|
||||||
else:
|
|
||||||
size = 7
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
score = 0
|
|
||||||
|
|
||||||
def draw_worm():
|
def draw_worm():
|
||||||
for location in worm_locations:
|
for location in worm_locations:
|
||||||
|
@ -58,10 +39,11 @@ def draw_frame():
|
||||||
+ term.white_on_red('─' * (width-3))
|
+ term.white_on_red('─' * (width-3))
|
||||||
+ term.white_on_red('┘')
|
+ term.white_on_red('┘')
|
||||||
, end='')
|
, end='')
|
||||||
print(term.move(*reversed(bonus_location)) + term.black_on_bright_green(str(bonus_points)), end='')
|
print(term.move(*reversed(bonus_location)) + term.black_on_green(term.on_bright_green(str(bonus_points))), end='')
|
||||||
draw_worm()
|
draw_worm()
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
def move_head(direction, distance):
|
def move_head(direction, distance):
|
||||||
global size, worm_head, last_dir, worm_locations, bonus_location, bonus_points, score
|
global size, worm_head, last_dir, worm_locations, bonus_location, bonus_points, score
|
||||||
|
|
||||||
|
@ -111,6 +93,7 @@ def move_head(direction, distance):
|
||||||
if distance > 1:
|
if distance > 1:
|
||||||
move_head(direction, distance-1)
|
move_head(direction, distance-1)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def decanonize(fd):
|
def decanonize(fd):
|
||||||
old_settings = termios.tcgetattr(fd)
|
old_settings = termios.tcgetattr(fd)
|
||||||
|
@ -120,22 +103,6 @@ def decanonize(fd):
|
||||||
yield
|
yield
|
||||||
termios.tcsetattr(fd, termios.TCSAFLUSH, old_settings)
|
termios.tcsetattr(fd, termios.TCSAFLUSH, old_settings)
|
||||||
|
|
||||||
worm_y = height // 2
|
|
||||||
|
|
||||||
worm_locations = [[i+10, worm_y] for i in range(size)]
|
|
||||||
worm_head = [size+10, worm_y]
|
|
||||||
|
|
||||||
bonus_points = random.randint(1, 9)
|
|
||||||
bonus_location = worm_head
|
|
||||||
while worm_head == bonus_location or bonus_location in worm_locations:
|
|
||||||
bonus_location = [
|
|
||||||
random.randint(1, width-3),
|
|
||||||
random.randint(2, height-2),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
do_automove = True
|
|
||||||
do_help = False
|
|
||||||
|
|
||||||
@timeout_decorator.timeout(1)
|
@timeout_decorator.timeout(1)
|
||||||
def run(*_):
|
def run(*_):
|
||||||
|
@ -161,6 +128,48 @@ def run(*_):
|
||||||
do_help = True
|
do_help = True
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
term = Terminal()
|
||||||
|
|
||||||
|
height = term.height
|
||||||
|
width = term.width
|
||||||
|
|
||||||
|
last_dir = 'x'
|
||||||
|
|
||||||
|
|
||||||
|
if len(sys.argv) == 2:
|
||||||
|
try:
|
||||||
|
size = int(sys.argv[1])
|
||||||
|
if size <= 0 or size >= width-13:
|
||||||
|
size = 7
|
||||||
|
except ValueError:
|
||||||
|
size = 7
|
||||||
|
else:
|
||||||
|
size = 7
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
score = 0
|
||||||
|
|
||||||
|
|
||||||
|
worm_y = height // 2
|
||||||
|
|
||||||
|
worm_locations = [[i+10, worm_y] for i in range(size)]
|
||||||
|
worm_head = [size+10, worm_y]
|
||||||
|
|
||||||
|
bonus_points = random.randint(1, 9)
|
||||||
|
bonus_location = worm_head
|
||||||
|
while worm_head == bonus_location or bonus_location in worm_locations:
|
||||||
|
bonus_location = [
|
||||||
|
random.randint(1, width-3),
|
||||||
|
random.randint(2, height-2),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
do_automove = True
|
||||||
|
do_help = False
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with term.fullscreen():
|
with term.fullscreen():
|
||||||
os.system('stty raw -echo')
|
os.system('stty raw -echo')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user