загружаю код с выбором размера корабля

This commit is contained in:
ehermakov 2023-12-11 23:01:55 +03:00
parent 2d8b895dc6
commit f4e0db3e7f
2 changed files with 21 additions and 5 deletions

View File

@ -0,0 +1,7 @@
def keypress_handler(e):
if 1 <= int(e.keysym) <= 4:
ship_size = int(e.keysym)
return ship_size
print(keypress_handler(3))

View File

@ -97,15 +97,23 @@ def colorize(field, buttons):
def keypress_handler(e):
if e.keysym == 1:
ship_size = 1
global ship_size
global field_mode
if 1 <= int(e.keysym) <= 4:
ship_size = int(e.keysym)
return ship_size
elif e.keysym == 'm':
if field_mode == 0:
field_mode = 1
else:
field_mode = 0
return field_mode
def left_button_click(field, row, col):
if field_mode == 0:
if check_possible(field, row, col):
set_ship(row, col,ship_size, ship_direction)
set_ship(row, col, ship_size, ship_direction)
elif field_mode == 1:
shoot(field, row, col)
colorize(field, buttons)
@ -137,6 +145,7 @@ def button_enter(field, row, col):
colorize(field, buttons)
def check_possible(field, row, col):
# Функция должна возвращать True, если можно поставить сюда корабль,
# в противном случае - False
@ -150,7 +159,6 @@ def check_possible(field, row, col):
return False
return True
if ship_direction == 1:
if field_size - col >= ship_size:
for c in range(col, col + ship_size):
@ -176,6 +184,7 @@ def check_blocked(field, row, col):
window = Tk()
window.title("Ship Craft!")
window.geometry('450x410')
window.bind_all('<KeyPress>', keypress_handler)
set_ship(1, 1, 4, 1)
set_ship(0, 6, 3, 0)
set_ship(7, 3, 1, 0)