hnc-daniil/HNC/Exercises/Ship_Battle/main.py

157 lines
4.2 KiB
Python
Raw Normal View History

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-02-26 18:34:51 +01:00
2023-11-06 15:35:22 +01:00
2024-02-26 18:34:51 +01:00
def draw_field(window, field, col_offset):
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-02-26 18:34:51 +01:00
btn.grid(column=c + col_offset, row=r)
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'
2024-05-06 18:48:47 +02:00
<<<<<<< HEAD
=======
2024-04-29 19:44:54 +02:00
2024-05-06 18:48:47 +02:00
>>>>>>> d51d6b9fd1009b5d2eec1276597e4b6e34e005b5
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-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
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-02-26 18:34:51 +01:00
if buttons == my_buttons:
2024-03-04 08:46:18 +01:00
active_field = my_field
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
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-05-06 18:47:26 +02:00
def savebutton_click():
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:
json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json)
2024-04-29 19:05:10 +02:00
2024-05-06 18:47:26 +02:00
def loadbutton_click():
2024-04-29 19:33:00 +02:00
global my_field
2024-05-06 18:47:26 +02:00
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
if os.path.isfile(file_path):
with open(file_path) as lines:
my_field.from_json(json.load(lines)["my_field"])
2024-04-29 19:05:10 +02:00
2024-04-29 19:33:00 +02:00
colorize(my_field, my_buttons)
2024-04-29 19:05:10 +02:00
2024-05-06 18:47:26 +02:00
def savebutton_click_enemy():
file_path = filedialog.asksaveasfilename(filetypes=[("JSON files", "*.json")])
if file_path:
with open(file_path, 'w') as f:
json.dump({'enemy_field': enemy_field}, f, default=ShipField.convert_to_json)
def loadbutton_click_enemy():
global enemy_field
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
if os.path.isfile(file_path):
with open(file_path) as lines:
enemy_field.from_json(json.load(lines)["enemy_field"])
colorize(enemy_field, enemy_buttons)
2023-11-06 15:35:22 +01:00
window = Tk()
window.title("Ship Craft!")
2024-04-29 19:05:10 +02:00
window.geometry('940x510')
2023-12-11 20:01:56 +01:00
window.bind_all('<KeyPress>', keypress_handler)
2024-03-04 08:46:18 +01:00
2024-02-26 18:34:51 +01:00
my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 11)
2023-11-06 15:35:22 +01:00
2024-02-26 18:34:51 +01:00
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
2023-11-06 15:35:22 +01:00
2024-05-06 18:47:26 +02:00
savebutton = Button(window, text='Save', width=20, height=2, command=savebutton_click)
2024-04-29 19:05:10 +02:00
savebutton.grid(column=0, row=11, columnspan=4)
2024-05-06 18:47:26 +02:00
loadbutton = Button(window, text='Load', width=20, height=2, command=loadbutton_click)
2024-04-29 19:05:10 +02:00
loadbutton.grid(column=5, row=11, columnspan=4)
2024-05-06 18:48:47 +02:00
<<<<<<< HEAD
2024-05-06 18:47:26 +02:00
savebutton_enemy = Button(window, text='Save_enemy', width=20, height=2, command=savebutton_click_enemy)
savebutton_enemy.grid(column=11, row=11, columnspan=4)
loadbutton_enemy = Button(window, text='Load_enemy', width=20, height=2, command=loadbutton_click_enemy)
loadbutton_enemy.grid(column=16, row=11, columnspan=4)
2024-04-29 19:05:10 +02:00
2024-05-06 18:48:47 +02:00
window.mainloop()
=======
2024-04-29 19:44:54 +02:00
window.mainloop()
2024-05-06 18:48:47 +02:00
>>>>>>> d51d6b9fd1009b5d2eec1276597e4b6e34e005b5