загружаю правки с разбора на уроке итоги

This commit is contained in:
unknown 2024-01-15 08:50:56 +03:00
parent cf5a947782
commit 2b321dcc0a
1 changed files with 19 additions and 9 deletions

View File

@ -2,9 +2,12 @@ from tkinter import *
from enum import Enum from enum import Enum
import math import math
buttons = [] my_buttons = []
enemy_buttons = []
field_size = 10 field_size = 10
active_field = 0
ship_size = 4 ship_size = 4
ship_direction = 0 ship_direction = 0
field_mode = 0 field_mode = 0
@ -70,17 +73,19 @@ def shoot(field, row, col):
return ShootResult.UNDEFINED return ShootResult.UNDEFINED
def draw_field(window, field): def draw_field(window, field, col_offset):
buttons = []
for r in range(0, field_size): for r in range(0, field_size):
for c in range(0, field_size): for c in range(0, field_size):
index = r * field_size + c index = r * field_size + c
btn = Button(window, text='', width=5, height=2) btn = Button(window, text='', width=5, height=2)
btn.grid(column=c, row=r) btn.grid(column=c + col_offset, row=r)
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(field, x, y)) btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(field, buttons, x, y))
btn.bind('<Button-3>', right_button_click) btn.bind('<Button-3>', right_button_click)
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, x, y)) btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(field, buttons, x, y))
buttons.append(btn) buttons.append(btn)
colorize(field, buttons) colorize(field, buttons)
return buttons
def colorize(field, buttons): def colorize(field, buttons):
@ -110,7 +115,7 @@ def keypress_handler(e):
field_mode = 0 field_mode = 0
def left_button_click(field, row, col): def left_button_click(field, buttons, row, col):
if field_mode == 0: if field_mode == 0:
if check_possible(field, row, col): if check_possible(field, row, col):
set_ship(field, row, col, ship_size, ship_direction) set_ship(field, row, col, ship_size, ship_direction)
@ -128,7 +133,7 @@ def right_button_click(d):
ship_direction = 0 ship_direction = 0
def button_enter(field, row, col): def button_enter(field, buttons, row, col):
if field_mode == 0: if field_mode == 0:
for i in range(0, len(field)): for i in range(0, len(field)):
if field[i] == "p": if field[i] == "p":
@ -183,14 +188,19 @@ def check_blocked(field, row, col):
window = Tk() window = Tk()
window.title("Ship Craft!") window.title("Ship Craft!")
window.geometry('450x410') window.geometry('900x410')
window.bind_all('<KeyPress>', keypress_handler) window.bind_all('<KeyPress>', keypress_handler)
set_ship(my_field, 1, 1, 4, 1) set_ship(my_field, 1, 1, 4, 1)
set_ship(my_field,0, 6, 3, 0) set_ship(my_field,0, 6, 3, 0)
set_ship(my_field, 7, 3, 1, 0) set_ship(my_field, 7, 3, 1, 0)
draw_field(window, my_field) my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 10)
print(len(my_buttons))
print(len(enemy_buttons))
window.mainloop() window.mainloop()
for r in range(0, field_size): for r in range(0, field_size):
blocked_string = "" blocked_string = ""
ship_string = "" ship_string = ""