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