Advertisement
sphinx2001

Морской бой 1 группа

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