Format code with black
This commit is contained in:
parent
39ff3a5672
commit
74035ad320
|
@ -29,26 +29,47 @@ class IsFullScreen(BaseException):
|
||||||
|
|
||||||
def draw_worm():
|
def draw_worm():
|
||||||
for location in worm_locations:
|
for location in worm_locations:
|
||||||
print(do_move(*reversed(location)) + term.bright_blue('o'), end='')
|
print(do_move(*reversed(location)) + term.bright_blue("o"), end="")
|
||||||
|
|
||||||
print(do_move(*reversed(worm_head)) + '@' + do_move(*reversed(worm_head)), end='')
|
print(do_move(*reversed(worm_head)) + "@" + do_move(*reversed(worm_head)), end="")
|
||||||
|
|
||||||
|
|
||||||
def draw_frame():
|
def draw_frame():
|
||||||
print(term.clear, end='')
|
print(term.clear, end="")
|
||||||
print(do_move(0, 0) + term.on_red(' Worm') + term.black_on_bright_cyan('.py ') + ' Press ' + term.bold_green('I') + ' for info, ' + term.bold_red('Ctrl-C')+ ' to quit', end='')
|
|
||||||
if score > -1:
|
|
||||||
print(do_move(0, width-12) + f'Score:{" "*(4-len(str(score)))}{term.bright_green(str(score))}', end='')
|
|
||||||
print(do_move(1, 0) + term.white_on_red('┌' + ('─' * (width-3)) + '┐'), end='')
|
|
||||||
for y in range(2, height-1):
|
|
||||||
print(do_move(y, 0) + term.white_on_red('│' + do_move(y, width-2) + '│'), end='')
|
|
||||||
print(
|
print(
|
||||||
do_move(height-1, 0)
|
do_move(0, 0)
|
||||||
+ term.white_on_red('└')
|
+ term.on_red(" Worm")
|
||||||
+ term.white_on_red('─' * (width-3))
|
+ term.black_on_bright_cyan(".py ")
|
||||||
+ term.white_on_red('┘')
|
+ " Press "
|
||||||
, end='')
|
+ term.bold_green("I")
|
||||||
print(do_move(*reversed(bonus_location)) + term.black_on_green(term.on_bright_green(str(bonus_points))), end='')
|
+ " for info, "
|
||||||
|
+ term.bold_red("Ctrl-C")
|
||||||
|
+ " to quit",
|
||||||
|
end="",
|
||||||
|
)
|
||||||
|
if score > -1:
|
||||||
|
print(
|
||||||
|
do_move(0, width - 12)
|
||||||
|
+ f'Score:{" "*(4-len(str(score)))}{term.bright_green(str(score))}',
|
||||||
|
end="",
|
||||||
|
)
|
||||||
|
print(do_move(1, 0) + term.white_on_red("┌" + ("─" * (width - 3)) + "┐"), end="")
|
||||||
|
for y in range(2, height - 1):
|
||||||
|
print(
|
||||||
|
do_move(y, 0) + term.white_on_red("│" + do_move(y, width - 2) + "│"), end=""
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
do_move(height - 1, 0)
|
||||||
|
+ term.white_on_red("└")
|
||||||
|
+ term.white_on_red("─" * (width - 3))
|
||||||
|
+ term.white_on_red("┘"),
|
||||||
|
end="",
|
||||||
|
)
|
||||||
|
print(
|
||||||
|
do_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()
|
||||||
|
|
||||||
|
@ -56,37 +77,39 @@ def draw_frame():
|
||||||
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
|
||||||
|
|
||||||
if direction not in 'hjklHJKLABCDwasdx':
|
if direction not in "hjklHJKLABCDwasdx":
|
||||||
raise ValueError('argument to move_head() must be one of `hjklHJKLABCDwasdx`')
|
raise ValueError("argument to move_head() must be one of `hjklHJKLABCDwasdx`")
|
||||||
|
|
||||||
if direction == 'x':
|
if direction == "x":
|
||||||
return
|
return
|
||||||
|
|
||||||
worm_locations.append(worm_head.copy())
|
worm_locations.append(worm_head.copy())
|
||||||
|
|
||||||
worm_head = worm_head.copy()
|
worm_head = worm_head.copy()
|
||||||
|
|
||||||
if direction in 'kKAw': #up
|
if direction in "kKAw": # up
|
||||||
worm_head[1] -= 1
|
worm_head[1] -= 1
|
||||||
last_dir = 'A'
|
last_dir = "A"
|
||||||
elif direction in 'jJBs': #down
|
elif direction in "jJBs": # down
|
||||||
worm_head[1] += 1
|
worm_head[1] += 1
|
||||||
last_dir = 'B'
|
last_dir = "B"
|
||||||
elif direction in 'hHDa': #left
|
elif direction in "hHDa": # left
|
||||||
worm_head[0] -= 1
|
worm_head[0] -= 1
|
||||||
last_dir = 'D'
|
last_dir = "D"
|
||||||
elif direction in 'lLCd': #right
|
elif direction in "lLCd": # right
|
||||||
worm_head[0] += 1
|
worm_head[0] += 1
|
||||||
last_dir = 'C'
|
last_dir = "C"
|
||||||
|
|
||||||
while len(worm_locations) > size:
|
while len(worm_locations) > size:
|
||||||
worm_locations.pop(0)
|
worm_locations.pop(0)
|
||||||
|
|
||||||
if worm_head in worm_locations \
|
if (
|
||||||
or worm_head[1] <= 1 \
|
worm_head in worm_locations
|
||||||
or worm_head[1] >= height-1 \
|
or worm_head[1] <= 1
|
||||||
or worm_head[0] <= 0 \
|
or worm_head[1] >= height - 1
|
||||||
or worm_head[0] >= width-2:
|
or worm_head[0] <= 0
|
||||||
|
or worm_head[0] >= width - 2
|
||||||
|
):
|
||||||
raise RanIntoSomethingError()
|
raise RanIntoSomethingError()
|
||||||
|
|
||||||
if worm_head == bonus_location:
|
if worm_head == bonus_location:
|
||||||
|
@ -95,7 +118,7 @@ def move_head(direction, distance):
|
||||||
new_bonus()
|
new_bonus()
|
||||||
|
|
||||||
if distance > 1:
|
if distance > 1:
|
||||||
move_head(direction, distance-1)
|
move_head(direction, distance - 1)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
|
@ -116,19 +139,19 @@ def run(*_):
|
||||||
draw_frame()
|
draw_frame()
|
||||||
|
|
||||||
do_automove = True
|
do_automove = True
|
||||||
|
|
||||||
save_game()
|
save_game()
|
||||||
|
|
||||||
k = await_keys('hjklHJKLABCDwasdiI')
|
k = await_keys("hjklHJKLABCDwasdiI")
|
||||||
if k in 'hjklHJKLABCDwasd':
|
if k in "hjklHJKLABCDwasd":
|
||||||
move_head(k, 10 if k in 'HJKL' else 1)
|
move_head(k, 10 if k in "HJKL" else 1)
|
||||||
draw_frame()
|
draw_frame()
|
||||||
do_automove = False
|
do_automove = False
|
||||||
return
|
return
|
||||||
elif k in '':
|
elif k in "":
|
||||||
save_game()
|
save_game()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif k in 'iI':
|
elif k in "iI":
|
||||||
do_help = True
|
do_help = True
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -146,9 +169,9 @@ def new_bonus():
|
||||||
bonus_location = worm_head
|
bonus_location = worm_head
|
||||||
while worm_head == bonus_location or bonus_location in worm_locations:
|
while worm_head == bonus_location or bonus_location in worm_locations:
|
||||||
bonus_location = [
|
bonus_location = [
|
||||||
random.randint(1, width-3),
|
random.randint(1, width - 3),
|
||||||
random.randint(2, height-2),
|
random.randint(2, height - 2),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def do_move(y, x):
|
def do_move(y, x):
|
||||||
|
@ -159,37 +182,65 @@ def do_move(y, x):
|
||||||
|
|
||||||
def save_game():
|
def save_game():
|
||||||
if not args.full_screen:
|
if not args.full_screen:
|
||||||
with open(gamesave_path, 'w+') as f:
|
with open(gamesave_path, "w+") as f:
|
||||||
json.dump([worm_locations, worm_head, last_dir, score, size, bonus_location, bonus_points, do_automove, do_help], f)
|
json.dump(
|
||||||
|
[
|
||||||
|
worm_locations,
|
||||||
|
worm_head,
|
||||||
|
last_dir,
|
||||||
|
score,
|
||||||
|
size,
|
||||||
|
bonus_location,
|
||||||
|
bonus_points,
|
||||||
|
do_automove,
|
||||||
|
do_help,
|
||||||
|
],
|
||||||
|
f,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
term = Terminal()
|
term = Terminal()
|
||||||
gamesave_path = os.path.join(os.getenv('HOME'), '.worm.py-gamesave')
|
gamesave_path = os.path.join(os.getenv("HOME"), ".worm.py-gamesave")
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(prog='wormpy')
|
parser = argparse.ArgumentParser(prog="wormpy")
|
||||||
group = parser.add_mutually_exclusive_group()
|
group = parser.add_mutually_exclusive_group()
|
||||||
group.add_argument("--save-game", '-s', help="use 80x24 box and save game on ^C (default)", action='store_true', default=True)
|
group.add_argument(
|
||||||
group.add_argument("--full-screen", '-f', help="use entire screen (disables game saving)", action='store_true')
|
"--save-game",
|
||||||
group.add_argument("--delete-save", help="delete saved game and exit", action='store_true')
|
"-s",
|
||||||
|
help="use 80x24 box and save game on ^C (default)",
|
||||||
|
action="store_true",
|
||||||
|
default=True,
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--full-screen",
|
||||||
|
"-f",
|
||||||
|
help="use entire screen (disables game saving)",
|
||||||
|
action="store_true",
|
||||||
|
)
|
||||||
|
group.add_argument(
|
||||||
|
"--delete-save", help="delete saved game and exit", action="store_true"
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.delete_save:
|
if args.delete_save:
|
||||||
try:
|
try:
|
||||||
shutil.move(gamesave_path, gamesave_path + '-old')
|
shutil.move(gamesave_path, gamesave_path + "-old")
|
||||||
print("Saved game deleted.")
|
print("Saved game deleted.")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print("No saved game found.")
|
print("No saved game found.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("An error occurred.")
|
print("An error occurred.")
|
||||||
print(str(type(e)).split("'")[1] + ': ' + str(e))
|
print(str(type(e)).split("'")[1] + ": " + str(e))
|
||||||
cont = input("Do you want to try to non-recoverably delete the saved game? [Y/n] ")
|
cont = input(
|
||||||
if cont[0] in 'Yy':
|
"Do you want to try to non-recoverably delete the saved game? [Y/n] "
|
||||||
|
)
|
||||||
|
if cont[0] in "Yy":
|
||||||
try:
|
try:
|
||||||
os.unlink(gamesave_path)
|
os.unlink(gamesave_path)
|
||||||
print("Saved game deleted.")
|
print("Saved game deleted.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("An error occurred.")
|
print("An error occurred.")
|
||||||
print(str(type(e)).split("'")[1] + ': ' + str(e))
|
print(str(type(e)).split("'")[1] + ": " + str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
else:
|
else:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -215,12 +266,22 @@ try:
|
||||||
|
|
||||||
with open(gamesave_path) as f:
|
with open(gamesave_path) as f:
|
||||||
save = json.load(f)
|
save = json.load(f)
|
||||||
worm_locations, worm_head, last_dir, score, size, bonus_location, bonus_points, do_automove, do_help = save
|
(
|
||||||
|
worm_locations,
|
||||||
|
worm_head,
|
||||||
|
last_dir,
|
||||||
|
score,
|
||||||
|
size,
|
||||||
|
bonus_location,
|
||||||
|
bonus_points,
|
||||||
|
do_automove,
|
||||||
|
do_help,
|
||||||
|
) = save
|
||||||
except (FileNotFoundError, IsFullScreen):
|
except (FileNotFoundError, IsFullScreen):
|
||||||
size = 7
|
size = 7
|
||||||
worm_locations = [[i+10, worm_y] for i in range(size)]
|
worm_locations = [[i + 10, worm_y] for i in range(size)]
|
||||||
worm_head = [size+10, worm_y]
|
worm_head = [size + 10, worm_y]
|
||||||
last_dir = 'x'
|
last_dir = "x"
|
||||||
score = 0
|
score = 0
|
||||||
new_bonus()
|
new_bonus()
|
||||||
do_automove = True
|
do_automove = True
|
||||||
|
@ -235,12 +296,12 @@ try:
|
||||||
if do_help:
|
if do_help:
|
||||||
for info in (info_1, info_2):
|
for info in (info_1, info_2):
|
||||||
stty.stty(raw=False)
|
stty.stty(raw=False)
|
||||||
print(info, end='')
|
print(info, end="")
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
stty.stty(raw=True)
|
stty.stty(raw=True)
|
||||||
|
|
||||||
k = await_keys('cC')
|
k = await_keys("cC")
|
||||||
if k in '':
|
if k in "":
|
||||||
save_game()
|
save_game()
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
|
@ -260,12 +321,12 @@ except RanIntoSomethingError:
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
stty.stty(raw=False, echo=True)
|
stty.stty(raw=False, echo=True)
|
||||||
print('', end='\r\n')
|
print("", end="\r\n")
|
||||||
if size + 1 >= (height-3) * (width-1):
|
if size + 1 >= (height - 3) * (width - 1):
|
||||||
print('You won!', end='\r\n')
|
print("You won!", end="\r\n")
|
||||||
else:
|
else:
|
||||||
print('Well, you ran into something and the game is over.', end='\r\n')
|
print("Well, you ran into something and the game is over.", end="\r\n")
|
||||||
print(f'Your final score was {score}', end='\r\n')
|
print(f"Your final score was {score}", end="\r\n")
|
||||||
print('', end='\r\n')
|
print("", end="\r\n")
|
||||||
finally:
|
finally:
|
||||||
stty.stty(raw=False, echo=True)
|
stty.stty(raw=False, echo=True)
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
def get_infos(term, do_move, height, width, x_offset):
|
def get_infos(term, do_move, height, width, x_offset):
|
||||||
info_1 = term.clear() + do_move(0, 0) + term.on_red(' Worm') \
|
info_1 = (
|
||||||
+ term.black_on_bright_cyan('.py ') + f''' v1.0: bsdgames worm, ported \
|
term.clear()
|
||||||
|
+ do_move(0, 0)
|
||||||
|
+ term.on_red(" Worm")
|
||||||
|
+ term.black_on_bright_cyan(".py ")
|
||||||
|
+ f""" v1.0: bsdgames worm, ported \
|
||||||
to Python and improved
|
to Python and improved
|
||||||
|
|
||||||
See https://github.com/kj7rrv/wormpy for source code and installation
|
See https://github.com/kj7rrv/wormpy for source code and installation
|
||||||
|
@ -24,15 +28,18 @@ after `{term.bright_red('worm')}{term.bright_cyan('py')}`, as in \
|
||||||
-long worm.
|
-long worm.
|
||||||
|
|
||||||
{term.bright_red('Worm')}{term.bright_cyan('.py')} is released under the MIT \
|
{term.bright_red('Worm')}{term.bright_cyan('.py')} is released under the MIT \
|
||||||
license.'''\
|
license."""
|
||||||
+ '{}Press {} to continue, {} to exit the game...'.format(
|
+ "{}Press {} to continue, {} to exit the game...".format(
|
||||||
do_move(height - 1, 0),
|
do_move(height - 1, 0), term.bold_green("C"), term.bold_red("Ctrl-C")
|
||||||
term.bold_green('C'),
|
)
|
||||||
term.bold_red('Ctrl-C')
|
)
|
||||||
)
|
|
||||||
|
|
||||||
info_2 = term.clear() + do_move(0, 0) + term.on_red(' Worm') \
|
info_2 = (
|
||||||
+ term.black_on_bright_cyan('.py ') + f''' Copyright and License Info
|
term.clear()
|
||||||
|
+ do_move(0, 0)
|
||||||
|
+ term.on_red(" Worm")
|
||||||
|
+ term.black_on_bright_cyan(".py ")
|
||||||
|
+ f""" Copyright and License Info
|
||||||
|
|
||||||
Copyright (c) 2021 Samuel L. Sloniker
|
Copyright (c) 2021 Samuel L. Sloniker
|
||||||
|
|
||||||
|
@ -46,19 +53,21 @@ so, subject to the following conditions:
|
||||||
The above copyright notice and this permission notice shall be included in all
|
The above copyright notice and this permission notice shall be included in all
|
||||||
copies or substantial portions of the Software.
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
''' + term.bold('''THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY \
|
"""
|
||||||
|
+ term.bold(
|
||||||
|
"""THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY \
|
||||||
KIND, EXPRESS OR
|
KIND, EXPRESS OR
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
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
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.''') \
|
SOFTWARE."""
|
||||||
+ '{}Press {} to return to the game, {} to exit...'.format(
|
)
|
||||||
do_move(height - 1, 0),
|
+ "{}Press {} to return to the game, {} to exit...".format(
|
||||||
term.bold_green('C'),
|
do_move(height - 1, 0), term.bold_green("C"), term.bold_red("Ctrl-C")
|
||||||
term.bold_red('Ctrl-C')
|
)
|
||||||
)
|
)
|
||||||
info_1 = info_1.replace('\n', '\n' + term.move_x(x_offset))
|
info_1 = info_1.replace("\n", "\n" + term.move_x(x_offset))
|
||||||
info_2 = info_2.replace('\n', '\n' + term.move_x(x_offset))
|
info_2 = info_2.replace("\n", "\n" + term.move_x(x_offset))
|
||||||
return info_1, info_2
|
return info_1, info_2
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
def stty(raw=None, echo=None):
|
def stty(raw=None, echo=None):
|
||||||
args = ['stty']
|
args = ["stty"]
|
||||||
|
|
||||||
if raw is not None:
|
if raw is not None:
|
||||||
args.append('raw' if raw else '-raw')
|
args.append("raw" if raw else "-raw")
|
||||||
|
|
||||||
if echo is not None:
|
if echo is not None:
|
||||||
args.append('echo' if echo else '-echo')
|
args.append("echo" if echo else "-echo")
|
||||||
|
|
||||||
subprocess.run(args)
|
subprocess.run(args)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user