From 34b594ac288c090d0c305fe8577d0f4668eb88c7 Mon Sep 17 00:00:00 2001 From: vitalii Malcov Date: Mon, 19 Feb 2024 19:51:32 +0100 Subject: [PATCH] Merge remote-tracking branch 'origin/main' --- .idea/.name | 2 +- .../02.11.2023 Battle Ship/main (4).py | 61 +++++++++++++------ 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/.idea/.name b/.idea/.name index c49de95..f25a89b 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -main4.py \ No newline at end of file +main (4).py \ No newline at end of file diff --git a/HNS/Excercises/02.11.2023 Battle Ship/main (4).py b/HNS/Excercises/02.11.2023 Battle Ship/main (4).py index 41c0474..c03063c 100644 --- a/HNS/Excercises/02.11.2023 Battle Ship/main (4).py +++ b/HNS/Excercises/02.11.2023 Battle Ship/main (4).py @@ -1,5 +1,7 @@ from tkinter import * +from enum import Enum +field_size = 10 empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', @@ -38,33 +40,52 @@ step2_field = [' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ', my_field = list(empty_field) enemy_field = list(empty_field) -buttons = [] + +class ShootResult(Enum): + Enum = 0 + Damaged = '\\' + Killed = X + Underfined = 'undefined' def set_ship(field, row, col, size, direction): - # size - 1, 2, 3, 4 - # direction - вниз(0), вправо(1) - if row < 0 or row > size - 1: + if row < 0 or row > field_size - 1: return - if col < 0 or col > size - 1: - return 0 + if col < 0 or col > field_size - 1: + return if direction == 0: - if row + size > size - 1: - return 0 + if row + size > field_size - 1: + return - for i in range(row, row + size): - index = i * size + col - size[index] = '1' + for r in range(row, row + size): + index = r * field_size + col + field[index] = '1' + + if direction == 1: + if col + size > field_size - 1: + return + for c in range(col, col + size): + index = c * field_size + row + field[index] = '1' -def draw_field(window, field): - btn = Button(window, text=" ") - btn.grid(column=0, row=0) - btn1 = Button(window, text=" ", bg='pink') - btn1.grid(column=1, row=0) - btn2 = Button(window, text=" ") - btn2.grid(column=2, row=0) +def draw_field(field): + for r in range(0, field_size): + for c in range(0, field_size): + bg = 'white' + if field[index] == '1': + bg = 'ping' + elif field[index] == '\\': + bg = 'red' + elif field[index] == '0': + bg = 'black' + btn.configupe(bg=bg) + + +def button_action(field, row, col): + shoot(field, row, col) + colorize(field) window = Tk() @@ -72,8 +93,8 @@ window.title("Ship Craft!") window.geometry('800x400') set_ship(my_field, 1, 1, 4, 1) -set_ship(1, 5, 3, 0) -draw_field(window, my_field) + +draw_field(field_size) window.mainloop()