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)