Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox
- import time
- import random
- def shot(x, y):
- global turn
- # 0 - пусто
- # 1 - корабль
- # 2 - выстрел мимо
- # 3 - попадание
- if turn == 1:
- if battle_map1[y][x] == 0:
- # рисуем выстрел мимо
- #canvas.create_rectangle(offset, 0, offset + size_width, size_heigh, fill="white")
- ox = x * cell_size_w
- oy = y * cell_size_h
- id1 = canvas.create_oval(ox, oy, ox+cell_size_w, oy+cell_size_h, fill="red")
- off_x = cell_size_w//4
- off_y = cell_size_h//4
- id1 = canvas.create_oval(ox+off_x, oy+off_y, ox + off_x * 3, oy + off_y * 3, fill="white")
- canvas_elements.append(id1)
- battle_map1[y][x] = 2
- elif battle_map1[y][x] == 1:
- ox = x * cell_size_w
- oy = y * cell_size_h
- off_x = cell_size_w // 3
- off_y = cell_size_h // 3
- id1 = canvas.create_rectangle(ox+off_x, oy, ox + cell_size_w-off_x, oy + cell_size_h, fill="blue")
- canvas_elements.append(id1)
- id1 = canvas.create_rectangle(ox, oy + off_y, ox + cell_size_w, oy + cell_size_h - off_y, fill="blue")
- canvas_elements.append(id1)
- battle_map1[y][x] = 3
- else:
- pass
- turn = 0
- else:
- off = size_width + menu_x
- if battle_map2[y][x] == 0:
- # рисуем выстрел мимо
- ox = x * cell_size_w
- oy = y * cell_size_h
- id1 = canvas.create_oval(off + ox, oy, off + ox+cell_size_w, oy+cell_size_h, fill="red")
- off_x = cell_size_w//4
- off_y = cell_size_h//4
- id1 = canvas.create_oval(off + ox+off_x, oy+off_y, off + ox + off_x * 3, oy + off_y * 3, fill="white")
- canvas_elements.append(id1)
- battle_map2[y][x] = 2
- elif battle_map2[y][x] == 1:
- ox = x * cell_size_w
- oy = y * cell_size_h
- off_x = cell_size_w // 3
- off_y = cell_size_h // 3
- id1 = canvas.create_rectangle(off + ox+off_x, oy, off + ox + cell_size_w-off_x, oy + cell_size_h, fill="blue")
- canvas_elements.append(id1)
- id1 = canvas.create_rectangle(off + ox, oy + off_y, off + ox + cell_size_w, oy + cell_size_h - off_y, fill="blue")
- canvas_elements.append(id1)
- battle_map2[y][x] = 3
- else:
- pass
- turn = 1
- def check_win():
- global battle_map1, battle_map2, stop_game
- ships = 0
- for line in battle_map1:
- ships += line.count(1)
- if ships == 0:
- stop_game = True
- messagebox.showinfo("Морской бой", "Выиграл игрок 2")
- return
- ships = 0
- for line in battle_map2:
- ships += line.count(1)
- if ships == 0:
- stop_game = True
- messagebox.showinfo("Морской бой", "Выиграл игрок 1")
- return
- def canvas_click(event):
- _button = 0
- if stop_game:
- return
- if event.num == 3:
- _button = 1
- mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
- mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
- off = size_width + menu_x
- if turn == 1:
- if 0 <= mouse_x <= size_width and 0 <= mouse_y <= size_heigh:
- x = mouse_x // cell_size_w
- y = mouse_y // cell_size_h
- shot(x, y)
- check_win()
- label_player2['bg'] = 'white'
- label_player1['bg'] = 'yellow'
- else:
- if off <= mouse_x <= size_width + off and 0 <= mouse_y <= size_heigh:
- x = (mouse_x - off) // cell_size_w
- y = mouse_y // cell_size_h
- shot(x, y)
- check_win()
- label_player1['bg'] = 'white'
- label_player2['bg'] = 'yellow'
- def on_close_window():
- global flag_run_game
- if messagebox.askokcancel("Морской бой", "Хотите завершить игру?"):
- flag_run_game = False
- game.destroy()
- def draw_battle_map(offset=0):
- canvas.create_rectangle(offset, 0, offset + size_width, size_heigh, fill="white")
- for i in range(cells_w + 1):
- cid = canvas.create_line(offset + cell_size_w * i, 0, offset + cell_size_w * i, size_width)
- canvas_elements.append(cid)
- for i in range(cells_h + 1):
- cid = canvas.create_line(offset, cell_size_h * i, offset + size_width, cell_size_h * i)
- canvas_elements.append(cid)
- version = "0.4"
- size_width = 400
- size_heigh = 400
- cells_w = cells_h = 10
- cell_size_w = size_width // cells_w
- cell_size_h = size_heigh // cells_h
- menu_x = 200
- menu_y = 50
- # ++Часть 2
- size_width = cell_size_w * cells_w
- size_heigh = cell_size_h * cells_h
- canvas_elements = []
- battle_map1 = []
- battle_map2 = []
- turn = 0
- stop_game = False
- #--Часть 2
- flag_run_game = True
- game = tk.Tk()
- game.title(f"Игра - Морской бой. Версия. {version}")
- game.resizable(0, 0)
- game.wm_attributes("-topmost", 1)
- game.protocol("WM_DELETE_WINDOW", on_close_window)
- # ++Часть 2
- def generate_battle_map():
- global battle_map1,battle_map2
- battle_map1 = [[0 for x in range(cells_w)] for y in range(cells_h)]
- ships = cells_w // 2
- ships_config = []
- for i in range(ships):
- ships_config.append(random.choice([1, 2, 3]))
- sum_all_ships = sum(ships_config)
- sum_enemy = 0
- while sum_enemy != sum_all_ships:
- battle_map1 = [[0 for x in range(cells_w)] for y in range(cells_h)]
- for i in range(ships):
- ls = ships_config[i] # длина коробля
- horizontal_vertical = random.randint(0, 2) # 0 горизонтали, 1 по вертикали
- pos_x = random.randint(0, cells_w)
- pos_y = random.randint(0, cells_h)
- if horizontal_vertical == 0: # по горизонтали
- if pos_x + ls < cells_w:
- for j in range(ls):
- try:
- check_near_ships = 0
- check_near_ships = battle_map1[pos_y][pos_x - 1] + \
- battle_map1[pos_y][pos_x + j] + \
- battle_map1[pos_y][pos_x + j + 1] + \
- battle_map1[pos_y + 1][pos_x + j + 1] + \
- battle_map1[pos_y - 1][pos_x + j + 1] + \
- battle_map1[pos_y + 1][pos_x + j] + \
- battle_map1[pos_y - 1][pos_x + j]
- if check_near_ships == 0:
- battle_map1[pos_y][pos_x] = 1
- except Exception:
- pass
- else: # по вертикали
- if pos_y + ls < cells_h:
- for j in range(ls):
- try:
- check_near_ships = 0
- check_near_ships = battle_map1[pos_y - 1][pos_x] + \
- battle_map1[pos_y + j][pos_x] + \
- battle_map1[pos_y + j + 1][pos_x] + \
- battle_map1[pos_y + j + 1][pos_x + 1] + \
- battle_map1[pos_y + j + 1][pos_x - 1] + \
- battle_map1[pos_y + j][pos_x + 1] + \
- battle_map1[pos_y + j][pos_x - 1]
- if check_near_ships == 0:
- battle_map1[pos_y + j][pos_x] = 1
- except Exception:
- pass
- sum_enemy = 0
- for row in battle_map1:
- sum_enemy += sum(row)
- # 2 игрок
- battle_map2 = [[0 for x in range(cells_w)] for y in range(cells_h)]
- ships = cells_w // 2
- ships_config = []
- for i in range(ships):
- ships_config.append(random.choice([1, 2, 3]))
- sum_all_ships = sum(ships_config)
- sum_enemy = 0
- while sum_enemy != sum_all_ships:
- battle_map2 = [[0 for x in range(cells_w)] for y in range(cells_h)]
- for i in range(ships):
- ls = ships_config[i] # длина коробля
- horizontal_vertical = random.randint(0, 2) # 0 горизонтали, 1 по вертикали
- pos_x = random.randint(0, cells_w)
- pos_y = random.randint(0, cells_h)
- if horizontal_vertical == 0: # по горизонтали
- if pos_x + ls < cells_w:
- for j in range(ls):
- try:
- check_near_ships = 0
- check_near_ships = battle_map2[pos_y][pos_x - 1] + \
- battle_map2[pos_y][pos_x + j] + \
- battle_map2[pos_y][pos_x + j + 1] + \
- battle_map2[pos_y + 1][pos_x + j + 1] + \
- battle_map2[pos_y - 1][pos_x + j + 1] + \
- battle_map2[pos_y + 1][pos_x + j] + \
- battle_map2[pos_y - 1][pos_x + j]
- if check_near_ships == 0:
- battle_map2[pos_y][pos_x] = 1
- except Exception:
- pass
- else: # по вертикали
- if pos_y + ls < cells_h:
- for j in range(ls):
- try:
- check_near_ships = 0
- check_near_ships = battle_map2[pos_y - 1][pos_x] + \
- battle_map2[pos_y + j][pos_x] + \
- battle_map2[pos_y + j + 1][pos_x] + \
- battle_map2[pos_y + j + 1][pos_x + 1] + \
- battle_map2[pos_y + j + 1][pos_x - 1] + \
- battle_map2[pos_y + j][pos_x + 1] + \
- battle_map2[pos_y + j][pos_x - 1]
- if check_near_ships == 0:
- battle_map2[pos_y + j][pos_x] = 1
- except Exception:
- pass
- sum_enemy = 0
- for row in battle_map2:
- sum_enemy += sum(row)
- def command_show_ships():
- for i in range(cells_w):
- for j in range(cells_h):
- if battle_map1[j][i] != 0:
- cid = canvas.create_rectangle(i * cell_size_w, j * cell_size_h, (i + 1) * cell_size_w, (j + 1) * cell_size_h, fill="red")
- canvas_elements.append(cid)
- off = size_width + menu_x
- for i in range(cells_w):
- for j in range(cells_h):
- if battle_map2[j][i] != 0:
- 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")
- canvas_elements.append(cid)
- def command_start():
- global stop_game
- stop_game = False
- label_player2['bg'] = 'white'
- label_player1['bg'] = 'yellow'
- turn = 0
- for canvas_id in canvas_elements:
- canvas.delete(canvas_id)
- canvas.create_rectangle(0, 0, size_width, size_heigh, fill="white")
- draw_battle_map()
- draw_battle_map(offset=size_width + menu_x)
- generate_battle_map()
- canvas = tk.Canvas(game, width=2 * size_width + menu_x, height=size_heigh + menu_y, bd=0, highlightthickness=0)
- canvas.pack()
- canvas.bind_all("<Button-1>", func=canvas_click) # Клик Левая кнопка мыши
- canvas.bind_all("<Button-3>", func=canvas_click) # Клик Правая кнопка мыши
- button1 = tk.Button(game, text="Показать корабли", command=command_show_ships)
- button2 = tk.Button(game, text="Начать заново", command=command_start)
- button1.place(x=size_width+10, y=10, width=menu_x - 20, height=40)
- button2.place(x=size_width+10, y=60, width=menu_x - 20, height=40)
- label_player1 = tk.Label(game, text="Игрок №1", font=("Helvetica", 16))
- label_player2 = tk.Label(game, text="Игрок №2", font=("Helvetica", 16))
- label_player1.place(x=size_width//3, y=size_heigh+10, width=100, height=30)
- label_player2.place(x=size_width + menu_x + (size_width//3), y=size_heigh+10, width=100, height=30)
- generate_battle_map()
- game.update()
- draw_battle_map()
- draw_battle_map(offset=size_width + menu_x)
- label_player2['bg'] = 'white'
- label_player1['bg'] = 'yellow'
- while flag_run_game:
- if flag_run_game:
- game.update_idletasks()
- game.update()
- time.sleep(0.005)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement