diff --git a/HNS/Excercises/02.11.2023 Battle Ship/main (4).py b/HNS/Excercises/02.11.2023 Battle Ship/main (4).py new file mode 100644 index 0000000..41c0474 --- /dev/null +++ b/HNS/Excercises/02.11.2023 Battle Ship/main (4).py @@ -0,0 +1,79 @@ +from tkinter import * + +empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + +# set_ship(1, 1, 4, 1) +step1_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', '1', '1', '1', '1', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + +# set_ship(0, 5, 3, 0) +step2_field = [' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] + +my_field = list(empty_field) +enemy_field = list(empty_field) + +buttons = [] + + +def set_ship(field, row, col, size, direction): + # size - 1, 2, 3, 4 + # direction - вниз(0), вправо(1) + if row < 0 or row > size - 1: + return + if col < 0 or col > size - 1: + return 0 + + if direction == 0: + if row + size > size - 1: + return 0 + + for i in range(row, row + size): + index = i * size + col + size[index] = '1' + + +def draw_field(window, field): + btn = Button(window, text=" ") + btn.grid(column=0, row=0) + btn1 = Button(window, text=" ", bg='pink') + btn1.grid(column=1, row=0) + btn2 = Button(window, text=" ") + btn2.grid(column=2, row=0) + + +window = Tk() +window.title("Ship Craft!") +window.geometry('800x400') + +set_ship(my_field, 1, 1, 4, 1) +set_ship(1, 5, 3, 0) + +draw_field(window, my_field) + +window.mainloop()