83 lines
2.2 KiB
Python
83 lines
2.2 KiB
Python
|
from tkinter import *
|
||
|
|
||
|
field_size = 10
|
||
|
mode = 0
|
||
|
|
||
|
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
||
|
|
||
|
|
||
|
my_field = list(empty_field)
|
||
|
enemy_field = list(empty_field)
|
||
|
|
||
|
buttons = []
|
||
|
|
||
|
|
||
|
def set_ship(field, row, col, size, direction):
|
||
|
if row < 0 or row > field_size - 1:
|
||
|
return
|
||
|
|
||
|
if col < 0 or col > field_size - 1:
|
||
|
return
|
||
|
|
||
|
if direction == 0:
|
||
|
if row + size > field_size:
|
||
|
return
|
||
|
|
||
|
for r in range(row, row + size):
|
||
|
index = r * field_size + col
|
||
|
field[index] = '1'
|
||
|
|
||
|
if direction == 1:
|
||
|
if col + size > field_size - 1:
|
||
|
return
|
||
|
|
||
|
for c in range(col, col + size):
|
||
|
index = row * field_size + c
|
||
|
field[index] = '1'
|
||
|
|
||
|
|
||
|
def draw_field(window, field):
|
||
|
for row in range(0, field_size):
|
||
|
for col in range(0, field_size):
|
||
|
index = row * field_size + col
|
||
|
bg = 'lightgray'
|
||
|
|
||
|
if mode == 1:
|
||
|
btn = Button(window, text="", bg=bg, width=5, height=2, command=lambda i=index: colorize(i, field))
|
||
|
else:
|
||
|
if field[index] == '1':
|
||
|
bg = 'pink'
|
||
|
btn = Button(window, text="", bg=bg, width=5, height=2)
|
||
|
|
||
|
btn.grid(column=col, row=row)
|
||
|
buttons.append(btn)
|
||
|
|
||
|
|
||
|
def colorize(index, field):
|
||
|
bg = 'black'
|
||
|
if field[index] == '1':
|
||
|
bg = 'red'
|
||
|
buttons[index].configure(bg=bg)
|
||
|
|
||
|
|
||
|
window = Tk()
|
||
|
window.title("Ship Craft!")
|
||
|
window.geometry('450x410')
|
||
|
|
||
|
set_ship(my_field, 1, 1, 4, 1)
|
||
|
set_ship(my_field, 0, 6, 3, 0)
|
||
|
set_ship(my_field, 9, 9, 1, 0)
|
||
|
|
||
|
draw_field(window, my_field)
|
||
|
|
||
|
window.mainloop()
|