diff --git a/HNS/Excercises/ShipCraft/Переделка/file_path b/HNS/Excercises/ShipCraft/Переделка/file_path index 4289e18..bdf64fb 100644 --- a/HNS/Excercises/ShipCraft/Переделка/file_path +++ b/HNS/Excercises/ShipCraft/Переделка/file_path @@ -1 +1 @@ -{"my_field": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", "1", "1", "1", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", "1", " ", "", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", "p", "", "", " ", " ", "", "", " ", " "], "ships": [3, 2, 2, 2, 1, 1], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}} \ No newline at end of file +{"my_field": {"field": [" ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", "1", "1", "1", "1", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", "1", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", "", " ", "", "", "", "1", " ", " ", " ", " ", "", " ", " ", " ", " ", "", " ", " ", " ", " ", "", " ", "1", " ", "", "", " ", " ", " ", " ", "", " ", " ", " ", "", " ", " ", " ", " ", " ", " ", "p", "", "", " ", " ", " ", " ", " "], "ships": [3, 2, 2, 2, 1, 1], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}} \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/Переделка/main.py b/HNS/Excercises/ShipCraft/Переделка/main.py index 4fb54d4..74005d2 100644 --- a/HNS/Excercises/ShipCraft/Переделка/main.py +++ b/HNS/Excercises/ShipCraft/Переделка/main.py @@ -56,6 +56,7 @@ def left_button_click(buttons, row, col): global active_field active_field.action(row, col) + refresh_remaining_ships_label() colorize(active_field, buttons) @@ -82,14 +83,14 @@ def button_enter(buttons, row, col): colorize(enemy_field, enemy_buttons) -def savebutton_click(event): +def savebutton_click(): file_path = filedialog.asksaveasfilename(filetypes=[('JSON files', '*.json')]) if file_path: with open("file_path", 'w') as f: json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json) -def loadbutton_click(event): +def loadbutton_click(): global my_field file_path = filedialog.askopenfilename(filetypes=[('JSON files', '*.json')]) @@ -100,6 +101,16 @@ def loadbutton_click(event): colorize(my_field, my_buttons) +def refresh_remaining_ships_label(): + text = '' + for i in range(1,5): + count = my_field.ships.count(i) + if count > 0: + text += f'{"[]" * i}: {count}, ' + + remainingShipsText.set(text[:-2]) + + window = Tk() window.title("Ship Craft!") window.geometry('940x510') @@ -120,12 +131,14 @@ enemy_buttons = draw_field(window, enemy_field, 11) lbl = Label(window, text='', width=5, height=2) lbl.grid(column=10, row=0) -savebutton = Button(window, text='Save', width=20, height=2) -savebutton.bind("", savebutton_click) +savebutton = Button(window, text='Save', width=20, height=2, command=savebutton_click) savebutton.grid(column=0, row=11, columnspan=4) -loadbutton = Button(window, text='Load', width=20, height=2) -loadbutton.bind("", loadbutton_click) +loadbutton = Button(window, text='Load', width=20, height=2, command=loadbutton_click) loadbutton.grid(column=5, row=11, columnspan=4) +remainingShipsText = StringVar() +lbl = Label(window, width=50, height=2, textvariable=remainingShipsText) +lbl.grid(column=11, row=11, columnspan=10) + window.mainloop()