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

80 lines
2.9 KiB
Python
Raw Normal View History

2023-11-06 19:12:25 +01:00
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()