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 == 0:
- if battle_map[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_map[y][x] = 2
- elif battle_map[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_map[y][x] = 3
- else:
- pass
- else:
- turn = 0
- # TODO дописать вариант для второго игрока
- def canvas_click(event):
- _button = 0
- if event.num == 3:
- _button = 1
- mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
- mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
- print(mouse_x, mouse_y, _button)
- if turn == 0:
- if 0 <= mouse_x <= size_width and 0 <= mouse_y <= size_heigh:
- x = mouse_x // cell_size_w
- y = mouse_y // cell_size_h
- print("player 1", x, y)
- shot(x, y)
- else:
- off = menu_x + size_width
- if off <= mouse_x <= off + size_width and 0 <= mouse_y <= size_heigh:
- x = (mouse_x - off) // cell_size_w
- y = mouse_y // cell_size_h
- print("player 2", x, y)
- 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_map = []
- turn = 0
- #--Часть 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_map
- battle_map = [[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_map = [[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_map[pos_y][pos_x - 1] + \
- battle_map[pos_y][pos_x + j] + \
- battle_map[pos_y][pos_x + j + 1] + \
- battle_map[pos_y + 1][pos_x + j + 1] + \
- battle_map[pos_y - 1][pos_x + j + 1] + \
- battle_map[pos_y + 1][pos_x + j] + \
- battle_map[pos_y - 1][pos_x + j]
- if check_near_ships == 0:
- battle_map[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_map[pos_y - 1][pos_x] + \
- battle_map[pos_y + j][pos_x] + \
- battle_map[pos_y + j + 1][pos_x] + \
- battle_map[pos_y + j + 1][pos_x + 1] + \
- battle_map[pos_y + j + 1][pos_x - 1] + \
- battle_map[pos_y + j][pos_x + 1] + \
- battle_map[pos_y + j][pos_x - 1]
- if check_near_ships == 0:
- battle_map[pos_y + j][pos_x] = 1
- except Exception:
- pass
- sum_enemy = 0
- for row in battle_map:
- sum_enemy += sum(row)
- def command_show_ships():
- for i in range(cells_w):
- for j in range(cells_h):
- if battle_map[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)
- def command_start():
- 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)
- 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