From 2b321dcc0a97f9a1d88e4745f67ed018cdbd6d4e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 15 Jan 2024 08:50:56 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B6=D0=B0?= =?UTF-8?q?=D1=8E=20=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D1=81=20=D1=80?= =?UTF-8?q?=D0=B0=D0=B7=D0=B1=D0=BE=D1=80=D0=B0=20=D0=BD=D0=B0=20=D1=83?= =?UTF-8?q?=D1=80=D0=BE=D0=BA=D0=B5=20=D0=B8=D1=82=D0=BE=D0=B3=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HNS/Excercises/ShipCraft/main.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/HNS/Excercises/ShipCraft/main.py b/HNS/Excercises/ShipCraft/main.py index fc7e8f1..2e7d7c6 100644 --- a/HNS/Excercises/ShipCraft/main.py +++ b/HNS/Excercises/ShipCraft/main.py @@ -2,9 +2,12 @@ from tkinter import * from enum import Enum import math -buttons = [] +my_buttons = [] +enemy_buttons = [] field_size = 10 +active_field = 0 + ship_size = 4 ship_direction = 0 field_mode = 0 @@ -70,17 +73,19 @@ def shoot(field, row, col): return ShootResult.UNDEFINED -def draw_field(window, field): +def draw_field(window, field, col_offset): + buttons = [] for r in range(0, field_size): for c in range(0, field_size): index = r * field_size + c btn = Button(window, text='', width=5, height=2) - btn.grid(column=c, row=r) - btn.bind('', lambda e, x=r, y=c: left_button_click(field, x, y)) + btn.grid(column=c + col_offset, row=r) + btn.bind('', lambda e, x=r, y=c: left_button_click(field, buttons, x, y)) btn.bind('', right_button_click) - btn.bind('', lambda e, x=r, y=c: button_enter(field, x, y)) + btn.bind('', lambda e, x=r, y=c: button_enter(field, buttons, x, y)) buttons.append(btn) colorize(field, buttons) + return buttons def colorize(field, buttons): @@ -110,7 +115,7 @@ def keypress_handler(e): field_mode = 0 -def left_button_click(field, row, col): +def left_button_click(field, buttons, row, col): if field_mode == 0: if check_possible(field, row, col): set_ship(field, row, col, ship_size, ship_direction) @@ -128,7 +133,7 @@ def right_button_click(d): ship_direction = 0 -def button_enter(field, row, col): +def button_enter(field, buttons, row, col): if field_mode == 0: for i in range(0, len(field)): if field[i] == "p": @@ -183,14 +188,19 @@ def check_blocked(field, row, col): window = Tk() window.title("Ship Craft!") -window.geometry('450x410') +window.geometry('900x410') window.bind_all('', 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) -draw_field(window, my_field) +my_buttons = draw_field(window, my_field, 0) +enemy_buttons = draw_field(window, enemy_field, 10) +print(len(my_buttons)) +print(len(enemy_buttons)) + window.mainloop() + for r in range(0, field_size): blocked_string = "" ship_string = ""