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_close_window():
- global flag_run_game
- if messagebox.askokcancel("Морской бой", "Хотите завершить игру?"):
- flag_run_game = False
- game.destroy()
- def draw_battle_map():
- for i in range(cells_w + 1):
- canvas.create_line(cell_size_w * i, 0, cell_size_w * i, size_width)
- for i in range(cells_h + 1):
- canvas.create_line(0, cell_size_h * i, size_heigh, cell_size_h * i)
- version = "0.1"
- size_width = 700
- size_heigh = 700
- cells_w = cells_h = 5
- cell_size_w = size_width // cells_w
- cell_size_h = size_heigh // cells_h
- # Part 2 ++
- menu_size = 250
- # Part 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)
- canvas = tk.Canvas(game, width=size_width+menu_size, height=size_heigh, bd=0, highlightthickness=0)
- canvas.create_rectangle(0, 0, size_width, size_heigh, fill="white")
- canvas.pack()
- game.update()
- draw_battle_map()
- # Part 2 ++
- battle_map = [[0 for _ in range(cells_w)] for x in range(cells_h)]
- print(battle_map)
- def command_start():
- pass
- def command_debug_show_ships():
- pass
- def on_click(event):
- _button = 0
- if event.num == 3:
- _button = 1
- #print(_button)
- mouse_x = canvas.winfo_pointerx() - canvas.winfo_rootx()
- mouse_y = canvas.winfo_pointery() - canvas.winfo_rooty()
- #print(f"mouse_x: {mouse_x}, mouse_y:{mouse_y}")
- map_x = mouse_x // cell_size_w
- map_y = mouse_y // cell_size_h
- print(f"x: {map_x}, y:{map_y}, button: {_button}")
- button1 = tk.Button(text="Начать заново", command=command_start)
- button1.place(x=size_width+20, y=30)
- button2 = tk.Button(text="Отладка: показать корабли", command=command_debug_show_ships)
- button2.place(x=size_width+20, y=90)
- canvas.bind_all("<Button-1>",on_click) # левый клик мыши
- canvas.bind_all("<Button-3>",on_click) # правый клик мыши
- # Part 2 --
- 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