New change in Ship Battle
This commit is contained in:
parent
7aaeeb0adc
commit
3e7ee1386a
|
@ -1,4 +1,9 @@
|
||||||
from tkinter import *
|
from tkinter import *
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
field_size = 10
|
||||||
|
|
||||||
|
buttons = []
|
||||||
|
|
||||||
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
|
@ -11,102 +16,84 @@ empty_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
||||||
|
|
||||||
|
|
||||||
my_field = list(empty_field)
|
my_field = list(empty_field)
|
||||||
enemy_field = list(empty_field)
|
enemy_field = list(empty_field)
|
||||||
|
|
||||||
|
|
||||||
|
class ShootResult(Enum):
|
||||||
|
Empty = "EMPTY"
|
||||||
|
Damaged = "DAMAGED"
|
||||||
|
Killed = "KILLED"
|
||||||
|
Undefined = "UNDEFINED"
|
||||||
|
|
||||||
|
|
||||||
def set_ship(row, col, size, direction):
|
def set_ship(row, col, size, direction):
|
||||||
# size: 1, 2, 3, 4
|
if row < 0 or row > field_size - 1:
|
||||||
# direction: вниз(0), вправо(1)
|
|
||||||
|
|
||||||
field_size = 10
|
|
||||||
|
|
||||||
if row < 0 or row > field_size -1:
|
|
||||||
return
|
return
|
||||||
|
if col < 0 or col > field_size - 1:
|
||||||
if col < 0 or col > field_size -1:
|
|
||||||
return
|
return
|
||||||
|
|
||||||
index = row * field_size *col
|
index = row * field_size *col
|
||||||
|
|
||||||
if direction == 0:
|
if direction == 0:
|
||||||
if field_size - row < size:
|
if field_size - row < size:
|
||||||
return
|
return
|
||||||
for r in range(row, row + size):
|
for r in range(row, row + size):
|
||||||
index = r * field_size + col
|
index = r * field_size + col
|
||||||
my_field[index] = '1'
|
my_field[index] = '1'
|
||||||
|
|
||||||
if direction == 1:
|
if direction == 1:
|
||||||
if field_size - col < size:
|
if field_size - col < size:
|
||||||
return
|
return
|
||||||
for c in range (col, col + size):
|
for c in range (col, col + size):
|
||||||
index = row * field_size + c
|
index = row * field_size + c
|
||||||
my_field[index] = '1'
|
my_field[index] = "1"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def shoot(field, row, col):
|
||||||
|
if row < 0 or row > field_size - 1:
|
||||||
|
return
|
||||||
|
if col < 0 or col > field_size - 1:
|
||||||
|
return
|
||||||
|
index = row * field_size *col
|
||||||
|
if field[index] == "":
|
||||||
|
field[index] = 0
|
||||||
|
return ShootResult.Empty
|
||||||
|
elif field[index] == "1":
|
||||||
|
field[index] = "\\"
|
||||||
|
return ShootResult.Damaged
|
||||||
|
elif field[index] == "1" or field[index] == "\\" or field[index] == "X":
|
||||||
|
return ShootResult.Damaged
|
||||||
|
|
||||||
|
|
||||||
def draw_field(window, field):
|
def draw_field(window, field):
|
||||||
canvas = Canvas(window, width=500, height=500)
|
for r in range(0, field_size):
|
||||||
canvas.pack()
|
for c in range(0, field_size):
|
||||||
|
bg = 'white'
|
||||||
|
index = r * field_size +c
|
||||||
|
if field[index] == '1':
|
||||||
|
bg = 'pink'
|
||||||
|
btn = Button(window, text="", bg=bg, width=5, height=2)
|
||||||
|
btn.grid(column=c, row=r)
|
||||||
|
buttons.append(btn)
|
||||||
|
|
||||||
square_size = 50
|
def colorize(field, buttons):
|
||||||
|
bg = "grey"
|
||||||
for row in range(10):
|
for i in range(len(field)):
|
||||||
for col in range(10):
|
if i == '1':
|
||||||
if (row + col) % 2 == 0:
|
bg = 'pink'
|
||||||
color = 'white'
|
elif i == '\\':
|
||||||
else:
|
bg = 'red'
|
||||||
color = 'blue'
|
elif i == '0':
|
||||||
if my_field[row*10 + col] == '1':
|
bg = 'black'
|
||||||
color = 'red'
|
buttons.configure(bg=bg)
|
||||||
x1 = col * square_size
|
|
||||||
y1 = row * square_size
|
|
||||||
x2 = x1 * square_size
|
|
||||||
y2 = y1 * square_size
|
|
||||||
canvas.create_rectangle(x1, y1, x2, y2, fill=color)
|
|
||||||
|
|
||||||
|
|
||||||
window = Tk()
|
window = Tk()
|
||||||
window.title("Ship Craft!")
|
window.title("Ship Craft!")
|
||||||
window.geometry('800x600')
|
window.geometry('450x410')
|
||||||
|
|
||||||
set_ship(1, 1, 4, 1)
|
set_ship(1, 1, 4, 1)
|
||||||
|
set_ship(0, 6, 3, 0)
|
||||||
|
set_ship(7, 3, 1, 0)
|
||||||
|
|
||||||
draw_field(window, my_field)
|
draw_field(window, my_field)
|
||||||
|
|
||||||
window.mainloop()
|
window.mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# set_ship(1, 1, 4, 1)
|
|
||||||
step1_field = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', '1', '1', '1', '1', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
|
||||||
|
|
||||||
# set_ship(0, 5, 3, 0)
|
|
||||||
step2_field = [' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', '1', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
||||||
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
|
|
||||||
|
|
Loading…
Reference in New Issue