All changes from last lesson
This commit is contained in:
parent
20b930520f
commit
75c78cc9a5
|
@ -1,6 +1,6 @@
|
|||
from enum import Enum
|
||||
|
||||
class ShipDirection(Enum):
|
||||
VERTICAL = 0
|
||||
HORIZONTAL = 1
|
||||
UNKNOWN = -1
|
||||
VERTICAL = "VERTICAL"
|
||||
HORIZONTAL = "HORIZONTAL"
|
||||
UNKNOWN = "UNKNOWN"
|
|
@ -1,3 +1,4 @@
|
|||
import copy
|
||||
from ShootResult import ShootResult
|
||||
from ShipMode import ShipMode
|
||||
from ShipDirection import ShipDirection
|
||||
|
@ -242,4 +243,13 @@ class ShipField:
|
|||
blocked_string += str(self.check_blocked(r, c))[0] + ", "
|
||||
ship_string += self.field[r * self.field_size + c] + ', '
|
||||
print(blocked_string[:-2] + ' ' + ship_string[:-2])
|
||||
print("********************************************************************")
|
||||
print("********************************************************************")
|
||||
|
||||
|
||||
@staticmethod
|
||||
def convert_to_json(obj):
|
||||
if isinstance(obj, ShipField):
|
||||
result = copy.copy(obj.__dict__)
|
||||
result['field_mode'] = obj.field_mode.value
|
||||
result['ship_direction'] = obj.ship_direction.value
|
||||
return result
|
|
@ -1,5 +1,5 @@
|
|||
from enum import Enum
|
||||
|
||||
class ShipMode(Enum):
|
||||
PUT = 0
|
||||
SHOOT = 1
|
||||
PUT = "PUT"
|
||||
SHOOT = "SHOOT"
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,28 @@
|
|||
import os
|
||||
|
||||
|
||||
def list_levels():
|
||||
files = os.listdir(path='.')
|
||||
result = []
|
||||
for file in files:
|
||||
if file.lower().endswith('.txt'):
|
||||
result.append(file)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def load(file):
|
||||
# 1. Загрузить весь файл в строку
|
||||
f = open(file, 'r')
|
||||
s = f.read()
|
||||
|
||||
# 2. Заменить все пробелы и переводы строки на "ничего"
|
||||
s = s.replace(' ', '')
|
||||
s = s.replace('\n', '')
|
||||
|
||||
# 3. Из полученной строки создать массив, используя разделитель ","
|
||||
a = s.split(',')
|
||||
|
||||
# a - массив чисел из а
|
||||
f.close()
|
||||
return a
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"my_field":{
|
||||
"field": [],
|
||||
"ships": [],
|
||||
"field_mode": "PUT"
|
||||
}
|
||||
}
|
|
@ -1,3 +1,4 @@
|
|||
import json
|
||||
from tkinter import *
|
||||
from ShipField import ShipField
|
||||
|
||||
|
@ -32,10 +33,10 @@ def colorize(field, buttons):
|
|||
bg = 'black'
|
||||
if field.field[i] == "p":
|
||||
bg = 'blue'
|
||||
if field.field[i] == "r":
|
||||
bg = 'red'
|
||||
if "+" in field.field[i]:
|
||||
bg = 'orange'
|
||||
if "r" in field[i]:
|
||||
bg = 'red'
|
||||
buttons[i].configure(bg=bg)
|
||||
|
||||
|
||||
|
@ -76,24 +77,41 @@ def button_enter(buttons, row, col):
|
|||
colorize(my_field, my_buttons)
|
||||
colorize(enemy_field, enemy_buttons)
|
||||
|
||||
|
||||
def savebutton_click(event):
|
||||
with open('test.json', 'w') as f:
|
||||
json.dump({'my_field': my_field}, f, default=ShipField.convert_to_json)
|
||||
|
||||
|
||||
def loadbutton_click(event):
|
||||
data_list = []
|
||||
with open('test.json') as lines:
|
||||
data = json.load(lines)
|
||||
for i in data['shapes']:
|
||||
sh1 = create_shape(i)
|
||||
data_list.append(sh1)
|
||||
|
||||
return data_list
|
||||
|
||||
|
||||
window = Tk()
|
||||
window.title("Ship Craft!")
|
||||
window.geometry('940x410')
|
||||
window.geometry('940x510')
|
||||
window.bind_all('<KeyPress>', keypress_handler)
|
||||
|
||||
my_field.toggle_ship_direction()
|
||||
my_field.set_ship_size(4)
|
||||
my_field.set_ship(1, 1)
|
||||
my_field.toggle_ship_direction()
|
||||
my_field.set_ship_size(3)
|
||||
my_field.set_ship(0, 6)
|
||||
my_field.set_ship_size(1)
|
||||
my_field.set_ship(7, 3)
|
||||
|
||||
my_buttons = draw_field(window, my_field, 0)
|
||||
enemy_buttons = draw_field(window, enemy_field, 11)
|
||||
|
||||
lbl = Label(window, text='', width=5, height=2)
|
||||
lbl.grid(column=10, row=0)
|
||||
|
||||
savebutton = Button(window, text='Save', width=20, height=2)
|
||||
savebutton.bind('<Button-1>', savebutton_click)
|
||||
savebutton.grid(column=0, row=11, columnspan=4)
|
||||
|
||||
loadbutton = Button(window, text='Load', width=20, height=2)
|
||||
loadbutton.bind('<Button-1>', loadbutton_click)
|
||||
loadbutton.grid(column=5, row=11, columnspan=4)
|
||||
|
||||
|
||||
window.mainloop()
|
|
@ -0,0 +1 @@
|
|||
{"shapes": {"field": ["1", "1", "1", "1", " ", " ", " ", " ", "", "1", "", "", " ", " ", " ", "1", " ", "", "", "1", "", "", "", "", "", "", "", "1", "", "", "1", "", "", "", "", "1", "", "", "", "", "1", "", "", "1", "", "", "", "1", " ", " ", "1", "", "", "1", " ", " ", " ", "", "", "", " ", "", "", "", " ", " ", "", "", "", "", " ", "1", "", " ", " ", " ", " ", " ", "", "", " ", "1", "", " ", " ", " ", " ", " ", " ", " ", " ", "1", "", " ", " ", " ", "r", "", "1", "1"], "ships": [], "field_size": 10, "field_mode": "PUT", "ship_size": 1, "ship_direction": "VERTICAL"}}
|
Loading…
Reference in New Issue