Splitted to files

This commit is contained in:
Andrzej 2021-04-02 03:39:46 +02:00
parent 5f95603626
commit ad1681e561
15 changed files with 360 additions and 228 deletions

77
bin/Classess/Field.py Normal file
View File

@ -0,0 +1,77 @@
from doctest import master
from tkinter import *
# from bin.main import PlayerReturn as player
# from bin.main import DrawingLargeImage
WINDOW_X = 533 + 1200
WINDOW_Y = 950
FRAME_WIDTH = 533
FRAME_HEIGHT = 533
# Size of small image
IMAGE_SIZE = 50
step = IMAGE_SIZE + 3
class Field(object):
def __init__(self):
self.win = Tk()
self.width = 533
self.height = 533
self.image_size = 50
self.rows = 10
self.columns = 10
self.x_start = 3
self.y_start = 3
self.state_of_cell_array = [[0 for i in range(3)] for j in range(200)]
self.small_image_array = [[0 for i in range(self.rows)] for j in range(self.columns)]
self.large_image_array = [[0 for i in range(self.rows)] for j in range(self.columns)]
self.main_frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=0)
self.main_frame.pack(anchor=NW)
self.small_field_canvas = Canvas(self.main_frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, highlightthickness=0,
bg='light gray')
self.small_field_canvas.pack()
self.large_image_canvas = Canvas(self.win, width=WINDOW_X - 533 - 20, height=900, highlightthickness=0,
bg='gray')
self.large_image_canvas.place(x=FRAME_WIDTH + 5, y=3)
# # Putting images
# def Fill(self):
# x = self.x_start
# y = self.y_start
#
# row = 0
# column = 0
#
# # Drawing small images
# for i in range(self.columns):
# for j in range(self.rows):
# small_image_name = self.small_image_array[column][row]
#
# self.small_field_canvas.image = small_image_name
# self.small_field_canvas.create_image(x, y, anchor=NW, image=small_image_name)
#
# x += self.image_size + self.x_start
# row += 1
# y += self.image_size + self.y_start
# x = self.x_start
# column += 1
# row = 0
#
# # Drawing red/green rectangles
# for el in self.state_of_cell_array:
# if el[0] != 0:
# self.small_field_canvas.create_rectangle(el[0], el[1], el[0] + step - 2,
# el[1] + step - 2, width=3, outline=el[2])
#
# DrawingLargeImage()
# self.DrawingLargeImage()
# def DrawingLargeImage(self):
# large_img_name = self.large_image_array[player.current_array_y][player.current_array_x]
#
# self.large_image_canvas.image = large_img_name
# self.large_image_canvas.create_image(0, 0, anchor=NW, image=large_img_name)

78
bin/Classess/Player.py Normal file
View File

@ -0,0 +1,78 @@
import sys
WINDOW_X = 533 + 1200
WINDOW_Y = 950
FRAME_WIDTH = 533
FRAME_HEIGHT = 533
# Size of small image
IMAGE_SIZE = 50
class Player(object):
def __init__(self):
self.x_start = 3
self.y_start = 3
self.current_x = self.x_start
self.current_y = self.y_start
self.step = IMAGE_SIZE + self.x_start
self.current_array_x = 0
self.current_array_y = 0
# def Moving(self, event):
# # Moving
# if event.keysym == "Right":
# if self.current_x + self.step < FRAME_WIDTH:
# self.current_x += self.step
# field.small_field_canvas.delete('all')
# field.large_image_canvas.delete('all')
# self.current_array_x += 1
# field.Fill()
# self.Rectangle()
# elif self.current_y + self.step < FRAME_HEIGHT:
# self.current_x = self.x_start
# field.small_field_canvas.delete('all')
# field.large_image_canvas.delete('all')
# self.current_array_x = 0
# self.current_array_y += 1
# self.current_y += self.step
# field.Fill()
# self.Rectangle()
# elif event.keysym == "Left":
# if self.current_x - self.step >= self.x_start:
# self.current_x -= self.step
# field.small_field_canvas.delete('all')
# field.large_image_canvas.delete('all')
# self.current_array_x -= 1
# field.Fill()
# self.Rectangle()
# elif self.current_y - self.step >= self.y_start:
# self.current_x = FRAME_WIDTH - self.step
# field.small_field_canvas.delete('all')
# field.large_image_canvas.delete('all')
# self.current_array_x = 9
# self.current_array_y -= 1
# self.current_y -= self.step
# field.Fill()
# self.Rectangle()
# elif event.keysym == "Up":
# if self.current_y - self.step >= self.y_start:
# self.current_y -= self.step
# field.small_field_canvas.delete('all')
# field.large_image_canvas.delete('all')
# self.current_array_y -= 1
# field.Fill()
# self.Rectangle()
# elif event.keysym == "Down":
# if self.current_y + self.step < FRAME_HEIGHT:
# self.current_y += self.step
# field.small_field_canvas.delete('all')
# field.large_image_canvas.delete('all')
# self.current_array_y += 1
# field.Fill()
# self.Rectangle()
# # Drawing rectangle
# def Rectangle(self):
# field.small_field_canvas.create_rectangle(self.current_x, self.current_y, self.current_x + self.step - 2,
# self.current_y + self.step - 2, width=3, outline='blue2')

0
bin/Classess/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

23
bin/Classess/test_2.py Normal file
View File

@ -0,0 +1,23 @@
# from bin.test_1 import fun as pl
#
# print(pl())
from bin.Main.main import ex
from bin.Main.main import t
print(ex.ret(t))
# class exmp:
# def __init__(self):
# self.width = 533
# self.height = 533
# self.image_size = 50
# self.rows = 10
# self.columns = 10
# self.x_start = 3
# self.y_start = 3
# self.state_of_cell_array = [[0 for i in range(3)] for j in range(200)]
# self.small_image_array = [[0 for i in range(self.rows)] for j in range(self.columns)]
# self.large_image_array = [[0 for i in range(self.rows)] for j in range(self.columns)]

0
bin/Main/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

182
bin/Main/main.py Normal file
View File

@ -0,0 +1,182 @@
from tkinter import *
import os
from bin.Classess.Player import Player
from bin.Classess.Field import Field
WINDOW_X = 533 + 1200
WINDOW_Y = 950
FRAME_WIDTH = 533
FRAME_HEIGHT = 533
# Size of small image
IMAGE_SIZE = 50
# Creating objects
player = Player()
field = Field()
# Putting images
def Fill():
x = field.x_start
y = field.y_start
row = 0
column = 0
# Drawing small images
for i in range(field.columns):
for j in range(field.rows):
small_image_name = field.small_image_array[column][row]
field.small_field_canvas.image = small_image_name
field.small_field_canvas.create_image(x, y, anchor=NW, image=small_image_name)
x += field.image_size + field.x_start
row += 1
y += field.image_size + field.y_start
x = field.x_start
column += 1
row = 0
# Drawing red/green rectangles
for el in field.state_of_cell_array:
if el[0] != 0:
field.small_field_canvas.create_rectangle(el[0], el[1], el[0] + player.step - 2,
el[1] + player.step - 2, width=3, outline=el[2])
DrawingLargeImage()
def DrawingLargeImage():
large_img_name = field.large_image_array[player.current_array_y][player.current_array_x]
field.large_image_canvas.image = large_img_name
field.large_image_canvas.create_image(0, 0, anchor=NW, image=large_img_name)
# Drawing rectangle
def Rectangle():
field.small_field_canvas.create_rectangle(player.current_x, player.current_y, player.current_x + player.step - 2,
player.current_y + player.step - 2, width=3, outline='blue2')
def Moving(event):
# Moving
if event.keysym == "Right":
if player.current_x + player.step < FRAME_WIDTH:
player.current_x += player.step
field.small_field_canvas.delete('all')
field.large_image_canvas.delete('all')
player.current_array_x += 1
Fill()
Rectangle()
elif player.current_y + player.step < FRAME_HEIGHT:
player.current_x = player.x_start
field.small_field_canvas.delete('all')
field.large_image_canvas.delete('all')
player.current_array_x = 0
player.current_array_y += 1
player.current_y += player.step
Fill()
Rectangle()
elif event.keysym == "Left":
if player.current_x - player.step >= player.x_start:
player.current_x -= player.step
field.small_field_canvas.delete('all')
field.large_image_canvas.delete('all')
player.current_array_x -= 1
Fill()
Rectangle()
elif player.current_y - player.step >= player.y_start:
player.current_x = FRAME_WIDTH - player.step
field.small_field_canvas.delete('all')
field.large_image_canvas.delete('all')
player.current_array_x = 9
player.current_array_y -= 1
player.current_y -= player.step
Fill()
Rectangle()
elif event.keysym == "Up":
if player.current_y - player.step >= player.y_start:
player.current_y -= player.step
field.small_field_canvas.delete('all')
field.large_image_canvas.delete('all')
player.current_array_y -= 1
Fill()
Rectangle()
elif event.keysym == "Down":
if player.current_y + player.step < FRAME_HEIGHT:
player.current_y += player.step
field.small_field_canvas.delete('all')
field.large_image_canvas.delete('all')
player.current_array_y += 1
Fill()
Rectangle()
def ImagesInArray(directory, array):
# Filling array from directory
row = column = 0
for file in os.listdir(directory):
image_name = file
image_path = f"{directory}/{image_name}"
if directory == "../files/large_images":
image = PhotoImage(master=field.large_image_canvas, file=image_path)
else:
image = PhotoImage(master=field.small_field_canvas, file=image_path)
array[row][column] = image
column += 1
if column == 10:
column = 0
row += 1
def CellDesignation(array, color):
for element in array:
if element[0] == 0:
element[0] = player.current_x
element[1] = player.current_y
element[2] = color
break
def Action(event):
if event.keysym in ["Right", "Left", "Up", "Down"]:
Moving(event)
elif event.keysym in ["1", "2"]:
if event.keysym == "1":
CellDesignation(field.state_of_cell_array, "red")
else:
CellDesignation(field.state_of_cell_array, "green")
def main():
# Creating the main window of an application
win_size = f'{WINDOW_X}x{WINDOW_Y}'
# global win
# win = Tk()
field.win.title("Sapper")
field.win.configure(bg='gray')
field.win.geometry(win_size)
# Filling image arrays
large_directory = "../../files/large_images"
ImagesInArray(large_directory, field.large_image_array)
small_directory = "../../files/small_images"
ImagesInArray(small_directory, field.small_image_array)
# Filling window with images
Fill()
# Drawing rectangle (player)
Rectangle()
# Binding keyboard press to function
field.win.bind("<Key>", Action)
# Starting mainloop for window
field.win.mainloop()
if __name__ == '__main__':
main()

0
bin/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

View File

@ -1,228 +0,0 @@
from doctest import master
from tkinter import *
import os
from PIL import Image, ImageTk
WINDOW_X = 533 + 1200
WINDOW_Y = 950
FRAME_WIDTH = 533
FRAME_HEIGHT = 533
# Size of small image
IMAGE_SIZE = 50
class Player(object):
def __init__(self):
self.x_start = 3
self.y_start = 3
self.current_x = self.x_start
self.current_y = self.y_start
self.step = IMAGE_SIZE + self.x_start
self.current_array_x = 0
self.current_array_y = 0
def Moving(self, event):
# Moving
if event.keysym == "Right":
if self.current_x + self.step < FRAME_WIDTH:
self.current_x += self.step
field_canvas.delete('all')
large_image_canvas.delete('all')
self.current_array_x += 1
field.Fill()
self.Rectangle()
elif self.current_y + self.step < FRAME_HEIGHT:
self.current_x = self.x_start
field_canvas.delete('all')
large_image_canvas.delete('all')
self.current_array_x = 0
self.current_array_y += 1
self.current_y += self.step
field.Fill()
self.Rectangle()
elif event.keysym == "Left":
if self.current_x - self.step >= self.x_start:
self.current_x -= self.step
field_canvas.delete('all')
large_image_canvas.delete('all')
self.current_array_x -= 1
field.Fill()
self.Rectangle()
elif self.current_y - self.step >= self.y_start:
self.current_x = FRAME_WIDTH - self.step
field_canvas.delete('all')
large_image_canvas.delete('all')
self.current_array_x = 9
self.current_array_y -= 1
self.current_y -= self.step
field.Fill()
self.Rectangle()
elif event.keysym == "Up":
if self.current_y - self.step >= self.y_start:
self.current_y -= self.step
field_canvas.delete('all')
large_image_canvas.delete('all')
self.current_array_y -= 1
field.Fill()
self.Rectangle()
elif event.keysym == "Down":
if self.current_y + self.step < FRAME_HEIGHT:
self.current_y += self.step
field_canvas.delete('all')
large_image_canvas.delete('all')
self.current_array_y += 1
field.Fill()
self.Rectangle()
# Drawing rectangle
def Rectangle(self):
field_canvas.create_rectangle(self.current_x, self.current_y, self.current_x + self.step - 2,
self.current_y + self.step - 2, width=3, outline='blue2')
def DrawingLargeImage():
large_img_name = large_image_array[player.current_array_y][player.current_array_x]
large_image_canvas.image = large_img_name
large_image_canvas.create_image(0, 0, anchor=NW, image=large_img_name)
class Field(object):
def __init__(self):
self.width = 533
self.height = 533
self.image_size = 50
self.rows = 10
self.columns = 10
self.x_start = 3
self.y_start = 3
# Putting images
def Fill(self):
x = self.x_start
y = self.y_start
row = 0
column = 0
# Drawing small images
for i in range(self.columns):
for j in range(self.rows):
small_image_name = small_image_array[column][row]
field_canvas.image = small_image_name
field_canvas.create_image(x, y, anchor=NW, image=small_image_name)
x += self.image_size + self.x_start
row += 1
y += self.image_size + self.y_start
x = self.x_start
column += 1
row = 0
# Drawing red/green rectangles
for el in state_of_cell_array:
if el[0] != 0:
field_canvas.create_rectangle(el[0], el[1], el[0] + player.step - 2,
el[1] + player.step - 2, width=3, outline=el[2])
DrawingLargeImage()
def ImagesInArray(directory, array):
# Filling array from directory
row = column = 0
for file in os.listdir(directory):
image_name = file
image_path = f"{directory}/{image_name}"
image = PhotoImage(file=image_path)
array[row][column] = image
column += 1
if column == 10:
column = 0
row += 1
def CellDesignation(array, color):
for element in array:
if element[0] == 0:
element[0] = player.current_x
element[1] = player.current_y
element[2] = color
break
def Action(event):
global state_of_cell_array
if event.keysym in ["Right", "Left", "Up", "Down"]:
player.Moving(event)
elif event.keysym in ["1", "2"]:
if event.keysym == "1":
CellDesignation(state_of_cell_array, "red")
else:
CellDesignation(state_of_cell_array, "green")
def main():
# Creating the main window of an application
window_size = f'{WINDOW_X}x{WINDOW_Y}'
global window
window = Tk()
window.title("Sapper")
window.configure(bg='gray')
window.geometry(window_size)
global state_of_cell_array
state_of_cell_array = [[0 for i in range(3)] for j in range(200)]
# Creating objects
global player
global field
field = Field()
player = Player()
# Creating arrays with names of images
global large_image_array
global small_image_array
small_image_array = [[0 for i in range(field.rows)] for j in range(field.columns)]
large_image_array = [[0 for i in range(field.rows)] for j in range(field.columns)]
# Filling image arrays
global large_directory
global small_directory
large_directory = "../files/large_images"
ImagesInArray(large_directory, large_image_array)
small_directory = "../files/small_images"
ImagesInArray(small_directory, small_image_array)
# Creating the frames
main_frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=0)
main_frame.pack(anchor=NW)
# Creating the canvas
global field_canvas
field_canvas = Canvas(main_frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, highlightthickness=0, bg='light gray')
field_canvas.pack()
# Creating the canvas for large image
global large_image_canvas
large_image_canvas = Canvas(window, width=WINDOW_X - 533 - 20, height=900, highlightthickness=0, bg='gray')
large_image_canvas.place(x=FRAME_WIDTH + 5, y=player.y_start)
# Filling window with images
Field.Fill(field)
# Drawing rectangle (player)
Player.Rectangle(player)
# Binding keyboard press to function
window.bind("<Key>", Action)
# Starting mainloop for window
window.mainloop()
if __name__ == '__main__':
main()