2021-03-15 16:50:33 +01:00
|
|
|
from doctest import master
|
|
|
|
from tkinter import *
|
|
|
|
|
|
|
|
from PIL import Image, ImageTk
|
|
|
|
|
|
|
|
WINDOW_X = 1100
|
|
|
|
WINDOW_Y = 540
|
|
|
|
FRAME_WIDTH = 533
|
|
|
|
FRAME_HEIGHT = 533
|
2021-03-19 17:45:28 +01:00
|
|
|
|
|
|
|
# Size of small image
|
2021-03-15 16:50:33 +01:00
|
|
|
IMAGE_SIZE = 50
|
2021-03-19 17:45:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
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.arrayPosition = [[0] * field.cows] * field.cows
|
|
|
|
|
|
|
|
def Moving(self, event):
|
|
|
|
# Moving
|
|
|
|
if event.keysym == "Right":
|
|
|
|
if self.current_x + self.step < FRAME_WIDTH:
|
|
|
|
self.current_x += self.step
|
|
|
|
canvas.delete('all')
|
|
|
|
field.Fill()
|
|
|
|
self.Rectangle()
|
|
|
|
elif event.keysym == "Left":
|
|
|
|
if self.current_x - self.step >= self.x_start:
|
|
|
|
self.current_x -= self.step
|
|
|
|
canvas.delete('all')
|
|
|
|
field.Fill()
|
|
|
|
self.Rectangle()
|
|
|
|
elif event.keysym == "Up":
|
|
|
|
if self.current_y - self.step >= self.y_start:
|
|
|
|
self.current_y -= self.step
|
|
|
|
canvas.delete('all')
|
|
|
|
field.Fill()
|
|
|
|
self.Rectangle()
|
|
|
|
elif event.keysym == "Down":
|
|
|
|
if self.current_y + self.step < FRAME_HEIGHT:
|
|
|
|
self.current_y += self.step
|
|
|
|
canvas.delete('all')
|
|
|
|
field.Fill()
|
|
|
|
self.Rectangle()
|
|
|
|
|
|
|
|
# Drawing rectangle
|
|
|
|
def Rectangle(self):
|
|
|
|
canvas.create_rectangle(self.current_x, self.current_y, self.current_x + self.step - 2,
|
|
|
|
self.current_y + self.step - 2, width=3, outline='red')
|
|
|
|
|
|
|
|
|
|
|
|
class Field(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.width = 533
|
|
|
|
self.height = 533
|
|
|
|
self.image_size = 50
|
|
|
|
self.rows = 10
|
|
|
|
self.cows = 10
|
|
|
|
self.x_start = 3
|
|
|
|
self.y_start = 3
|
|
|
|
|
|
|
|
# Putting images
|
|
|
|
def Fill(self):
|
|
|
|
x = self.x_start
|
|
|
|
y = self.y_start
|
|
|
|
for i in range(self.cows):
|
|
|
|
for j in range(self.rows):
|
|
|
|
canvas.create_image(x, y, anchor=NW, image=img)
|
|
|
|
x += self.image_size + self.x_start
|
|
|
|
y += self.image_size + self.y_start
|
|
|
|
x = self.x_start
|
2021-03-15 16:50:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2021-03-19 17:45:28 +01:00
|
|
|
# Creating the main window of an application
|
2021-03-15 16:50:33 +01:00
|
|
|
window_size = f'{WINDOW_X}x{WINDOW_Y}'
|
|
|
|
global window
|
|
|
|
window = Tk()
|
|
|
|
window.title("Sapper")
|
|
|
|
window.geometry(window_size)
|
|
|
|
|
2021-03-19 17:45:28 +01:00
|
|
|
# Creating the frame
|
2021-03-15 16:50:33 +01:00
|
|
|
frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=1)
|
|
|
|
frame.pack(anchor=NW)
|
|
|
|
|
2021-03-19 17:45:28 +01:00
|
|
|
# Creating the canvas
|
2021-03-15 16:50:33 +01:00
|
|
|
global canvas
|
|
|
|
canvas = Canvas(frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, bg='white')
|
|
|
|
canvas.pack()
|
|
|
|
|
2021-03-19 17:45:28 +01:00
|
|
|
# Loading image
|
2021-03-15 16:50:33 +01:00
|
|
|
global img
|
|
|
|
img = PhotoImage(file="../files/imgs/image.png")
|
|
|
|
|
2021-03-19 17:45:28 +01:00
|
|
|
# Creating objects
|
|
|
|
global player
|
|
|
|
global field
|
|
|
|
field = Field()
|
|
|
|
player = Player()
|
|
|
|
|
|
|
|
# Filling window with images
|
|
|
|
Field.Fill(field)
|
|
|
|
# Drawing rectangle (player)
|
|
|
|
Player.Rectangle(player)
|
|
|
|
# Binding keyboard press to function
|
|
|
|
window.bind("<Key>", player.Moving)
|
|
|
|
# Starting mainloop for window
|
2021-03-15 16:50:33 +01:00
|
|
|
window.mainloop()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|