import json import os from tkinter import filedialog from tkinter import * from ShipField import ShipField from ShipView import ShipView active_view = {} def create_view(window, col_offset=0, row_offset=0): field = ShipField() buttons = [] view = ShipView(field, buttons) for r in range(0, field.field_size): for c in range(0, field.field_size): btn = Button(window, text='', width=5, height=2) btn.grid(column=c + col_offset, row=r + row_offset) btn.bind('', lambda e, x=r, y=c: left_button_click(view, x, y)) btn.bind('', lambda e: right_button_click(view)) btn.bind('', lambda e, x=r, y=c: button_enter(view, x, y)) buttons.append(btn) colorize(view) return view def colorize(view): field = view.ship_field for i in range(len(field.field)): bg = "white" if field.field[i] == "1": bg = 'pink' if field.field[i] == "\\": bg = 'grey' if field.field[i] == "0": bg = 'black' if field.field[i] == "p": bg = 'blue' if field.field[i] == "r": bg = 'red' if "+" in field.field[i]: bg = 'orange' view.buttons[i].configure(bg=bg) refresh_remaining_ships_label(view) def keypress_handler(e): global active_view if e.keysym.isnumeric(): active_view.ship_field.set_ship_size(int(e.keysym)) else: if e.keysym == 'm': active_view.ship_field.toggle_field_mode() def left_button_click(view, row, col): view.ship_field.action(row, col) colorize(view) def right_button_click(view): view.ship_field.toggle_ship_direction() colorize(view) def button_enter(view, row, col): global active_view active_view = view if view == my_view: enemy_view.ship_field.clear_marker() my_view.ship_field.target(row, col) elif view == enemy_view: my_view.ship_field.clear_marker() enemy_view.ship_field.target(row, col) colorize(my_view) colorize(enemy_view) def savebutton_click(view): file_path = filedialog.asksaveasfilename(filetypes=[("JSON files", "*.json")]) if file_path: with open(file_path, 'w') as f: json.dump({'shipField': view.ship_field}, f, default=ShipField.convert_to_json) def loadbutton_click(view): file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")]) if os.path.isfile(file_path): with open(file_path) as lines: view.ship_field.from_json(json.load(lines)["shipField"]) colorize(view) def refresh_remaining_ships_label(view): text = '' for i in range(1, 5): count = view.ship_field.ships.count(i) if count > 0: text += f'{"[]" * i}: {count}, ' view.remaining_ships_text.set(text[:-2]) window = Tk() window.title("Ship Craft!") window.geometry('1020x540') window.bind_all('', keypress_handler) start_column_my_field = 1 start_row_my_field = 1 start_column_enemy_field = start_column_my_field + ShipField.field_size + 1 start_row_enemy_field = start_row_my_field col_vertical_separator = start_column_my_field + ShipField.field_size row_horizontal_separator = start_row_my_field + ShipField.field_size load_button_row = start_row_my_field + ShipField.field_size + 1 my_view = create_view(window, start_column_my_field, start_row_my_field) enemy_view = create_view(window, start_column_enemy_field, start_row_enemy_field) if start_column_my_field > 0: lbl_left_vertical = Label(window, text='', width=5, height=2) lbl_left_vertical.grid(column=start_column_my_field - 1, row=start_row_my_field) lbl_center_vertical = Label(window, text='', width=5, height=2) lbl_center_vertical.grid(column=col_vertical_separator, row=start_row_my_field) if start_row_my_field > 0: lbl_upper_horizontal = Label(window, text='', width=5, height=2) lbl_upper_horizontal.grid(column=start_column_my_field, row=start_row_my_field - 1) lbl_lower_horizontal = Label(window, text='', width=50, height=2, textvariable=my_view.remaining_ships_text) lbl_lower_horizontal.grid(column=start_column_my_field, row=row_horizontal_separator, columnspan=10) lbl_lower_enemy_horizontal = Label(window, text='', width=50, height=2, textvariable=enemy_view.remaining_ships_text) lbl_lower_enemy_horizontal.grid(column=start_column_enemy_field, row=row_horizontal_separator, columnspan=10) savebutton = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(my_view)) savebutton.grid(column=start_column_my_field, row=load_button_row, columnspan=4) loadbutton = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(my_view)) loadbutton.grid(column=start_column_my_field + 6, row=load_button_row, columnspan=4) savebutton_enemy = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(enemy_view)) savebutton_enemy.grid(column=start_column_enemy_field, row=load_button_row, columnspan=4) loadbutton_enemy = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(enemy_view)) loadbutton_enemy.grid(column=start_column_enemy_field + 6, row=load_button_row, columnspan=4) window.mainloop()