hnc-eduard/HNS/Excercises/ShipCraft/main.py

112 lines
3.3 KiB
Python
Raw Normal View History

2023-11-12 19:09:32 +01:00
from tkinter import *
from enum import Enum
buttons = []
field_size = 10
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
my_field = list(empty_field)
enemy_field = list(empty_field)
# 1. Определите перечисление (Enum) ShootResult со следующими значениями:
# EMPTY (мимо), DAMAGED (ранен), KILLED (убит), UNDEFINED (действие не определено)
class ShootResult(Enum):
EMPTY = "EMPTY"
DAMAGED = "DAMAGED"
KILLED = "KILLED"
UNDEFINED = "UNDEFINED"
def set_ship(row, col, size, direction):
if row < 0 or row > field_size - 1:
return
if col < 0 or col > field_size - 1:
return
index = row * field_size + col
if direction == 0:
if field_size - row < size:
return
for r in range(row, row + size):
index = r * field_size + col
my_field[index] = '1'
if direction == 1:
if field_size - col < size:
return
for c in range(col, col + size):
index = row * field_size + c
my_field[index] = "1"
def shoot(field, row, col):
if row < 0 or row > field_size - 1:
return ShootResult.UNDEFINED
2023-11-12 19:09:32 +01:00
if col < 0 or col > field_size - 1:
return ShootResult.UNDEFINED
2023-11-12 19:09:32 +01:00
index = row * field_size + col
if (field[index]).strip() == "":
field[index] = '0'
2023-11-12 19:09:32 +01:00
return ShootResult.EMPTY
elif (field[index]).strip() == "1":
2023-11-12 19:09:32 +01:00
field[index] = "\\"
return ShootResult.DAMAGED
else:
return ShootResult.UNDEFINED
2023-11-12 19:09:32 +01:00
def draw_field(window, field):
for r in range(0, field_size):
for c in range(0, field_size):
index = r * field_size + c
bg = 'white'
2023-11-12 19:09:32 +01:00
if field[index] == '1':
bg = 'pink'
btn = Button(window, text='', bg=bg, width=5, height=2)
btn.grid(column=c, row=r)
btn.bind('<Button-1>', lambda e, x=r, y=c: shoot(field, x, y))
2023-11-12 19:09:32 +01:00
buttons.append(btn)
def colorize(field, buttons):
for i in range(len(field)):
bg = "white"
if field[i] == '1':
2023-11-12 19:09:32 +01:00
bg = 'pink'
if field[i] == '\\':
2023-11-12 19:09:32 +01:00
bg = 'red'
if field[i] == '0':
2023-11-12 19:09:32 +01:00
bg = 'black'
buttons[i].configure(bg=bg)
def button_click(field, row, col):
shoot(field, row, col)
colorize(field, buttons)
2023-11-12 19:09:32 +01:00
window = Tk()
window.title("Ship Craft!")
window.geometry('450x410')
set_ship(1, 1, 4, 1)
set_ship(0, 6, 3, 0)
set_ship(9, 9, 1, 0)
set_ship(0, 0, 1, 0)
set_ship(9, 0, 1, 0)
set_ship(9, 2, 4, 1)
draw_field(window, my_field)
colorize(my_field, buttons)
2023-11-12 19:09:32 +01:00
window.mainloop()