Advertisement
sphinx2001

Морской бой 1 группа, часть 4

Nov 29th, 2020
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.80 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. import time
  4. import random
  5.  
  6.  
  7. def shot(x, y):
  8.     global turn
  9.     # 0 - пусто
  10.     # 1 - корабль
  11.     # 2 - выстрел мимо
  12.     # 3 - попадание
  13.     if turn == 0:
  14.         if battle_map[y][x] == 0:
  15.             # рисуем выстрел мимо
  16.             #canvas.create_rectangle(offset, 0, offset + size_width, size_heigh, fill="white")
  17.             ox = x * cell_size_w
  18.             oy = y * cell_size_h
  19.             id1 = canvas.create_oval(ox, oy, ox+cell_size_w, oy+cell_size_h, fill="red")
  20.             off_x = cell_size_w//4
  21.             off_y = cell_size_h//4
  22.             id1 = canvas.create_oval(ox+off_x, oy+off_y, ox + off_x * 3, oy + off_y * 3, fill="white")
  23.             canvas_elements.append(id1)
  24.             battle_map[y][x] = 2
  25.  
  26.         elif battle_map[y][x] == 1:
  27.             ox = x * cell_size_w
  28.             oy = y * cell_size_h
  29.             off_x = cell_size_w // 3
  30.             off_y = cell_size_h // 3
  31.             id1 = canvas.create_rectangle(ox+off_x, oy, ox + cell_size_w-off_x, oy + cell_size_h, fill="blue")
  32.             canvas_elements.append(id1)
  33.             id1 = canvas.create_rectangle(ox, oy + off_y, ox + cell_size_w, oy + cell_size_h - off_y, fill="blue")
  34.             canvas_elements.append(id1)
  35.  
  36.             battle_map[y][x] = 3
  37.         else:
  38.             pass
  39.  
  40.     else:
  41.         turn = 0
  42.         # TODO дописать вариант для второго игрока
  43.  
  44. def canvas_click(event):
  45.     _button = 0
  46.     if event.num == 3:
  47.         _button = 1
  48.     mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
  49.     mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
  50.     print(mouse_x, mouse_y, _button)
  51.     if turn == 0:
  52.         if 0 <= mouse_x <= size_width and 0 <= mouse_y <= size_heigh:
  53.             x = mouse_x // cell_size_w
  54.             y = mouse_y // cell_size_h
  55.             print("player 1", x, y)
  56.             shot(x, y)
  57.  
  58.     else:
  59.         off = menu_x + size_width
  60.         if off <= mouse_x <= off + size_width and 0 <= mouse_y <= size_heigh:
  61.             x = (mouse_x - off) // cell_size_w
  62.             y = mouse_y // cell_size_h
  63.             print("player 2", x, y)
  64.  
  65.  
  66.  
  67.  
  68. def on_close_window():
  69.     global flag_run_game
  70.     if messagebox.askokcancel("Морской бой", "Хотите завершить игру?"):
  71.         flag_run_game = False
  72.         game.destroy()
  73.  
  74. def draw_battle_map(offset=0):
  75.     canvas.create_rectangle(offset, 0, offset + size_width, size_heigh, fill="white")
  76.     for i in range(cells_w + 1):
  77.         cid = canvas.create_line(offset + cell_size_w * i, 0, offset + cell_size_w * i, size_width)
  78.         canvas_elements.append(cid)
  79.     for i in range(cells_h + 1):
  80.         cid = canvas.create_line(offset, cell_size_h * i, offset + size_width, cell_size_h * i)
  81.         canvas_elements.append(cid)
  82.  
  83. version = "0.4"
  84.  
  85. size_width = 400
  86. size_heigh = 400
  87. cells_w = cells_h = 10
  88. cell_size_w = size_width // cells_w
  89. cell_size_h = size_heigh // cells_h
  90. menu_x = 200
  91. menu_y = 50
  92.  
  93. # ++Часть 2
  94. size_width = cell_size_w * cells_w
  95. size_heigh = cell_size_h * cells_h
  96. canvas_elements = []
  97. battle_map = []
  98. turn = 0
  99.  
  100. #--Часть 2
  101.  
  102. flag_run_game = True
  103.  
  104. game = tk.Tk()
  105. game.title(f"Игра - Морской бой. Версия. {version}")
  106. game.resizable(0, 0)
  107. game.wm_attributes("-topmost", 1)
  108. game.protocol("WM_DELETE_WINDOW", on_close_window)
  109.  
  110. # ++Часть 2
  111. def generate_battle_map():
  112.     global battle_map
  113.     battle_map = [[0 for x in range(cells_w)] for y in range(cells_h)]
  114.     ships = cells_w // 2
  115.     ships_config = []
  116.     for i in range(ships):
  117.         ships_config.append(random.choice([1, 2, 3]))
  118.  
  119.     sum_all_ships = sum(ships_config)
  120.     sum_enemy = 0
  121.     while sum_enemy != sum_all_ships:
  122.         battle_map = [[0 for x in range(cells_w)] for y in range(cells_h)]
  123.         for i in range(ships):
  124.             ls = ships_config[i] # длина коробля
  125.             horizontal_vertical = random.randint(0, 2) # 0 горизонтали, 1 по вертикали
  126.             pos_x = random.randint(0, cells_w)
  127.             pos_y = random.randint(0, cells_h)
  128.  
  129.             if horizontal_vertical == 0: # по горизонтали
  130.                 if pos_x + ls < cells_w:
  131.                     for j in range(ls):
  132.                         try:
  133.                             check_near_ships = 0
  134.                             check_near_ships = battle_map[pos_y][pos_x - 1] + \
  135.                                 battle_map[pos_y][pos_x + j] + \
  136.                                 battle_map[pos_y][pos_x + j + 1] + \
  137.                                 battle_map[pos_y + 1][pos_x + j + 1] + \
  138.                                 battle_map[pos_y - 1][pos_x + j + 1] + \
  139.                                 battle_map[pos_y + 1][pos_x + j] + \
  140.                                 battle_map[pos_y - 1][pos_x + j]
  141.  
  142.                             if check_near_ships == 0:
  143.                                 battle_map[pos_y][pos_x] = 1
  144.  
  145.                         except Exception:
  146.                             pass
  147.             else: # по вертикали
  148.                 if pos_y + ls < cells_h:
  149.                     for j in range(ls):
  150.                         try:
  151.                             check_near_ships = 0
  152.                             check_near_ships = battle_map[pos_y - 1][pos_x] + \
  153.                                 battle_map[pos_y + j][pos_x] + \
  154.                                 battle_map[pos_y + j + 1][pos_x] + \
  155.                                 battle_map[pos_y + j + 1][pos_x + 1] + \
  156.                                 battle_map[pos_y + j + 1][pos_x - 1] + \
  157.                                 battle_map[pos_y + j][pos_x + 1] + \
  158.                                 battle_map[pos_y + j][pos_x - 1]
  159.                             if check_near_ships == 0:
  160.                                 battle_map[pos_y + j][pos_x] = 1
  161.                         except Exception:
  162.                             pass
  163.  
  164.             sum_enemy = 0
  165.             for row in battle_map:
  166.                 sum_enemy += sum(row)
  167.  
  168.  
  169. def command_show_ships():
  170.     for i in range(cells_w):
  171.         for j in range(cells_h):
  172.             if battle_map[j][i] != 0:
  173.                 cid = canvas.create_rectangle(i * cell_size_w, j * cell_size_h, (i + 1) * cell_size_w, (j + 1) * cell_size_h, fill="red")
  174.                 canvas_elements.append(cid)
  175.  
  176. def command_start():
  177.     for canvas_id in canvas_elements:
  178.         canvas.delete(canvas_id)
  179.     canvas.create_rectangle(0, 0, size_width, size_heigh, fill="white")
  180.     draw_battle_map()
  181.     draw_battle_map(offset=size_width + menu_x)
  182.     generate_battle_map()
  183.  
  184.  
  185. canvas = tk.Canvas(game, width=2 * size_width + menu_x, height=size_heigh + menu_y, bd=0, highlightthickness=0)
  186. canvas.pack()
  187.  
  188. canvas.bind_all("<Button-1>", func=canvas_click) # Клик Левая кнопка мыши
  189. canvas.bind_all("<Button-3>", func=canvas_click) # Клик Правая кнопка мыши
  190.  
  191. button1 = tk.Button(game, text="Показать корабли", command=command_show_ships)
  192. button2 = tk.Button(game, text="Начать заново", command=command_start)
  193. button1.place(x=size_width+10, y=10, width=menu_x - 20, height=40)
  194. button2.place(x=size_width+10, y=60, width=menu_x - 20, height=40)
  195. label_player1 = tk.Label(game, text="Игрок №1", font=("Helvetica", 16))
  196. label_player2 = tk.Label(game, text="Игрок №2", font=("Helvetica", 16))
  197. label_player1.place(x=size_width//3, y=size_heigh+10, width=100, height=30)
  198. label_player2.place(x=size_width + menu_x + (size_width//3), y=size_heigh+10, width=100, height=30)
  199.  
  200. generate_battle_map()
  201. game.update()
  202. draw_battle_map()
  203. draw_battle_map(offset=size_width + menu_x)
  204. while flag_run_game:
  205.     if flag_run_game:
  206.         game.update_idletasks()
  207.         game.update()
  208.     time.sleep(0.005)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement