Advertisement
sphinx2001

морской бой. 2 группа. часть 2

Nov 24th, 2020
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox
  3. import time
  4.  
  5. def on_click_canvas(event):
  6.     _button = 0
  7.     if event.num == 3:
  8.         _button = 1
  9.     mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
  10.     mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
  11.  
  12.     map_x = mouse_x // cell_size_w
  13.     map_y = mouse_y // cell_size_h
  14.  
  15.     print(f"Click button {_button} x:{mouse_x}, y:{mouse_y} cell({map_x},{map_y})")
  16.  
  17. def command_new_game():
  18.     print("New game")
  19.  
  20. def command_debug_show_ships():
  21.     print("command_debug_show_ships")
  22.  
  23. def on_close_window():
  24.     global run_app
  25.     if messagebox.askokcancel("Морской бой", "Завершить работу программы?"):
  26.         run_app = False
  27.         game.destroy()
  28.  
  29. def draw_game_map():
  30.     for i in range(cells_w + 1):
  31.         canvas.create_line(cell_size_w * i, 0, cell_size_w * i, size_h)
  32.  
  33.     for i in range(cells_h + 1):
  34.         canvas.create_line(0, cell_size_h * i, size_w, cell_size_h * i)
  35.  
  36. size_w = 733
  37. size_h = 444
  38. cells_w = 5
  39. cells_h = 5
  40. cell_size_w = size_w // cells_w
  41. cell_size_h = size_h // cells_h
  42. size_w = cells_w * cell_size_w
  43. size_h = cells_h * cell_size_h
  44.  
  45. menu_size = 250
  46.  
  47. battle_map = [[0 for _ in range(cells_w)] for _ in range(cells_h)]
  48. print(battle_map)
  49.  
  50. _version = "0.1"
  51. run_app = True
  52. game = tk.Tk()
  53. game.protocol("WM_DELETE_WINDOW", on_close_window)
  54. game.resizable(0, 0) # Запрет изменения размеров окна
  55. game.wm_attributes("-topmost", 1) # окно приложение всегда на первом плане.
  56. game.title(f"Игра морской бой. Версия {_version}")
  57. canvas = tk.Canvas(game, width=size_w+menu_size, height=size_h, bd=0, highlightthickness=0)
  58. canvas.create_rectangle(0, 0, size_w, size_h, fill="white")
  59. canvas.pack()
  60.  
  61. button1 = tk.Button(game, text="Начать новую игру", command=command_new_game)
  62. button2 = tk.Button(game, text="Показать корабли (отладка)", command=command_debug_show_ships)
  63. button1.place(x=size_w+20, y=30)
  64. button2.place(x=size_w+20, y=90)
  65.  
  66. canvas.bind_all("<Button-1>", func=on_click_canvas) # обработка нажатия левой кнопки мыши
  67. canvas.bind_all("<Button-3>", func=on_click_canvas) # обработка нажатия правой кнопки мыши
  68.  
  69. draw_game_map()
  70. while run_app:
  71.     if run_app:
  72.         game.update_idletasks()
  73.         game.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement