Advertisement
sphinx2001

морской бой 1 группа 5

Dec 6th, 2020
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.98 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 == 1:
  14.         if battle_map1[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_map1[y][x] = 2
  25.  
  26.         elif battle_map1[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_map1[y][x] = 3
  37.         else:
  38.             pass
  39.  
  40.         turn = 0
  41.     else:
  42.         off = size_width + menu_x
  43.         if battle_map2[y][x] == 0:
  44.             # рисуем выстрел мимо
  45.             ox = x * cell_size_w
  46.             oy = y * cell_size_h
  47.             id1 = canvas.create_oval(off + ox, oy, off + ox+cell_size_w, oy+cell_size_h, fill="red")
  48.             off_x = cell_size_w//4
  49.             off_y = cell_size_h//4
  50.             id1 = canvas.create_oval(off + ox+off_x, oy+off_y, off + ox + off_x * 3, oy + off_y * 3, fill="white")
  51.             canvas_elements.append(id1)
  52.             battle_map2[y][x] = 2
  53.  
  54.         elif battle_map2[y][x] == 1:
  55.             ox = x * cell_size_w
  56.             oy = y * cell_size_h
  57.             off_x = cell_size_w // 3
  58.             off_y = cell_size_h // 3
  59.             id1 = canvas.create_rectangle(off + ox+off_x, oy, off + ox + cell_size_w-off_x, oy + cell_size_h, fill="blue")
  60.             canvas_elements.append(id1)
  61.             id1 = canvas.create_rectangle(off + ox, oy + off_y, off + ox + cell_size_w, oy + cell_size_h - off_y, fill="blue")
  62.             canvas_elements.append(id1)
  63.  
  64.             battle_map2[y][x] = 3
  65.         else:
  66.             pass
  67.         turn = 1
  68.         # TODO дописать вариант для второго игрока
  69.  
  70. def canvas_click(event):
  71.     print(battle_map1)
  72.     print(battle_map2)
  73.     _button = 0
  74.     if event.num == 3:
  75.         _button = 1
  76.     mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
  77.     mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
  78.     print(mouse_x, mouse_y, _button)
  79.     off = size_width + menu_x
  80.     # turn = 1
  81.     # if turn == 0:
  82.     #     x = mouse_x // cell_size_w
  83.     #     y = mouse_y // cell_size_h
  84.     #     print(f"player {turn}", x, y)
  85.     # else:
  86.     #     x = (mouse_x - off) // cell_size_w
  87.     #     y = mouse_y // cell_size_h
  88.     #     print(f"player {turn}", x, y)
  89.     #         shot(x, y)
  90.     if turn == 1:
  91.         if 0 <= mouse_x <= size_width and 0 <= mouse_y <= size_heigh:
  92.             x = mouse_x // cell_size_w
  93.             y = mouse_y // cell_size_h
  94.             print("player 2", x, y)
  95.             shot(x, y)
  96.  
  97.     else:
  98.         if off <= mouse_x <= size_width + off and 0 <= mouse_y <= size_heigh:
  99.  
  100.             x = (mouse_x - off) // cell_size_w
  101.             y = mouse_y // cell_size_h
  102.             print("player 2", x, y)
  103.             shot(x, y)
  104.  
  105.  
  106.  
  107.  
  108. def on_close_window():
  109.     global flag_run_game
  110.     if messagebox.askokcancel("Морской бой", "Хотите завершить игру?"):
  111.         flag_run_game = False
  112.         game.destroy()
  113.  
  114. def draw_battle_map(offset=0):
  115.     canvas.create_rectangle(offset, 0, offset + size_width, size_heigh, fill="white")
  116.     for i in range(cells_w + 1):
  117.         cid = canvas.create_line(offset + cell_size_w * i, 0, offset + cell_size_w * i, size_width)
  118.         canvas_elements.append(cid)
  119.     for i in range(cells_h + 1):
  120.         cid = canvas.create_line(offset, cell_size_h * i, offset + size_width, cell_size_h * i)
  121.         canvas_elements.append(cid)
  122.  
  123. version = "0.4"
  124.  
  125. size_width = 400
  126. size_heigh = 400
  127. cells_w = cells_h = 10
  128. cell_size_w = size_width // cells_w
  129. cell_size_h = size_heigh // cells_h
  130. menu_x = 200
  131. menu_y = 50
  132.  
  133. # ++Часть 2
  134. size_width = cell_size_w * cells_w
  135. size_heigh = cell_size_h * cells_h
  136. canvas_elements = []
  137. battle_map1 = []
  138. battle_map2 = []
  139. turn = 0
  140.  
  141. #--Часть 2
  142.  
  143. flag_run_game = True
  144.  
  145. game = tk.Tk()
  146. game.title(f"Игра - Морской бой. Версия. {version}")
  147. game.resizable(0, 0)
  148. game.wm_attributes("-topmost", 1)
  149. game.protocol("WM_DELETE_WINDOW", on_close_window)
  150.  
  151. # ++Часть 2
  152. def generate_battle_map():
  153.     global battle_map1,battle_map2
  154.     battle_map1 = [[0 for x in range(cells_w)] for y in range(cells_h)]
  155.     ships = cells_w // 2
  156.     ships_config = []
  157.     for i in range(ships):
  158.         ships_config.append(random.choice([1, 2, 3]))
  159.  
  160.     sum_all_ships = sum(ships_config)
  161.     sum_enemy = 0
  162.     while sum_enemy != sum_all_ships:
  163.         battle_map1 = [[0 for x in range(cells_w)] for y in range(cells_h)]
  164.         for i in range(ships):
  165.             ls = ships_config[i] # длина коробля
  166.             horizontal_vertical = random.randint(0, 2) # 0 горизонтали, 1 по вертикали
  167.             pos_x = random.randint(0, cells_w)
  168.             pos_y = random.randint(0, cells_h)
  169.  
  170.             if horizontal_vertical == 0: # по горизонтали
  171.                 if pos_x + ls < cells_w:
  172.                     for j in range(ls):
  173.                         try:
  174.                             check_near_ships = 0
  175.                             check_near_ships = battle_map1[pos_y][pos_x - 1] + \
  176.                                 battle_map1[pos_y][pos_x + j] + \
  177.                                 battle_map1[pos_y][pos_x + j + 1] + \
  178.                                 battle_map1[pos_y + 1][pos_x + j + 1] + \
  179.                                 battle_map1[pos_y - 1][pos_x + j + 1] + \
  180.                                 battle_map1[pos_y + 1][pos_x + j] + \
  181.                                 battle_map1[pos_y - 1][pos_x + j]
  182.  
  183.                             if check_near_ships == 0:
  184.                                 battle_map1[pos_y][pos_x] = 1
  185.  
  186.                         except Exception:
  187.                             pass
  188.             else: # по вертикали
  189.                 if pos_y + ls < cells_h:
  190.                     for j in range(ls):
  191.                         try:
  192.                             check_near_ships = 0
  193.                             check_near_ships = battle_map1[pos_y - 1][pos_x] + \
  194.                                 battle_map1[pos_y + j][pos_x] + \
  195.                                 battle_map1[pos_y + j + 1][pos_x] + \
  196.                                 battle_map1[pos_y + j + 1][pos_x + 1] + \
  197.                                 battle_map1[pos_y + j + 1][pos_x - 1] + \
  198.                                 battle_map1[pos_y + j][pos_x + 1] + \
  199.                                 battle_map1[pos_y + j][pos_x - 1]
  200.                             if check_near_ships == 0:
  201.                                 battle_map1[pos_y + j][pos_x] = 1
  202.                         except Exception:
  203.                             pass
  204.  
  205.             sum_enemy = 0
  206.             for row in battle_map1:
  207.                 sum_enemy += sum(row)
  208.     # 2 игрок
  209.     battle_map2 = [[0 for x in range(cells_w)] for y in range(cells_h)]
  210.     ships = cells_w // 2
  211.     ships_config = []
  212.     for i in range(ships):
  213.         ships_config.append(random.choice([1, 2, 3]))
  214.  
  215.     sum_all_ships = sum(ships_config)
  216.     sum_enemy = 0
  217.     while sum_enemy != sum_all_ships:
  218.         battle_map2 = [[0 for x in range(cells_w)] for y in range(cells_h)]
  219.         for i in range(ships):
  220.             ls = ships_config[i] # длина коробля
  221.             horizontal_vertical = random.randint(0, 2) # 0 горизонтали, 1 по вертикали
  222.             pos_x = random.randint(0, cells_w)
  223.             pos_y = random.randint(0, cells_h)
  224.  
  225.             if horizontal_vertical == 0: # по горизонтали
  226.                 if pos_x + ls < cells_w:
  227.                     for j in range(ls):
  228.                         try:
  229.                             check_near_ships = 0
  230.                             check_near_ships = battle_map2[pos_y][pos_x - 1] + \
  231.                                 battle_map2[pos_y][pos_x + j] + \
  232.                                 battle_map2[pos_y][pos_x + j + 1] + \
  233.                                 battle_map2[pos_y + 1][pos_x + j + 1] + \
  234.                                 battle_map2[pos_y - 1][pos_x + j + 1] + \
  235.                                 battle_map2[pos_y + 1][pos_x + j] + \
  236.                                 battle_map2[pos_y - 1][pos_x + j]
  237.  
  238.                             if check_near_ships == 0:
  239.                                 battle_map2[pos_y][pos_x] = 1
  240.  
  241.                         except Exception:
  242.                             pass
  243.             else: # по вертикали
  244.                 if pos_y + ls < cells_h:
  245.                     for j in range(ls):
  246.                         try:
  247.                             check_near_ships = 0
  248.                             check_near_ships = battle_map2[pos_y - 1][pos_x] + \
  249.                                 battle_map2[pos_y + j][pos_x] + \
  250.                                 battle_map2[pos_y + j + 1][pos_x] + \
  251.                                 battle_map2[pos_y + j + 1][pos_x + 1] + \
  252.                                 battle_map2[pos_y + j + 1][pos_x - 1] + \
  253.                                 battle_map2[pos_y + j][pos_x + 1] + \
  254.                                 battle_map2[pos_y + j][pos_x - 1]
  255.                             if check_near_ships == 0:
  256.                                 battle_map2[pos_y + j][pos_x] = 1
  257.                         except Exception:
  258.                             pass
  259.  
  260.             sum_enemy = 0
  261.             for row in battle_map2:
  262.                 sum_enemy += sum(row)
  263.  
  264.  
  265.  
  266. def command_show_ships():
  267.     for i in range(cells_w):
  268.         for j in range(cells_h):
  269.             if battle_map1[j][i] != 0:
  270.                 cid = canvas.create_rectangle(i * cell_size_w, j * cell_size_h, (i + 1) * cell_size_w, (j + 1) * cell_size_h, fill="red")
  271.                 canvas_elements.append(cid)
  272.  
  273. def command_start():
  274.     for canvas_id in canvas_elements:
  275.         canvas.delete(canvas_id)
  276.     canvas.create_rectangle(0, 0, size_width, size_heigh, fill="white")
  277.     draw_battle_map()
  278.     draw_battle_map(offset=size_width + menu_x)
  279.     generate_battle_map()
  280.  
  281.  
  282. canvas = tk.Canvas(game, width=2 * size_width + menu_x, height=size_heigh + menu_y, bd=0, highlightthickness=0)
  283. canvas.pack()
  284.  
  285. canvas.bind_all("<Button-1>", func=canvas_click) # Клик Левая кнопка мыши
  286. canvas.bind_all("<Button-3>", func=canvas_click) # Клик Правая кнопка мыши
  287.  
  288. button1 = tk.Button(game, text="Показать корабли", command=command_show_ships)
  289. button2 = tk.Button(game, text="Начать заново", command=command_start)
  290. button1.place(x=size_width+10, y=10, width=menu_x - 20, height=40)
  291. button2.place(x=size_width+10, y=60, width=menu_x - 20, height=40)
  292. label_player1 = tk.Label(game, text="Игрок №1", font=("Helvetica", 16))
  293. label_player2 = tk.Label(game, text="Игрок №2", font=("Helvetica", 16))
  294. label_player1.place(x=size_width//3, y=size_heigh+10, width=100, height=30)
  295. label_player2.place(x=size_width + menu_x + (size_width//3), y=size_heigh+10, width=100, height=30)
  296.  
  297. generate_battle_map()
  298. game.update()
  299. draw_battle_map()
  300. draw_battle_map(offset=size_width + menu_x)
  301. while flag_run_game:
  302.     if flag_run_game:
  303.         game.update_idletasks()
  304.         game.update()
  305.     time.sleep(0.005)
  306.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement