добавлена визуализация расстановки кораблей

This commit is contained in:
ehermakov 2024-05-12 12:43:56 +03:00
parent 932aa4a8f8
commit af7f915ae2
2 changed files with 20 additions and 7 deletions

View File

@ -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"}} {"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"}}

View File

@ -56,6 +56,7 @@ def left_button_click(buttons, row, col):
global active_field global active_field
active_field.action(row, col) active_field.action(row, col)
refresh_remaining_ships_label()
colorize(active_field, buttons) colorize(active_field, buttons)
@ -82,14 +83,14 @@ def button_enter(buttons, row, col):
colorize(enemy_field, enemy_buttons) colorize(enemy_field, enemy_buttons)
def savebutton_click(event): def savebutton_click():
file_path = filedialog.asksaveasfilename(filetypes=[('JSON files', '*.json')]) file_path = filedialog.asksaveasfilename(filetypes=[('JSON files', '*.json')])
if file_path: if file_path:
with open("file_path", 'w') as f: with open("file_path", 'w') as f:
json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json) json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json)
def loadbutton_click(event): def loadbutton_click():
global my_field global my_field
file_path = filedialog.askopenfilename(filetypes=[('JSON files', '*.json')]) file_path = filedialog.askopenfilename(filetypes=[('JSON files', '*.json')])
@ -100,6 +101,16 @@ def loadbutton_click(event):
colorize(my_field, my_buttons) 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 = Tk()
window.title("Ship Craft!") window.title("Ship Craft!")
window.geometry('940x510') window.geometry('940x510')
@ -120,12 +131,14 @@ enemy_buttons = draw_field(window, enemy_field, 11)
lbl = Label(window, text='', width=5, height=2) lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0) lbl.grid(column=10, row=0)
savebutton = Button(window, text='Save', width=20, height=2) savebutton = Button(window, text='Save', width=20, height=2, command=savebutton_click)
savebutton.bind("<Button-1>", savebutton_click)
savebutton.grid(column=0, row=11, columnspan=4) savebutton.grid(column=0, row=11, columnspan=4)
loadbutton = Button(window, text='Load', width=20, height=2) loadbutton = Button(window, text='Load', width=20, height=2, command=loadbutton_click)
loadbutton.bind("<Button-1>", loadbutton_click)
loadbutton.grid(column=5, row=11, columnspan=4) 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() window.mainloop()