загружаю дз 0 до 28 минуты ошибка артибута шипвью

This commit is contained in:
eduard ermakov 2024-05-20 20:48:44 +03:00
parent 925230782f
commit 5104b9cc70
9 changed files with 74 additions and 66 deletions

View File

@ -3,5 +3,5 @@
<component name="Black">
<option name="sdkName" value="Python 3.11" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>

View File

@ -5,8 +5,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="b389cdea-22f5-4ba2-8b46-337091984b3c" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/ShipView.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/file_path" beforeDir="false" afterPath="$PROJECT_DIR$/file_path" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/main.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -14,6 +15,13 @@
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../../../.." />
</component>
@ -40,7 +48,6 @@
<component name="RunManager" selected="Python.main">
<configuration name="ShipField" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="Переделка" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
@ -62,7 +69,6 @@
</configuration>
<configuration name="main" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="Переделка" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
@ -112,7 +118,7 @@
<breakpoints>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/main.py</url>
<line>147</line>
<line>139</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
</breakpoints>

View File

@ -0,0 +1,10 @@
from ShipField import ShipField
from tkinter import StringVar
class ShipView:
def __init__(self, ship_field, buttons):
self.ship_field = ship_field
self.buttons = buttons
self.remaining_ship_text = StringVar()

View File

@ -3,31 +3,30 @@ import os
import time
from tkinter import *
from tkinter import filedialog
from ShipView import ShipView
from ShipField import ShipField
my_field = ShipField()
enemy_field = ShipField()
active_view ={}
active_field = my_field
active_text = {}
def draw_field(window, field, col_offset=0, row_offset=0):
def create_view(window, col_offset=0, row_offset=0):
field = ShipField()
buttons = []
for r in range(0, field.field_size):
for c in range(0, field.field_size):
btn = Button(window, text='', width=5, height=2)
btn.grid(column=c + col_offset, row=r + row_offset)
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(buttons, x, y))
btn.bind('<Button-1>', lambda e, x=r, y=c: left_button_click(x, y))
btn.bind('<Button-3>', right_button_click)
btn.bind('<Enter>', lambda e, x=r, y=c: button_enter(buttons, x, y))
buttons.append(btn)
colorize(field, buttons)
return buttons
view = ShipView(field, buttons)
colorize(view)
return view
def colorize(field, buttons):
for i in range(len(buttons)):
def colorize(view):
field = view.ship_field
for i in range(len(field.field)):
bg = "white"
if field[i] == "1":
bg = 'pink'
@ -41,75 +40,71 @@ def colorize(field, buttons):
bg = 'orange'
if "r" in field[i]:
bg = 'red'
buttons[i].configure(bg=bg)
view.buttons[i].configure(bg=bg)
def keypress_handler(e):
global active_field
global active_view
if e.keysym.isnumeric():
active_field.set_ship_size(e.keysym)
active_view.ship_field.set_ship_size(e.keysym)
else:
if e.keysym == 'm':
active_field.toggle_field_mode()
active_view.ship_field.toggle_field_mode()
def left_button_click(buttons, row, col):
global active_field
global active_text
def left_button_click(row, col):
global active_view
active_field.action(row, col)
refresh_remaining_ships_label(active_field, active_text)
colorize(active_field, buttons)
active_view.ship_field.action(row, col)
colorize(active_view)
refresh_remaining_ships_label(active_view)
def right_button_click(unused):
global active_field
active_field.toggle_ship_direction()
global active_view
active_view.ship_field.toggle_ship_direction()
def button_enter(buttons, row, col):
global active_field
global active_text
global active_view
if buttons == my_buttons:
active_field = my_field
active_text = my_remainingShipsText
enemy_field.clear_marker()
my_field.target(row, col)
if buttons == my_view.buttons:
active_view = my_view
enemy_view.ship_field.clear_marker()
my_view.ship_field.target(row, col)
elif buttons == enemy_buttons:
active_field = enemy_field
active_text = enemy_remainingShipsText
enemy_field.clear_marker()
enemy_field.target(row, col)
elif buttons == enemy_view.buttons:
active_view = enemy_view
my_view.ship_field.clear_marker()
enemy_view.ship_field.target(row, col)
colorize(my_field, my_buttons)
colorize(enemy_field, enemy_buttons)
colorize(my_view)
colorize(enemy_view)
def savebutton_click(field):
def savebutton_click(view):
file_path = filedialog.asksaveasfilename(filetypes=[("JSON files", "*.json")])
if file_path:
with open(file_path, "w") as f:
json.dump({"shipField": field}, f, default=ShipField.convert_to_json)
json.dump({"shipField": view.ship_field}, f, default=ShipField.convert_to_json)
def loadbutton_click(field, buttons):
def loadbutton_click(view):
file_path = filedialog.askopenfilename(filetypes=[('JSON files', '*.json')])
if os.path.isfile(file_path):
with open(file_path) as lines:
field.from_json(json.load(lines)['shipField'])
view.ship_field.from_json(json.load(lines)['shipField'])
colorize(field, buttons)
colorize(view)
def refresh_remaining_ships_label(field, stringvar):
def refresh_remaining_ships_label(view):
text = ''
for i in range(1,5):
count = field.ships.count(i)
count = view.ship_field.ships.count(i)
if count > 0:
text += f'{"[]" * i}: {count}, '
stringvar.set(text[:-2])
view.remaining_ships_text.set(text[:-2])
window = Tk()
@ -117,22 +112,19 @@ window.title("Ship Craft!")
window.geometry('1020x540')
window.bind_all('<KeyPress>', keypress_handler)
my_remainingShipsText = StringVar()
enemy_remainingShipsText = StringVar()
start_column_my_field = 1
start_row_my_field = 1
start_column_enemy_field = start_column_my_field + my_field.field_size + 1
start_column_enemy_field = start_column_my_field + 10 + 1
start_row_enemy_field = start_row_my_field
col_vertical_separator = start_column_my_field + my_field.field_size
row_horizontal_separator = start_row_my_field + my_field.field_size
col_vertical_separator = start_column_my_field + 10
row_horizontal_separator = start_row_my_field + 10
load_button_row = start_row_my_field + my_field.field_size + 1
load_button_row = start_row_my_field + 10 + 1
my_buttons = draw_field(window, my_field, start_column_my_field, start_row_my_field)
enemy_buttons = draw_field(window, enemy_field, start_column_enemy_field, start_row_enemy_field)
my_view = create_view(window, start_column_my_field, start_row_my_field)
enemy_view = create_view(window, start_column_enemy_field, start_row_enemy_field)
if start_column_my_field > 0:
lbl_left_vertical = Label(window, text='', width=5, height=2)
@ -145,22 +137,22 @@ if start_row_my_field > 0:
lbl_upper_horizontal = Label(window, text='', width=5, height=2)
lbl_upper_horizontal.grid(column=start_column_my_field, row=start_row_my_field - 1)
lbl_lower_horizontal = Label(window, text='', width=50, height=2, textvariable=my_remainingShipsText)
lbl_lower_horizontal = Label(window, text='', width=50, height=2, textvariable=my_view.remaining_ship_text)
lbl_lower_horizontal.grid(column=start_column_my_field, row=row_horizontal_separator, columnspan=10)
lbl_lower_enemy_horizontal = Label(window, text='', width=50, height=2, textvariable=enemy_remainingShipsText)
lbl_lower_enemy_horizontal = Label(window, text='', width=50, height=2, textvariable=enemy_view.remaining_ship_text)
lbl_lower_enemy_horizontal.grid(column=start_column_enemy_field, row=row_horizontal_separator, columnspan=10)
savebutton = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(my_field))
savebutton = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(my_view))
savebutton.grid(column=start_column_my_field, row=load_button_row, columnspan=4)
loadbutton = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(my_field, my_buttons))
loadbutton = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(my_view))
loadbutton.grid(column=start_column_my_field + 6, row=load_button_row, columnspan=4)
savebutton_enemy = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(enemy_field))
savebutton_enemy = Button(window, text='Save', width=20, height=2, command=lambda: savebutton_click(enemy_view))
savebutton_enemy.grid(column=start_column_enemy_field, row=load_button_row, columnspan=4)
loadbutton_enemy = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(enemy_field, enemy_buttons))
loadbutton_enemy = Button(window, text='Load', width=20, height=2, command=lambda: loadbutton_click(enemy_view))
loadbutton_enemy.grid(column=start_column_enemy_field + 6, row=load_button_row, columnspan=4)
window.mainloop()