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

96 lines
2.4 KiB
Python
Raw Normal View History

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):
index = r * field.field_size + c
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] == "\\":
2023-11-13 10:50:09 +01:00
bg = 'red'
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'
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-04 08:46:18 +01:00
active_field.set_ship_size(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
2023-11-06 15:35:22 +01:00
window = Tk()
window.title("Ship Craft!")
2024-02-26 18:34:51 +01:00
window.geometry('940x410')
2023-12-11 20:01:56 +01:00
window.bind_all('<KeyPress>', keypress_handler)
2024-03-04 08:46:18 +01:00
my_field.toggle_ship_direction()
my_field.set_ship_size(4)
my_field.set_ship(1, 1)
my_field.toggle_ship_direction()
my_field.set_ship_size(3)
my_field.set_ship(0, 6)
my_field.set_ship_size(1)
my_field.set_ship(7, 3)
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-03-04 08:46:18 +01:00
window.mainloop()