Advertisement
sphinx2001

sea_war_part2

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