2024-04-29 19:05:10 +02:00
|
|
|
import json
|
2024-05-06 18:47:26 +02:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
from tkinter import filedialog
|
2023-11-06 15:35:22 +01:00
|
|
|
from tkinter import *
|
2024-03-04 08:46:18 +01:00
|
|
|
from ShipField import ShipField
|
2023-11-13 10:50:09 +01:00
|
|
|
|
2024-03-04 08:46:18 +01:00
|
|
|
my_field = ShipField()
|
|
|
|
enemy_field = ShipField()
|
2024-02-26 18:34:51 +01:00
|
|
|
|
2024-03-04 08:46:18 +01:00
|
|
|
active_field = my_field
|
2024-06-03 13:20:55 +02:00
|
|
|
active_text = {}
|
2024-02-26 18:34:51 +01:00
|
|
|
|
2023-11-06 15:35:22 +01:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
def draw_field(window, field, col_offset=0, row_offset=0):
|
2024-02-26 18:34:51 +01:00
|
|
|
buttons = []
|
2024-03-04 08:46:18 +01:00
|
|
|
for r in range(0, field.field_size):
|
|
|
|
for c in range(0, field.field_size):
|
2023-12-11 17:16:12 +01:00
|
|
|
btn = Button(window, text='', width=5, height=2)
|
2024-06-03 13:20:55 +02:00
|
|
|
btn.grid(column=c + col_offset, row=r + row_offset)
|
2024-03-04 08:46:18 +01:00
|
|
|
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(buttons, x, y))
|
2024-02-26 18:34:51 +01:00
|
|
|
btn.bind('<Button-3>', right_button_click)
|
2024-03-04 08:46:18 +01:00
|
|
|
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(buttons, x, y))
|
2023-11-13 10:50:09 +01:00
|
|
|
buttons.append(btn)
|
2024-02-26 18:34:51 +01:00
|
|
|
colorize(field, buttons)
|
|
|
|
return buttons
|
2023-11-13 10:50:09 +01:00
|
|
|
|
2023-11-20 19:36:57 +01:00
|
|
|
|
2023-11-13 10:50:09 +01:00
|
|
|
def colorize(field, buttons):
|
2024-03-04 08:46:18 +01:00
|
|
|
for i in range(len(field.field)):
|
2023-11-20 19:05:48 +01:00
|
|
|
bg = "white"
|
2024-03-04 08:46:18 +01:00
|
|
|
if field.field[i] == "1":
|
2023-11-13 10:50:09 +01:00
|
|
|
bg = 'pink'
|
2024-03-04 08:46:18 +01:00
|
|
|
if field.field[i] == "\\":
|
2024-03-13 09:51:59 +01:00
|
|
|
bg = 'grey'
|
2024-03-04 08:46:18 +01:00
|
|
|
if field.field[i] == "0":
|
2023-11-13 10:50:09 +01:00
|
|
|
bg = 'black'
|
2024-03-04 08:46:18 +01:00
|
|
|
if field.field[i] == "p":
|
2023-12-11 17:16:12 +01:00
|
|
|
bg = 'blue'
|
2024-05-06 18:47:26 +02:00
|
|
|
if field.field[i] == "r":
|
|
|
|
bg = 'red'
|
2024-03-13 09:51:59 +01:00
|
|
|
if "+" in field.field[i]:
|
|
|
|
bg = 'orange'
|
2023-11-20 19:05:48 +01:00
|
|
|
buttons[i].configure(bg=bg)
|
|
|
|
|
|
|
|
|
2024-02-26 18:34:51 +01:00
|
|
|
def keypress_handler(e):
|
2024-03-04 08:46:18 +01:00
|
|
|
global active_field
|
2024-02-26 18:34:51 +01:00
|
|
|
if e.keysym.isnumeric():
|
2024-03-12 09:51:04 +01:00
|
|
|
active_field.set_ship_size(int(e.keysym))
|
2024-02-26 18:34:51 +01:00
|
|
|
else:
|
|
|
|
if e.keysym == 'm':
|
2024-03-04 08:46:18 +01:00
|
|
|
active_field.toggle_field_mode()
|
2024-02-26 18:34:51 +01:00
|
|
|
|
|
|
|
|
2024-03-04 08:46:18 +01:00
|
|
|
def left_button_click(buttons, row, col):
|
|
|
|
global active_field
|
2024-06-03 13:20:55 +02:00
|
|
|
global active_text
|
2024-02-26 18:34:51 +01:00
|
|
|
|
2024-03-04 08:46:18 +01:00
|
|
|
active_field.action(row, col)
|
|
|
|
colorize(active_field, buttons)
|
2024-02-26 18:34:51 +01:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
refresh_remaining_ships_label(active_field, active_text)
|
|
|
|
|
2024-02-26 18:34:51 +01:00
|
|
|
|
|
|
|
def right_button_click(d):
|
|
|
|
global active_field
|
2024-03-04 08:46:18 +01:00
|
|
|
active_field.toggle_ship_direction()
|
2024-02-26 18:34:51 +01:00
|
|
|
|
|
|
|
|
2024-03-04 08:46:18 +01:00
|
|
|
def button_enter(buttons, row, col):
|
|
|
|
global active_field
|
2024-06-03 13:20:55 +02:00
|
|
|
global active_text
|
2024-02-26 18:34:51 +01:00
|
|
|
|
|
|
|
if buttons == my_buttons:
|
2024-03-04 08:46:18 +01:00
|
|
|
active_field = my_field
|
2024-06-03 13:20:55 +02:00
|
|
|
active_text = my_remainingShipsText
|
2024-03-04 08:46:18 +01:00
|
|
|
enemy_field.clear_marker()
|
|
|
|
my_field.target(row, col)
|
2024-02-26 18:34:51 +01:00
|
|
|
|
2024-03-04 08:46:18 +01:00
|
|
|
elif buttons == enemy_buttons:
|
|
|
|
active_field = enemy_field
|
2024-06-03 13:20:55 +02:00
|
|
|
active_text = enemy_remainingShipsText
|
2024-03-04 08:46:18 +01:00
|
|
|
my_field.clear_marker()
|
|
|
|
enemy_field.target(row, col)
|
2023-11-20 19:36:57 +01:00
|
|
|
|
2024-03-04 08:46:18 +01:00
|
|
|
colorize(my_field, my_buttons)
|
|
|
|
colorize(enemy_field, enemy_buttons)
|
2023-11-20 19:36:57 +01:00
|
|
|
|
2024-04-29 19:05:10 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
def savebutton_click(field):
|
2024-05-06 18:47:26 +02:00
|
|
|
file_path = filedialog.asksaveasfilename(filetypes=[("JSON files", "*.json")])
|
2024-04-29 19:05:10 +02:00
|
|
|
|
2024-05-06 18:47:26 +02:00
|
|
|
if file_path:
|
|
|
|
with open(file_path, 'w') as f:
|
2024-06-03 13:20:55 +02:00
|
|
|
json.dump({'shipField': field}, f, default=ShipField.convert_to_json)
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-04-29 19:33:00 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
def loadbutton_click(field, buttons):
|
|
|
|
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
|
2024-05-06 18:47:26 +02:00
|
|
|
|
|
|
|
if os.path.isfile(file_path):
|
|
|
|
with open(file_path) as lines:
|
2024-06-03 13:20:55 +02:00
|
|
|
field.from_json(json.load(lines)["shipField"])
|
2024-04-29 19:05:10 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
colorize(field, buttons)
|
2024-04-29 19:05:10 +02:00
|
|
|
|
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
def refresh_remaining_ships_label(field, stringvar):
|
|
|
|
text = ''
|
|
|
|
for i in range(1, 5):
|
|
|
|
count = field.ships.count(i)
|
|
|
|
if count > 0:
|
|
|
|
text += f'{"[]" * i}: {count}, '
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
stringvar.set(text[:-2])
|
|
|
|
|
|
|
|
|
|
|
|
window = Tk()
|
|
|
|
window.title("Ship Craft!")
|
|
|
|
window.geometry('1020x540')
|
|
|
|
window.bind_all('<KeyPress>', keypress_handler)
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
my_remainingShipsText = StringVar()
|
|
|
|
enemy_remainingShipsText = StringVar()
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
start_column_my_field = 1
|
|
|
|
start_row_my_field = 1
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
start_column_enemy_field = start_column_my_field + my_field.field_size + 1
|
|
|
|
start_row_enemy_field = start_row_my_field
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
col_vertical_separator = start_column_my_field + my_field.field_size
|
|
|
|
row_horizontal_separator = start_row_my_field + my_field.field_size
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
load_button_row = start_row_my_field + my_field.field_size + 1
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
my_buttons = draw_field(window, my_field, start_column_my_field, start_row_my_field)
|
|
|
|
enemy_buttons = draw_field(window, enemy_field, start_column_enemy_field, start_row_enemy_field)
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
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)
|
2024-03-04 08:46:18 +01:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
lbl_lower_horizontal = Label(window, text='', width=50, height=2, textvariable=my_remainingShipsText)
|
|
|
|
lbl_lower_horizontal.grid(column=start_column_my_field, row=row_horizontal_separator, columnspan=10)
|
2023-11-06 15:35:22 +01:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
lbl_lower_enemy_horizontal = Label(window, text='', width=50, height=2, textvariable=enemy_remainingShipsText)
|
|
|
|
lbl_lower_enemy_horizontal.grid(column=start_column_enemy_field, row=row_horizontal_separator, columnspan=10)
|
2023-11-06 15:35:22 +01:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
savebutton = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(my_field))
|
|
|
|
savebutton.grid(column=start_column_my_field, row=load_button_row, columnspan=4)
|
2024-04-29 19:05:10 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
loadbutton = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(my_field, my_buttons))
|
|
|
|
loadbutton.grid(column=start_column_my_field + 6, row=load_button_row, columnspan=4)
|
2024-04-29 19:05:10 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
savebutton_enemy = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(enemy_field))
|
|
|
|
savebutton_enemy.grid(column=start_column_enemy_field, row=load_button_row, columnspan=4)
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-06-03 13:20:55 +02:00
|
|
|
loadbutton_enemy = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(enemy_field, enemy_buttons))
|
|
|
|
loadbutton_enemy.grid(column=start_column_enemy_field + 6, row=load_button_row, columnspan=4)
|
2024-05-06 18:47:26 +02:00
|
|
|
|
2024-04-29 19:05:10 +02:00
|
|
|
|
2024-05-06 18:48:47 +02:00
|
|
|
window.mainloop()
|