ЗАГРУЖАЮ ДЗ РАЗНЫЕ ПОЛЯ И ПРОБЕЛ МЕЖДУ ПОЛЯМИ
This commit is contained in:
parent
2b321dcc0a
commit
f20203bdd9
|
@ -0,0 +1 @@
|
||||||
|
main.py
|
|
@ -1,7 +1,19 @@
|
||||||
def keypress_handler(e):
|
import tkinter as tk
|
||||||
if 1 <= int(e.keysym) <= 4:
|
|
||||||
ship_size = int(e.keysym)
|
|
||||||
return ship_size
|
|
||||||
|
|
||||||
|
root = tk.Tk()
|
||||||
|
|
||||||
print(keypress_handler(3))
|
l1 = tk.Label(root, text='Col:8 Row:1', bg='red')
|
||||||
|
l1.grid(column=8, row=1)
|
||||||
|
|
||||||
|
l2 = tk.Label(root, text='Col:1 Row:8', bg='red')
|
||||||
|
l2.grid(column=1, row=8)
|
||||||
|
|
||||||
|
# add empty label in row 0 and column 0
|
||||||
|
l0 = tk.Label(root, text=' \n ', bg='green')
|
||||||
|
l0.grid(column=0, row=0)
|
||||||
|
|
||||||
|
# add empty label in row 9 and column 9
|
||||||
|
l9 = tk.Label(root, text=' ', bg='green')
|
||||||
|
l9.grid(column=9, row=9)
|
||||||
|
|
||||||
|
root.mainloop()
|
|
@ -1,6 +1,5 @@
|
||||||
from tkinter import *
|
from tkinter import *
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
import math
|
|
||||||
|
|
||||||
my_buttons = []
|
my_buttons = []
|
||||||
enemy_buttons = []
|
enemy_buttons = []
|
||||||
|
@ -8,9 +7,13 @@ enemy_buttons = []
|
||||||
field_size = 10
|
field_size = 10
|
||||||
active_field = 0
|
active_field = 0
|
||||||
|
|
||||||
ship_size = 4
|
ship_size_left = 4
|
||||||
ship_direction = 0
|
ship_direction_left = 0
|
||||||
field_mode = 0
|
field_mode_left = 0
|
||||||
|
|
||||||
|
ship_size_right = 4
|
||||||
|
ship_direction_right = 0
|
||||||
|
field_mode_right = 0
|
||||||
|
|
||||||
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
|
@ -103,43 +106,123 @@ def colorize(field, buttons):
|
||||||
|
|
||||||
|
|
||||||
def keypress_handler(e):
|
def keypress_handler(e):
|
||||||
global ship_size
|
global ship_size_left
|
||||||
global field_mode
|
global field_mode_left
|
||||||
|
|
||||||
|
global ship_size_right
|
||||||
|
global field_mode_right
|
||||||
|
|
||||||
if e.keysym.isnumeric():
|
if e.keysym.isnumeric():
|
||||||
ship_size = int(e.keysym)
|
if active_field == 0:
|
||||||
|
ship_size_left = int(e.keysym)
|
||||||
|
else:
|
||||||
|
ship_size_right = int(e.keysym)
|
||||||
else:
|
else:
|
||||||
if e.keysym == 'm':
|
if e.keysym == 'm':
|
||||||
|
if active_field == 0:
|
||||||
|
field_mode = field_mode_left
|
||||||
|
else:
|
||||||
|
field_mode = field_mode_right
|
||||||
if field_mode == 0:
|
if field_mode == 0:
|
||||||
field_mode = 1
|
field_mode = 1
|
||||||
else:
|
else:
|
||||||
field_mode = 0
|
field_mode = 0
|
||||||
|
|
||||||
|
if active_field == 0:
|
||||||
|
field_mode_left = field_mode
|
||||||
|
else:
|
||||||
|
field_mode_right = field_mode
|
||||||
|
|
||||||
|
|
||||||
def left_button_click(field, buttons, row, col):
|
def left_button_click(field, buttons, row, col):
|
||||||
|
if active_field == 0:
|
||||||
|
field_mode = field_mode_left
|
||||||
|
else:
|
||||||
|
field_mode = field_mode_right
|
||||||
|
|
||||||
if field_mode == 0:
|
if field_mode == 0:
|
||||||
if check_possible(field, row, col):
|
if check_possible(field, row, col):
|
||||||
set_ship(field, row, col, ship_size, ship_direction)
|
if active_field == 0:
|
||||||
|
set_ship(field, row, col, ship_size_left, ship_direction_left)
|
||||||
|
else:
|
||||||
|
set_ship(field, row, col, ship_size_right, ship_direction_right)
|
||||||
elif field_mode == 1:
|
elif field_mode == 1:
|
||||||
shoot(field, row, col)
|
shoot(field, row, col)
|
||||||
colorize(field, buttons)
|
colorize(field, buttons)
|
||||||
|
|
||||||
|
|
||||||
def right_button_click(d):
|
def right_button_click(d):
|
||||||
global ship_direction
|
global ship_direction_left
|
||||||
|
global ship_direction_right
|
||||||
|
|
||||||
|
global field_mode_left
|
||||||
|
global field_mode_right
|
||||||
|
|
||||||
|
if active_field == 0:
|
||||||
|
field_mode = field_mode_left
|
||||||
|
ship_direction = ship_direction_left
|
||||||
|
else:
|
||||||
|
field_mode = field_mode_right
|
||||||
|
ship_direction = ship_direction_right
|
||||||
|
|
||||||
|
|
||||||
if field_mode == 0:
|
if field_mode == 0:
|
||||||
if ship_direction == 0:
|
if ship_direction == 0:
|
||||||
ship_direction = 1
|
ship_direction = 1
|
||||||
else:
|
else:
|
||||||
ship_direction = 0
|
ship_direction = 0
|
||||||
|
|
||||||
|
if active_field == 0:
|
||||||
|
ship_direction_left = ship_direction
|
||||||
|
else:
|
||||||
|
ship_direction_right = ship_direction
|
||||||
|
|
||||||
def button_enter(field, buttons, row, col):
|
def button_enter(field, buttons, row, col):
|
||||||
|
global active_field
|
||||||
|
global ship_direction_left
|
||||||
|
global ship_direction_right
|
||||||
|
|
||||||
|
global ship_size_left
|
||||||
|
global ship_size_right
|
||||||
|
|
||||||
|
global field_mode_left
|
||||||
|
global field_mode_right
|
||||||
|
|
||||||
|
|
||||||
|
if buttons == my_buttons:
|
||||||
|
active_field = 0
|
||||||
|
elif buttons == enemy_buttons:
|
||||||
|
active_field = 1
|
||||||
|
|
||||||
|
if active_field == 0:
|
||||||
|
field_mode = field_mode_left
|
||||||
|
ship_direction = ship_direction_left
|
||||||
|
ship_size = ship_size_left
|
||||||
|
other_field = enemy_field
|
||||||
|
other_buttons = enemy_buttons
|
||||||
|
else:
|
||||||
|
field_mode = field_mode_right
|
||||||
|
ship_direction = ship_direction_right
|
||||||
|
ship_size = ship_size_right
|
||||||
|
other_field = my_field
|
||||||
|
other_buttons = my_buttons
|
||||||
|
|
||||||
|
|
||||||
|
for i in range(0, len(other_field)):
|
||||||
|
if other_field[i] == "p":
|
||||||
|
other_field[i] = ''
|
||||||
|
|
||||||
|
colorize(other_field, other_buttons)
|
||||||
|
|
||||||
|
|
||||||
if field_mode == 0:
|
if field_mode == 0:
|
||||||
for i in range(0, len(field)):
|
for i in range(0, len(field)):
|
||||||
if field[i] == "p":
|
if field[i] == "p":
|
||||||
field[i] = ''
|
field[i] = ''
|
||||||
|
|
||||||
if check_possible(field, row, col):
|
|
||||||
|
|
||||||
|
if check_possible(field, row, col,ship_size, ship_direction):
|
||||||
if ship_direction == 0:
|
if ship_direction == 0:
|
||||||
for r in range(row, row + ship_size):
|
for r in range(row, row + ship_size):
|
||||||
field[r * field_size + col] = "p"
|
field[r * field_size + col] = "p"
|
||||||
|
@ -151,7 +234,7 @@ def button_enter(field, buttons, row, col):
|
||||||
colorize(field, buttons)
|
colorize(field, buttons)
|
||||||
|
|
||||||
|
|
||||||
def check_possible(field, row, col):
|
def check_possible(field, row, col, ship_size, ship_direction):
|
||||||
# Функция должна возвращать True, если можно поставить сюда корабль,
|
# Функция должна возвращать True, если можно поставить сюда корабль,
|
||||||
# в противном случае - False
|
# в противном случае - False
|
||||||
|
|
||||||
|
@ -194,10 +277,13 @@ set_ship(my_field, 1, 1, 4, 1)
|
||||||
set_ship(my_field,0, 6, 3, 0)
|
set_ship(my_field,0, 6, 3, 0)
|
||||||
set_ship(my_field, 7, 3, 1, 0)
|
set_ship(my_field, 7, 3, 1, 0)
|
||||||
my_buttons = draw_field(window, my_field, 0)
|
my_buttons = draw_field(window, my_field, 0)
|
||||||
enemy_buttons = draw_field(window, enemy_field, 10)
|
enemy_buttons = draw_field(window, enemy_field, 11)
|
||||||
print(len(my_buttons))
|
print(len(my_buttons))
|
||||||
print(len(enemy_buttons))
|
print(len(enemy_buttons))
|
||||||
|
|
||||||
|
lbl = Label(window, text='', width=5, height=2)
|
||||||
|
lbl.grid(column=10, row=0)
|
||||||
|
|
||||||
window.mainloop()
|
window.mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue