Move repeated code to functions
This commit is contained in:
parent
d5a534a173
commit
a9e6a76f8e
37
worm.py
37
worm.py
|
@ -83,12 +83,7 @@ def move_head(direction, distance):
|
||||||
if worm_head == bonus_location:
|
if worm_head == bonus_location:
|
||||||
size += bonus_points
|
size += bonus_points
|
||||||
score += bonus_points
|
score += bonus_points
|
||||||
bonus_points = random.randint(1, 9)
|
new_bonus()
|
||||||
while worm_head == bonus_location or bonus_location in worm_locations:
|
|
||||||
bonus_location = [
|
|
||||||
random.randint(1, width-3),
|
|
||||||
random.randint(2, height-2),
|
|
||||||
]
|
|
||||||
|
|
||||||
if distance > 1:
|
if distance > 1:
|
||||||
move_head(direction, distance-1)
|
move_head(direction, distance-1)
|
||||||
|
@ -113,16 +108,13 @@ def run(*_):
|
||||||
|
|
||||||
do_automove = True
|
do_automove = True
|
||||||
|
|
||||||
while True:
|
k = await_keys('hjklHJKLABCDwasdiI')
|
||||||
k = sys.stdin.read(1)
|
|
||||||
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 '':
|
||||||
# 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)
|
||||||
elif k in 'iI':
|
elif k in 'iI':
|
||||||
do_help = True
|
do_help = True
|
||||||
|
@ -136,8 +128,22 @@ def await_keys(keys):
|
||||||
return k
|
return k
|
||||||
|
|
||||||
|
|
||||||
|
def new_bonus():
|
||||||
|
global bonus_location, bonus_points
|
||||||
|
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),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
term = Terminal()
|
term = Terminal()
|
||||||
|
|
||||||
|
height = term.height
|
||||||
|
width = term.width
|
||||||
|
|
||||||
info_1 = term.clear() + term.move(0, 0) + term.on_red(' worm') \
|
info_1 = term.clear() + term.move(0, 0) + term.on_red(' worm') \
|
||||||
+ term.bright_cyan_on_red('.py ') + f''' v1.0: bsdgames worm, ported \
|
+ term.bright_cyan_on_red('.py ') + f''' v1.0: bsdgames worm, ported \
|
||||||
to Python and improved
|
to Python and improved
|
||||||
|
@ -199,9 +205,6 @@ SOFTWARE.''') \
|
||||||
term.bold_red('Ctrl-C')
|
term.bold_red('Ctrl-C')
|
||||||
)
|
)
|
||||||
|
|
||||||
height = term.height
|
|
||||||
width = term.width
|
|
||||||
|
|
||||||
last_dir = 'x'
|
last_dir = 'x'
|
||||||
|
|
||||||
if len(sys.argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
|
@ -221,13 +224,7 @@ 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)]
|
||||||
worm_head = [size+10, worm_y]
|
worm_head = [size+10, worm_y]
|
||||||
|
|
||||||
bonus_points = random.randint(1, 9)
|
new_bonus()
|
||||||
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_automove = True
|
||||||
do_help = False
|
do_help = False
|
||||||
|
|
Loading…
Reference in New Issue
Block a user