Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox
- import time
- def on_click_canvas(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()
- map_x = mouse_x // cell_size_w
- map_y = mouse_y // cell_size_h
- print(f"Click button {_button} x:{mouse_x}, y:{mouse_y} cell({map_x},{map_y})")
- def command_new_game():
- print("New game")
- def command_debug_show_ships():
- print("command_debug_show_ships")
- def on_close_window():
- global run_app
- if messagebox.askokcancel("Морской бой", "Завершить работу программы?"):
- run_app = False
- game.destroy()
- def draw_game_map():
- for i in range(cells_w + 1):
- canvas.create_line(cell_size_w * i, 0, cell_size_w * i, size_h)
- for i in range(cells_h + 1):
- canvas.create_line(0, cell_size_h * i, size_w, cell_size_h * i)
- size_w = 733
- size_h = 444
- cells_w = 5
- cells_h = 5
- cell_size_w = size_w // cells_w
- cell_size_h = size_h // cells_h
- size_w = cells_w * cell_size_w
- size_h = cells_h * cell_size_h
- menu_size = 250
- battle_map = [[0 for _ in range(cells_w)] for _ in range(cells_h)]
- print(battle_map)
- _version = "0.1"
- run_app = True
- game = tk.Tk()
- game.protocol("WM_DELETE_WINDOW", on_close_window)
- game.resizable(0, 0) # Запрет изменения размеров окна
- game.wm_attributes("-topmost", 1) # окно приложение всегда на первом плане.
- game.title(f"Игра морской бой. Версия {_version}")
- canvas = tk.Canvas(game, width=size_w+menu_size, height=size_h, bd=0, highlightthickness=0)
- canvas.create_rectangle(0, 0, size_w, size_h, fill="white")
- canvas.pack()
- button1 = tk.Button(game, text="Начать новую игру", command=command_new_game)
- button2 = tk.Button(game, text="Показать корабли (отладка)", command=command_debug_show_ships)
- button1.place(x=size_w+20, y=30)
- button2.place(x=size_w+20, y=90)
- canvas.bind_all("<Button-1>", func=on_click_canvas) # обработка нажатия левой кнопки мыши
- canvas.bind_all("<Button-3>", func=on_click_canvas) # обработка нажатия правой кнопки мыши
- draw_game_map()
- while run_app:
- if run_app:
- game.update_idletasks()
- game.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement