From f20203bdd9f503ae3e31e1d0e68cb562a2ebcaf5 Mon Sep 17 00:00:00 2001 From: ehermakov Date: Mon, 5 Feb 2024 20:17:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=90=D0=93=D0=A0=D0=A3=D0=96=D0=90?= =?UTF-8?q?=D0=AE=20=D0=94=D0=97=20=D0=A0=D0=90=D0=97=D0=9D=D0=AB=D0=95=20?= =?UTF-8?q?=D0=9F=D0=9E=D0=9B=D0=AF=20=D0=98=20=D0=9F=D0=A0=D0=9E=D0=91?= =?UTF-8?q?=D0=95=D0=9B=20=D0=9C=D0=95=D0=96=D0=94=D0=A3=20=D0=9F=D0=9E?= =?UTF-8?q?=D0=9B=D0=AF=D0=9C=D0=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HNS/Excercises/ShipCraft/.idea/.name | 1 + HNS/Excercises/ShipCraft/123.py | 22 +++-- HNS/Excercises/ShipCraft/main.py | 116 +++++++++++++++++++++++---- 3 files changed, 119 insertions(+), 20 deletions(-) create mode 100644 HNS/Excercises/ShipCraft/.idea/.name diff --git a/HNS/Excercises/ShipCraft/.idea/.name b/HNS/Excercises/ShipCraft/.idea/.name new file mode 100644 index 0000000..11a5d8e --- /dev/null +++ b/HNS/Excercises/ShipCraft/.idea/.name @@ -0,0 +1 @@ +main.py \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/123.py b/HNS/Excercises/ShipCraft/123.py index a7ab792..5094f20 100644 --- a/HNS/Excercises/ShipCraft/123.py +++ b/HNS/Excercises/ShipCraft/123.py @@ -1,7 +1,19 @@ -def keypress_handler(e): - if 1 <= int(e.keysym) <= 4: - ship_size = int(e.keysym) - return ship_size +import tkinter as tk +root = tk.Tk() -print(keypress_handler(3)) \ No newline at end of file +l1 = tk.Label(root, text='Col:8 Row:1', bg='red') +l1.grid(column=8, row=1) + +l2 = tk.Label(root, text='Col:1 Row:8', bg='red') +l2.grid(column=1, row=8) + +# add empty label in row 0 and column 0 +l0 = tk.Label(root, text=' \n ', bg='green') +l0.grid(column=0, row=0) + +# add empty label in row 9 and column 9 +l9 = tk.Label(root, text=' ', bg='green') +l9.grid(column=9, row=9) + +root.mainloop() \ No newline at end of file diff --git a/HNS/Excercises/ShipCraft/main.py b/HNS/Excercises/ShipCraft/main.py index 2e7d7c6..9280732 100644 --- a/HNS/Excercises/ShipCraft/main.py +++ b/HNS/Excercises/ShipCraft/main.py @@ -1,6 +1,5 @@ from tkinter import * from enum import Enum -import math my_buttons = [] enemy_buttons = [] @@ -8,9 +7,13 @@ enemy_buttons = [] field_size = 10 active_field = 0 -ship_size = 4 -ship_direction = 0 -field_mode = 0 +ship_size_left = 4 +ship_direction_left = 0 +field_mode_left = 0 + +ship_size_right = 4 +ship_direction_right = 0 +field_mode_right = 0 empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', @@ -103,43 +106,123 @@ def colorize(field, buttons): def keypress_handler(e): - global ship_size - global field_mode + global ship_size_left + global field_mode_left + + global ship_size_right + global field_mode_right + if e.keysym.isnumeric(): - ship_size = int(e.keysym) + if active_field == 0: + ship_size_left = int(e.keysym) + else: + ship_size_right = int(e.keysym) else: if e.keysym == 'm': - if field_mode == 0: - field_mode = 1 + if active_field == 0: + field_mode = field_mode_left else: - field_mode = 0 + field_mode = field_mode_right + if field_mode == 0: + field_mode = 1 + else: + field_mode = 0 + + if active_field == 0: + field_mode_left = field_mode + else: + field_mode_right = field_mode def left_button_click(field, buttons, row, col): + if active_field == 0: + field_mode = field_mode_left + else: + field_mode = field_mode_right + if field_mode == 0: if check_possible(field, row, col): - set_ship(field, row, col, ship_size, ship_direction) + if active_field == 0: + set_ship(field, row, col, ship_size_left, ship_direction_left) + else: + set_ship(field, row, col, ship_size_right, ship_direction_right) elif field_mode == 1: shoot(field, row, col) colorize(field, buttons) def right_button_click(d): - global ship_direction + global ship_direction_left + global ship_direction_right + + global field_mode_left + global field_mode_right + + if active_field == 0: + field_mode = field_mode_left + ship_direction = ship_direction_left + else: + field_mode = field_mode_right + ship_direction = ship_direction_right + + if field_mode == 0: if ship_direction == 0: ship_direction = 1 else: ship_direction = 0 + if active_field == 0: + ship_direction_left = ship_direction + else: + ship_direction_right = ship_direction def button_enter(field, buttons, row, col): + global active_field + global ship_direction_left + global ship_direction_right + + global ship_size_left + global ship_size_right + + global field_mode_left + global field_mode_right + + + if buttons == my_buttons: + active_field = 0 + elif buttons == enemy_buttons: + active_field = 1 + + if active_field == 0: + field_mode = field_mode_left + ship_direction = ship_direction_left + ship_size = ship_size_left + other_field = enemy_field + other_buttons = enemy_buttons + else: + field_mode = field_mode_right + ship_direction = ship_direction_right + ship_size = ship_size_right + other_field = my_field + other_buttons = my_buttons + + + for i in range(0, len(other_field)): + if other_field[i] == "p": + other_field[i] = '' + + colorize(other_field, other_buttons) + + if field_mode == 0: for i in range(0, len(field)): if field[i] == "p": field[i] = '' - if check_possible(field, row, col): + + + if check_possible(field, row, col,ship_size, ship_direction): if ship_direction == 0: for r in range(row, row + ship_size): field[r * field_size + col] = "p" @@ -151,7 +234,7 @@ def button_enter(field, buttons, row, col): colorize(field, buttons) -def check_possible(field, row, col): +def check_possible(field, row, col, ship_size, ship_direction): # Функция должна возвращать True, если можно поставить сюда корабль, # в противном случае - False @@ -194,10 +277,13 @@ 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, 10) +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()