hnc-artur/HNS/MB/main.py

99 lines
2.4 KiB
Python
Raw Normal View History

2023-11-06 19:09:06 +01:00
from tkinter import *
2024-02-19 12:55:57 +01:00
from ShipField import ShipField
2024-02-15 18:27:17 +01:00
2024-02-19 12:55:57 +01:00
my_field = ShipField()
enemy_field = ShipField()
2023-11-06 19:09:06 +01:00
2024-02-19 12:55:57 +01:00
active_field = my_field
2023-11-06 19:09:06 +01:00
2024-02-15 18:27:17 +01:00
def draw_field(window, field, col_offset):
buttons = []
2024-02-19 12:55:57 +01:00
for r in range(0, field.field_size):
for c in range(0, field.field_size):
2024-02-15 18:27:17 +01:00
btn = Button(window, text='', width=5, height=2)
btn.grid(column=c + col_offset, row=r)
2024-02-19 12:55:57 +01:00
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(buttons, x, y))
2024-02-15 18:27:17 +01:00
btn.bind('<Button-3>', right_button_click)
2024-02-19 12:55:57 +01:00
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(buttons, x, y))
2023-11-06 19:09:06 +01:00
buttons.append(btn)
2024-02-15 18:27:17 +01:00
colorize(field, buttons)
return buttons
2023-11-06 19:09:06 +01:00
2024-02-15 18:27:17 +01:00
def colorize(field, buttons):
2024-02-19 12:55:57 +01:00
for i in range(len(buttons)):
2024-02-15 18:27:17 +01:00
bg = "white"
if field[i] == "1":
bg = 'pink'
if field[i] == "\\":
bg = 'red'
if field[i] == "0":
bg = 'black'
if field[i] == "p":
bg = 'blue'
2024-02-19 12:55:57 +01:00
if "+" in field[i]:
bg = 'orange'
2024-02-15 18:27:17 +01:00
buttons[i].configure(bg=bg)
2023-11-27 19:16:08 +01:00
2024-02-15 18:27:17 +01:00
def keypress_handler(e):
if e.keysym.isnumeric():
2024-02-19 12:55:57 +01:00
active_field.set_ship_size(e.keysym)
2024-02-15 18:27:17 +01:00
else:
if e.keysym == 'm':
2024-02-19 12:55:57 +01:00
active_field.toggle_field_mode()
2023-11-27 19:16:08 +01:00
2024-02-19 12:55:57 +01:00
def left_button_click(buttons, row, col):
global active_field
2023-11-27 19:16:08 +01:00
2024-02-19 12:55:57 +01:00
active_field.action(row, col)
colorize(active_field, buttons)
2023-11-27 19:16:08 +01:00
2024-02-19 12:55:57 +01:00
def right_button_click(unused):
global active_field
2023-11-27 19:16:08 +01:00
2024-02-19 12:55:57 +01:00
active_field.toggle_ship_direction()
2023-11-27 19:16:08 +01:00
2024-02-19 12:55:57 +01:00
def button_enter(buttons, row, col):
2024-02-15 18:27:17 +01:00
global active_field
2023-11-27 19:16:08 +01:00
2024-02-15 18:27:17 +01:00
if buttons == my_buttons:
2024-02-19 12:55:57 +01:00
active_field = my_field
enemy_field.clear_marker()
my_field.target(row, col)
2023-11-27 19:16:08 +01:00
2024-02-19 12:55:57 +01:00
elif buttons == enemy_buttons:
active_field = enemy_field
my_field.clear_marker()
enemy_field.target(row, col)
2023-11-27 19:16:08 +01:00
2024-02-19 12:55:57 +01:00
colorize(my_field, my_buttons)
colorize(enemy_field, enemy_buttons)
2023-11-06 19:09:06 +01:00
window = Tk()
window.title("Ship Craft!")
2024-02-15 18:27:17 +01:00
window.geometry('940x410')
2023-11-27 19:16:08 +01:00
window.bind_all('<KeyPress>', keypress_handler)
2024-02-19 12:55:57 +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-15 18:27:17 +01:00
my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 11)
2023-11-06 19:09:06 +01:00
2024-02-15 18:27:17 +01:00
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
2023-11-06 19:09:06 +01:00
window.mainloop()