я учусь коммитить

This commit is contained in:
vitalii Malcov 2024-02-26 19:07:25 +01:00
parent 34b594ac28
commit 71ce427037
3 changed files with 245 additions and 68 deletions

View File

@ -0,0 +1,156 @@
from ShootResult import ShootResult
class ShipField:
def __init__(self):
self.field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
self.field_size = 10
self.field_mode = 0
self.ship_size = 4
self.ship_direction = 0
def __getitem__(self, item):
if item is None:
return None
if type(item) is not int and item.isnumeric():
item = int(item)
if type(item) is int and 0 <= item < len(self.field):
return self.field[item]
return None
def action(self, row,col):
if self.field_mode == 0:
if self.check_possible(row, col):
self.set_ship(row, col)
elif self.field_mode == 1:
self.shoot(row, col)
def target(self, row, col):
if self.field_mode == 0:
self.clear_market()
if self.check_possible(row, col):
if self.ship_direction == 0:
for r in range(row, row + self.ship_size):
self.field[r * self.field_size + col] = 'p'
if self.ship_direction == 1:
for c in range(col, col + self.ship_size):
self.field[row * self.field_size + c] = 'p'
def clear_market(self):
for i in range(0, len(self.field)):
if self.field[i] == 'p':
self.field = ''
def set_ship(self, row, col):
if row < 0 or row > self.field_size:
return
if col < 0 or col > self.field_size:
return
index = row * self.field_size + col
if self.ship_direction == 0:
if self.field_size - row < self.ship_size:
return
for r in range(row, row + self.ship_size):
index = r * self.field_size + col
self.field[index] = "1"
if self.ship_direction == 1:
if self.field_size - col < self.ship_size:
return
for c in range(col, col + self.ship_size):
index = row * self.field_size + c
self.field[index] = "1"
def shoot(self, row, col):
if row < 0 or row > self.field_size - 1:
return ShootResult.UNDEFINED
if col < 0 or col > self.field_size - 1:
return ShootResult.UNDEFINED
index = row * self.field_size + col
if (self.field[index]).strip() == "":
self.field[index] = "0"
return ShootResult.EMPTY
elif (self.field[index]).strip() == "1":
self.field[index] = "\\"
return ShootResult.DAMAGED
else:
return ShootResult.UNDEFINED
def check_possible(self, row, col, ship_direction):
if ship_direction == 0:
if self.field_size - row >= self.ship_size:
for r in range(row, row + self.ship_size):
if not self.check_blocked(self.field, r, col):
return False
return True
if ship_direction == 1:
if self.field_size - col >= self.ship_size:
for c in range(col, col + self.ship_size):
if not self.check_blocked(self.field, row, c):
return False
return True
return False
def check_blocked(self, row, col):
for r in range(row - 1, row + 2):
for c in range(col - 1, col + 2):
if 0 <= r < self.field_size and 0 <= c < self.field_size:
cell = (self.field[r * self.field_size + c]).strip()
if cell != '' and cell != 'p':
return False
return True
def set_ship_size(self, value):
if value is None:
return
if type(value) is str and value.isnumeric():
value = int(value)
if type(value) is int and 1 <= value <= 4:
self.ship_size = value
def set_ship_direction(self, value):
if value is None:
return
if type(value) is str and value.isnumeric():
value = int(value)
if type(value) is int and 0 <= value <= 1:
self.ship_direction = value
def toggle_field_mode(self):
if self.field_mode == 1:
self.field_mode = 0
else:
self.field_mode = 0
def print_field(self):
for r in range(0, self.field_size):
blocked_string = ""
ship_string = ""
for c in range(0, self.field_size):
blocked_string += str(self.check_blocked(self, r, c))[0] + ", "
ship_string += self.field[r * self.field_size + c] + ', '
print(blocked_string[:-2] + ' ' + ship_string[:-2])
print("********************************************************************")

View File

@ -0,0 +1,7 @@
from enum import Enum
class ShootResult(Enum):
EMPTY = "EMPTY"
DAMAGED = "DAMAGED"
KILLED = "KILLED"
UNDEFINED = "UNDEFINED"

View File

@ -1,84 +1,98 @@
from tkinter import * # имортирую модуль
from tkinter import messagebox # сообщение для закрытия нашей игры
import time # импортируем модуль тайм
tk = Tk() # создаю обьект
app_running = True # создаю переменную которая будет отслеживать что наше приложение работает
size_canvas_x = 600 # создаю стороны нашего окна
size_canvas_y = 600
s_x = s_y = 10 # размер игрового поля
step_x = size_canvas_x // s_x # шаг отрисовки наших лини по горизонтали
step_y = size_canvas_y // s_y # тоже самое по вертикили
size_canvas_x = step_x * s_x # коректирую поля челочисленным делением
size_canvas_y = step_y * s_y # коректирую поля челочисленным делением
menu_x = 200 # сделал пополнитнльное меню для кнопок
from tkinter import *
from ShipField import ShipField
def on_closing(): # создаю функцию на момент закрытия
global app_running # передаем значение
if messagebox.askokcancel('Выход из игры',
'Хотите выйти из игры?'): # задаем вопрос на закрытие окна (если ДА тогда окно закрываеться)
app_running = False # тогда наша переменная получает новое булевое значение
tk.destroy() # закрываем приложение
my_field = ShipField()
enemy_field = ShipField()
active_field = my_field
# 1. Определите перечисление (Enum) ShootResult со следующими значениями:
# EMPTY (мимо), DAMAGED (ранен), KILLED (убит), UNDEFINED (действие не определено)
def draw_field(window, field, col_offset):
buttons = []
for r in range(0, field.field_size):
for c in range(0, field.field_size):
index = r * field.field_size + c
btn = Button(window, text='', width=5, height=2)
btn.grid(column=c + col_offset, row=r)
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('<Enter>', lambda e, x=r, y=c: button_enter(field, buttons, x, y))
buttons.append(btn)
colorize(field, buttons)
return buttons
tk.protocol('WM_DELETE_WINDOW',
on_closing) # определяю что я делаю на момент закрытия окна и какую функцию она будет выполнять
tk.title('Игра Морской Бой') # определяю параметры нашего окна
tk.resizable(0, 0) # делаю так чтобы окно нельзя было менять
tk.wm_attributes('-topmost', 1) # создаю атребут чтобы наше окно было по верх всех окон
canvas = Canvas(tk, width=size_canvas_x + menu_x, height=size_canvas_y, bd=0,
highlightthickness=0) # создаю общию картину приложения - задаю ширину и высоту
canvas.create_rectangle(0, 0, size_canvas_x, size_canvas_y,
fill='white') # задаю прямоугольную форму окна и цвет - (белый) но в данном случае это квадрат так как стороны одинаковы!)
canvas.pack() # за пакуем наше оконо для игры
tk.update() # обновляем наше окно
def colorize(field, buttons):
for i in range(len(field.field)):
bg = "white"
if field.field[i] == "1":
bg = 'pink'
if field.field[i] == "\\":
bg = 'red'
if field.field[i] == "0":
bg = 'black'
if field.field[i] == "p":
bg = 'blue'
buttons[i].configure(bg=bg)
def draw_tabel(): # создаю фукцию которая будет рисовать нам поле
for i in range(0, s_x + 1): # создаю цилк который отрисует нашу линию по х
canvas.create_line(step_x * i, 0, step_x * i, size_canvas_y)
for i in range(0, s_y + 1): # содаю цикл который отрисует нашу линию по y
canvas.create_line(0, step_y * i, size_canvas_x, step_y * i)
def keypress_handler(e):
global active_field
if e.keysym.isnumeric():
active_field.set_ship_size(e.keysym)
else:
if e.keysym == 'm':
active_field.toggle_field_mode()
draw_tabel()
def left_button_click(buttons, row, col):
global active_field
active_field.action(row, col)
colorize(active_field, buttons)
def button_show_again():
pass
def right_button_click(d):
global active_field
active_field.toggle_field_direction()
def button_begin_again():
pass
def button_enter(buttons, row, col):
global active_field
if buttons == my_buttons:
active_field = my_field
my_field.target(row, col)
elif buttons == enemy_buttons:
active_field = enemy_field
my_buttons.clear_marker()
enemy_field.target(row, col)
colorize(my_field, my_buttons)
colorize(enemy_field, buttons)
b0 = Button(tk, text='Показать корабли противника', command=button_show_again)
b0.place(x=size_canvas_x + 20, y=30)
window = Tk()
window.title("Ship Craft!")
window.geometry('940x410')
window.bind_all('<KeyPress>', keypress_handler)
set_ship(my_field, 1, 1, 4, 1)
set_ship(my_field,0, 6, 3, 0)
set_ship(my_field, 7, 3, 1, 0)
my_buttons = draw_field(window, my_field, 0)
enemy_buttons = draw_field(window, enemy_field, 11)
print(len(my_buttons))
print(len(enemy_buttons))
b1 = Button(tk, text='Начать игру заново!!!', command=button_begin_again)
b1.place(x=size_canvas_x + 20, y=70)
lbl = Label(window, text='', width=5, height=2)
lbl.grid(column=10, row=0)
def add_to_all(event):
_type = 0 # ЛКМ
if event.num == 3:
_type = 1 # ПКМ
#print(_type)
mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
#print(mouse_x, mouse_y)
ip_x = mouse_x // step_x
ip_y = mouse_y // step_y
print(ip_x, ip_y, '-type', _type)
canvas.bind_all('<Button-1>', add_to_all)# ЛКМ
canvas.bind_all('<Button-3>', add_to_all)# ПКМ
while app_running: # создем цикл для нашей игры
if app_running: # если наша переменная еще запущена
tk.update_idletasks() # тогда мы будем обновлять наш window
tk.update()
time.sleep(0.005)
window.mainloop()