Rearrange code again

This commit is contained in:
Samuel Sloniker 2021-08-20 12:05:28 -07:00
parent b88c3b648a
commit d5a534a173

141
worm.py
View File

@ -129,14 +129,81 @@ def run(*_):
return return
def await_keys(keys):
while True:
k = sys.stdin.read(1)
if k in keys:
return k
term = Terminal() term = Terminal()
info_1 = term.clear() + term.move(0, 0) + term.on_red(' worm') \
+ term.bright_cyan_on_red('.py ') + f''' v1.0: bsdgames worm, ported \
to Python and improved
See https://github.com/kj7rrv/worm.py for source code and installation
instructions.
Thanks to the authors of the following libraries:
* blessings\t\t{term.blue("https://pypi.org/project/blessings/")}
* timeout-decorator\t\
{term.blue("https://pypi.org/project/timeout-decorator/")}
Also, thanks to the devolopers of Python and bsdgames worm. It would have been
much harder to port worm to Python if either if either worm or Python did not
exist.
Use the arrow keys or WASD to move. Try to get the green numbers, but don't
let the worm run into itself or the red edge.
To change the initial length of the worm, add the desired length of the worm
after `{term.bright_red('worm')}{term.bright_cyan('.py')}`, as in \
`{term.bright_red('worm')}{term.bright_cyan('.py')} 20` for a twenty-character\
-long worm.
{term.bright_red('worm')}{term.bright_cyan('.py')} is released under the MIT \
license.'''\
+ '{}Press {} to continue, {} to exit the game...'.format(
term.move(height - 1, 0),
term.bold_green('C'),
term.bold_red('Ctrl-C')
)
info_2 = term.clear() + term.move(0, 0) + term.on_red(' worm') \
+ term.bright_cyan_on_red('.py ') + f''' Copyright and License Info
Copyright (c) 2021 Samuel L. Sloniker
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
''' + term.bold('''THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY \
KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.''') \
+ '{}Press {} to return to the game, {} to exit...'.format(
term.move(height - 1, 0),
term.bold_green('C'),
term.bold_red('Ctrl-C')
)
height = term.height height = term.height
width = term.width width = term.width
last_dir = 'x' last_dir = 'x'
if len(sys.argv) == 2: if len(sys.argv) == 2:
try: try:
size = int(sys.argv[1]) size = int(sys.argv[1])
@ -147,11 +214,8 @@ if len(sys.argv) == 2:
else: else:
size = 7 size = 7
score = 0 score = 0
worm_y = height // 2 worm_y = height // 2
worm_locations = [[i+10, worm_y] for i in range(size)] worm_locations = [[i+10, worm_y] for i in range(size)]
@ -165,84 +229,23 @@ while worm_head == bonus_location or bonus_location in worm_locations:
random.randint(2, height-2), random.randint(2, height-2),
] ]
do_automove = True do_automove = True
do_help = False do_help = False
try: try:
with term.fullscreen(): with term.fullscreen():
os.system('stty raw -echo') os.system('stty raw -echo')
draw_frame() draw_frame()
while True: while True:
if do_help: if do_help:
for info in (info_1, info_2):
os.system('stty -raw') os.system('stty -raw')
print(term.clear() + term.move(0, 0) + term.on_red(' worm') + term.bright_cyan_on_red('.py ') + f''' v1.0: bsdgames worm, ported to Python and improved print(info, end='')
See https://github.com/kj7rrv/worm.py for source code and installation
instructions.
Thanks to the authors of the following libraries:
* blessings\t\t{term.blue("https://pypi.org/project/blessings/")}
* timeout-decorator\t{term.blue("https://pypi.org/project/timeout-decorator/")}
Also, thanks to the devolopers of Python and bsdgames worm. It would have been
much harder to port worm to Python if either if either worm or Python did not
exist.
Use the arrow keys or WASD to move. Try to get the green numbers, but don't
let the worm run into itself or the red edge.
To change the initial length of the worm, add the desired length of the worm
after `{term.bright_red('worm')}{term.bright_cyan('.py')}`, as in `{term.bright_red('worm')}{term.bright_cyan('.py')} 20` for a twenty-character-long worm.
{term.bright_red('worm')}{term.bright_cyan('.py')} is released under the MIT license.''')
print(term.move(height - 1, 0) + 'Press ' + term.bold_green('C') + ' to continue, ' + term.bold_red('Ctrl-C') + ' to exit the game...', end='')
sys.stdout.flush() sys.stdout.flush()
os.system('stty raw') os.system('stty raw')
while True:
k = sys.stdin.read(1)
if k in 'cC':
break
elif k in '':
# That string is Ctrl-C Ctrl-\, in case you editor doesn't handle
# control characters as well as Vim does.
sys.exit(0)
os.system('stty -raw')
print(term.clear() + term.move(0, 0) + term.on_red(' worm') + term.bright_cyan_on_red('.py ') + f''' Copyright and License Info
Copyright (c) 2021 Samuel L. Sloniker k = await_keys('cC')
if k in '':
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
''' + term.bold('''THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.'''))
print(term.move(height - 1, 0) + 'Press ' + term.bold_green('C') + ' to return to the game, ' + term.bold_red('Ctrl-C') + ' to exit...', end='')
sys.stdout.flush()
os.system('stty raw')
while True:
k = sys.stdin.read(1)
if k in 'cC':
break
elif k in '':
# That string is Ctrl-C Ctrl-\, in case you editor doesn't handle
# control characters as well as Vim does.
sys.exit(0) sys.exit(0)
do_help = False do_help = False