From f4e0db3e7ff02caeb88250c4bfed53692b31765b Mon Sep 17 00:00:00 2001 From: ehermakov Date: Mon, 11 Dec 2023 23:01:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B6=D0=B0?= =?UTF-8?q?=D1=8E=20=D0=BA=D0=BE=D0=B4=20=D1=81=20=D0=B2=D1=8B=D0=B1=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=BC=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D0=B0?= =?UTF-8?q?=20=D0=BA=D0=BE=D1=80=D0=B0=D0=B1=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HNS/Excercises/ShipCraft/123.py | 7 +++++++ HNS/Excercises/ShipCraft/main.py | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 HNS/Excercises/ShipCraft/123.py diff --git a/HNS/Excercises/ShipCraft/123.py b/HNS/Excercises/ShipCraft/123.py new file mode 100644 index 0000000..a7ab792 --- /dev/null +++ b/HNS/Excercises/ShipCraft/123.py @@ -0,0 +1,7 @@ +def keypress_handler(e): + if 1 <= int(e.keysym) <= 4: + ship_size = int(e.keysym) + return ship_size + + +print(keypress_handler(3)) \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/main.py b/HNS/Excercises/ShipCraft/main.py index 11e1db2..f264cb6 100644 --- a/HNS/Excercises/ShipCraft/main.py +++ b/HNS/Excercises/ShipCraft/main.py @@ -97,15 +97,23 @@ def colorize(field, buttons): def keypress_handler(e): - - if e.keysym == 1: - ship_size = 1 + global ship_size + global field_mode + if 1 <= int(e.keysym) <= 4: + ship_size = int(e.keysym) + return ship_size + elif e.keysym == 'm': + if field_mode == 0: + field_mode = 1 + else: + field_mode = 0 + return field_mode def left_button_click(field, row, col): if field_mode == 0: if check_possible(field, row, col): - set_ship(row, col,ship_size, ship_direction) + set_ship(row, col, ship_size, ship_direction) elif field_mode == 1: shoot(field, row, col) colorize(field, buttons) @@ -137,6 +145,7 @@ def button_enter(field, row, col): colorize(field, buttons) + def check_possible(field, row, col): # Функция должна возвращать True, если можно поставить сюда корабль, # в противном случае - False @@ -150,7 +159,6 @@ def check_possible(field, row, col): return False return True - if ship_direction == 1: if field_size - col >= ship_size: for c in range(col, col + ship_size): @@ -176,6 +184,7 @@ def check_blocked(field, row, col): window = Tk() window.title("Ship Craft!") window.geometry('450x410') +window.bind_all('', keypress_handler) set_ship(1, 1, 4, 1) set_ship(0, 6, 3, 0) set_ship(7, 3, 1, 0)