hnc-vitalii/HNS/Excercises/02.11.2023 Battle Ship/main.py

99 lines
2.6 KiB
Python
Raw Normal View History

2024-02-26 19:07:25 +01:00
from tkinter import *
from ShipField import ShipField
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
my_field = ShipField()
enemy_field = ShipField()
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
active_field = my_field
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
# 1. Определите перечисление (Enum) ShootResult со следующими значениями:
# EMPTY (мимо), DAMAGED (ранен), KILLED (убит), UNDEFINED (действие не определено)
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
def draw_field(window, field, col_offset):
buttons = []
for r in range(0, field.field_size):
for c in range(0, field.field_size):
index = r * field.field_size + c
btn = Button(window, text='', width=5, height=2)
btn.grid(column=c + col_offset, row=r)
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(field, buttons, x, y))
btn.bind('<Button-3>', right_button_click)
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, buttons, x, y))
buttons.append(btn)
colorize(field, buttons)
return buttons
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
def colorize(field, buttons):
for i in range(len(field.field)):
bg = "white"
if field.field[i] == "1":
bg = 'pink'
if field.field[i] == "\\":
bg = 'red'
if field.field[i] == "0":
bg = 'black'
if field.field[i] == "p":
bg = 'blue'
buttons[i].configure(bg=bg)
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
def keypress_handler(e):
global active_field
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
if e.keysym.isnumeric():
active_field.set_ship_size(e.keysym)
2023-11-02 06:57:41 +01:00
2024-02-26 19:07:25 +01:00
else:
if e.keysym == 'm':
active_field.toggle_field_mode()
2023-11-02 06:57:41 +01:00
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
def left_button_click(buttons, row, col):
global active_field
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
active_field.action(row, col)
colorize(active_field, buttons)
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
def right_button_click(d):
global active_field
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
active_field.toggle_field_direction()
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
def button_enter(buttons, row, col):
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
global active_field
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
if buttons == my_buttons:
active_field = my_field
my_field.target(row, col)
2023-11-05 18:28:15 +01:00
2024-02-26 19:07:25 +01:00
elif buttons == enemy_buttons:
active_field = enemy_field
my_buttons.clear_marker()
enemy_field.target(row, col)
colorize(my_field, my_buttons)
colorize(enemy_field, buttons)
window = Tk()
window.title("Ship Craft!")
window.geometry('940x410')
window.bind_all('<KeyPress>', keypress_handler)
set_ship(my_field, 1, 1, 4, 1)
set_ship(my_field,0, 6, 3, 0)
set_ship(my_field, 7, 3, 1, 0)
my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 11)
print(len(my_buttons))
print(len(enemy_buttons))
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
window.mainloop()