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 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.3"
- size_width = 700
- size_heigh = 700
- cells_w = cells_h = 10
- cell_size_w = size_width // cells_w
- cell_size_h = size_heigh // cells_h
- # ++Часть 2
- size_width = cell_size_w * cells_w
- size_heigh = cell_size_h * cells_h
- battle_map = []
- #--Часть 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:
- canvas.create_rectangle(i * cell_size_w, j * cell_size_h, (i + 1) * cell_size_w, (j + 1) * cell_size_h, fill="red")
- def command_start():
- canvas.create_rectangle(0, 0, size_width, size_heigh, fill="white")
- draw_battle_map()
- generate_battle_map()
- # canvas = tk.Canvas(game, width=size_width, height=size_heigh, bd=0, highlightthickness=0)
- canvas = tk.Canvas(game, width=size_width + 250, height=size_heigh, bd=0, highlightthickness=0)
- canvas.create_rectangle(0, 0, size_width, size_heigh, fill="white")
- canvas.pack()
- button1 = tk.Button(game, text="Показать корабли", command=command_show_ships)
- button2 = tk.Button(game, text="Начать заново", command=command_start)
- button1.place(x=size_width+20, y=30)
- button2.place(x=size_width+20, y=90)
- # --Часть 2
- generate_battle_map()
- game.update()
- draw_battle_map()
- 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