2023-11-06 15:35:22 +01:00
|
|
|
from tkinter import *
|
|
|
|
|
|
|
|
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
|
|
|
|
|
|
|
|
|
|
|
my_field = list(empty_field)
|
|
|
|
enemy_field = list(empty_field)
|
|
|
|
|
|
|
|
|
2023-11-06 19:54:00 +01:00
|
|
|
def set_ship(row, col, size, direction):
|
2023-11-06 15:35:22 +01:00
|
|
|
# size: 1, 2, 3, 4
|
|
|
|
# direction: вниз(0), вправо(1)
|
|
|
|
|
2023-11-06 19:54:00 +01:00
|
|
|
field_size = 10
|
|
|
|
|
|
|
|
if row < 0 or row > field_size -1:
|
|
|
|
return
|
|
|
|
|
|
|
|
if col < 0 or col > field_size -1:
|
|
|
|
return
|
|
|
|
|
|
|
|
index = row * field_size *col
|
|
|
|
|
|
|
|
if direction == 0:
|
|
|
|
if field_size - row < size:
|
|
|
|
return
|
|
|
|
for r in range(row, row + size):
|
|
|
|
index = r * field_size + col
|
|
|
|
my_field[index] = '1'
|
|
|
|
|
|
|
|
if direction == 1:
|
|
|
|
if field_size - col < size:
|
|
|
|
return
|
|
|
|
for c in range (col, col + size):
|
|
|
|
index = row * field_size + c
|
|
|
|
my_field[index] = '1'
|
|
|
|
|
2023-11-06 15:35:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def draw_field(window, field):
|
|
|
|
canvas = Canvas(window, width=500, height=500)
|
|
|
|
canvas.pack()
|
|
|
|
|
|
|
|
square_size = 50
|
|
|
|
|
|
|
|
for row in range(10):
|
|
|
|
for col in range(10):
|
|
|
|
if (row + col) % 2 == 0:
|
|
|
|
color = 'white'
|
|
|
|
else:
|
|
|
|
color = 'blue'
|
2023-11-06 19:54:00 +01:00
|
|
|
if my_field[row*10 + col] == '1':
|
|
|
|
color = 'red'
|
2023-11-06 15:35:22 +01:00
|
|
|
x1 = col * square_size
|
|
|
|
y1 = row * square_size
|
|
|
|
x2 = x1 * square_size
|
|
|
|
y2 = y1 * square_size
|
|
|
|
canvas.create_rectangle(x1, y1, x2, y2, fill=color)
|
|
|
|
|
|
|
|
|
|
|
|
window = Tk()
|
|
|
|
window.title("Ship Craft!")
|
|
|
|
window.geometry('800x600')
|
|
|
|
|
2023-11-06 19:54:00 +01:00
|
|
|
set_ship(1, 1, 4, 1)
|
|
|
|
|
2023-11-06 15:35:22 +01:00
|
|
|
draw_field(window, my_field)
|
|
|
|
|
|
|
|
window.mainloop()
|
2023-11-06 19:54:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# set_ship(1, 1, 4, 1)
|
|
|
|
step1_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', '1', '1', '1', '1', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
|
|
|
|
|
|
|
# set_ship(0, 5, 3, 0)
|
|
|
|
step2_field = [' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|