Global refactor
This commit is contained in:
parent
9a0ecd3302
commit
ae7b1b699f
141
bin/main.py
141
bin/main.py
@ -7,110 +7,111 @@ WINDOW_X = 1100
|
|||||||
WINDOW_Y = 540
|
WINDOW_Y = 540
|
||||||
FRAME_WIDTH = 533
|
FRAME_WIDTH = 533
|
||||||
FRAME_HEIGHT = 533
|
FRAME_HEIGHT = 533
|
||||||
|
|
||||||
|
# Size of small image
|
||||||
IMAGE_SIZE = 50
|
IMAGE_SIZE = 50
|
||||||
X_START = Y_START = 3
|
|
||||||
STEP = IMAGE_SIZE + X_START
|
|
||||||
|
|
||||||
current_x = 3
|
|
||||||
current_y = 3
|
|
||||||
|
|
||||||
|
|
||||||
def Rectangle():
|
class Player(object):
|
||||||
global current_x
|
def __init__(self):
|
||||||
global current_y
|
self.x_start = 3
|
||||||
canvas.create_rectangle(current_x, current_y, current_x + STEP - 2, current_y + STEP - 2, width=3, outline='red')
|
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
|
||||||
|
|
||||||
# t_x = current_x - STEP
|
def Moving(self, event):
|
||||||
# t_y = current_y - STEP
|
# Moving
|
||||||
# canvas.create_rectangle(t_x, t_y, STEP, STEP, width=3, outline='white')
|
|
||||||
|
|
||||||
window.bind("<Key>", Moving)
|
|
||||||
|
|
||||||
|
|
||||||
def Field():
|
|
||||||
x = X_START
|
|
||||||
y = Y_START
|
|
||||||
for i in range(10):
|
|
||||||
for j in range(10):
|
|
||||||
canvas.create_image(x, y, anchor=NW, image=img)
|
|
||||||
x += IMAGE_SIZE + X_START
|
|
||||||
y += IMAGE_SIZE + Y_START
|
|
||||||
x = X_START
|
|
||||||
|
|
||||||
|
|
||||||
def Moving(event):
|
|
||||||
global current_x
|
|
||||||
global current_y
|
|
||||||
if event.keysym == "Right":
|
if event.keysym == "Right":
|
||||||
if current_x + STEP < FRAME_WIDTH:
|
if self.current_x + self.step < FRAME_WIDTH:
|
||||||
current_x += STEP
|
self.current_x += self.step
|
||||||
canvas.delete('all')
|
canvas.delete('all')
|
||||||
Field()
|
field.Fill()
|
||||||
Rectangle()
|
self.Rectangle()
|
||||||
elif event.keysym == "Left":
|
elif event.keysym == "Left":
|
||||||
if current_x - STEP >= X_START:
|
if self.current_x - self.step >= self.x_start:
|
||||||
current_x -= STEP
|
self.current_x -= self.step
|
||||||
canvas.delete('all')
|
canvas.delete('all')
|
||||||
Field()
|
field.Fill()
|
||||||
Rectangle()
|
self.Rectangle()
|
||||||
elif event.keysym == "Up":
|
elif event.keysym == "Up":
|
||||||
if current_y - STEP >= Y_START:
|
if self.current_y - self.step >= self.y_start:
|
||||||
current_y -= STEP
|
self.current_y -= self.step
|
||||||
canvas.delete('all')
|
canvas.delete('all')
|
||||||
Field()
|
field.Fill()
|
||||||
Rectangle()
|
self.Rectangle()
|
||||||
elif event.keysym == "Down":
|
elif event.keysym == "Down":
|
||||||
if current_y + STEP < FRAME_HEIGHT:
|
if self.current_y + self.step < FRAME_HEIGHT:
|
||||||
current_y += STEP
|
self.current_y += self.step
|
||||||
canvas.delete('all')
|
canvas.delete('all')
|
||||||
Field()
|
field.Fill()
|
||||||
Rectangle()
|
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
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# This creates the main window of an application
|
# Creating the main window of an application
|
||||||
window_size = f'{WINDOW_X}x{WINDOW_Y}'
|
window_size = f'{WINDOW_X}x{WINDOW_Y}'
|
||||||
global window
|
global window
|
||||||
window = Tk()
|
window = Tk()
|
||||||
window.title("Sapper")
|
window.title("Sapper")
|
||||||
window.geometry(window_size)
|
window.geometry(window_size)
|
||||||
|
|
||||||
|
# Creating the frame
|
||||||
frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=1)
|
frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=1)
|
||||||
frame.pack(anchor=NW)
|
frame.pack(anchor=NW)
|
||||||
|
|
||||||
|
# Creating the canvas
|
||||||
global canvas
|
global canvas
|
||||||
canvas = Canvas(frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, bg='white')
|
canvas = Canvas(frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, bg='white')
|
||||||
canvas.pack()
|
canvas.pack()
|
||||||
|
|
||||||
|
# Loading image
|
||||||
global img
|
global img
|
||||||
img = PhotoImage(file="../files/imgs/image.png")
|
img = PhotoImage(file="../files/imgs/image.png")
|
||||||
|
|
||||||
# x = X_START
|
# Creating objects
|
||||||
# y = Y_START
|
global player
|
||||||
# for i in range(10):
|
global field
|
||||||
# for j in range(10):
|
field = Field()
|
||||||
# canvas.create_image(x, y, anchor=NW, image=img)
|
player = Player()
|
||||||
# x += IMAGE_SIZE + X_START
|
|
||||||
# y += IMAGE_SIZE + Y_START
|
|
||||||
# x = X_START
|
|
||||||
|
|
||||||
# canvas.create_rectangle(X_START, Y_START, X_START + IMAGE_SIZE, Y_START + IMAGE_SIZE, width=3, outline='red')
|
# Filling window with images
|
||||||
|
Field.Fill(field)
|
||||||
# app = Sapper.
|
# Drawing rectangle (player)
|
||||||
|
Player.Rectangle(player)
|
||||||
# global current_x
|
# Binding keyboard press to function
|
||||||
# global current_y
|
window.bind("<Key>", player.Moving)
|
||||||
# current_x = 3
|
# Starting mainloop for window
|
||||||
# current_y = 3
|
|
||||||
|
|
||||||
Field()
|
|
||||||
Rectangle()
|
|
||||||
window.bind("<Key>", Moving)
|
|
||||||
window.mainloop()
|
window.mainloop()
|
||||||
|
|
||||||
# moving(window)
|
|
||||||
# window.mainloop()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
117
bin/test.py
Normal file
117
bin/test.py
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
from doctest import master
|
||||||
|
from tkinter import *
|
||||||
|
|
||||||
|
from PIL import Image, ImageTk
|
||||||
|
|
||||||
|
WINDOW_X = 1100
|
||||||
|
WINDOW_Y = 540
|
||||||
|
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.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
|
||||||
|
|
||||||
|
|
||||||
|
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.geometry(window_size)
|
||||||
|
|
||||||
|
# Creating the frame
|
||||||
|
frame = Frame(master, width=FRAME_WIDTH, height=FRAME_HEIGHT, bd=1)
|
||||||
|
frame.pack(anchor=NW)
|
||||||
|
|
||||||
|
# Creating the canvas
|
||||||
|
global canvas
|
||||||
|
canvas = Canvas(frame, width=FRAME_WIDTH, height=FRAME_HEIGHT, bg='white')
|
||||||
|
canvas.pack()
|
||||||
|
|
||||||
|
# Loading image
|
||||||
|
global img
|
||||||
|
img = PhotoImage(file="../files/imgs/image.png")
|
||||||
|
|
||||||
|
# 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
|
||||||
|
window.mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
Loading…
Reference in New Issue
Block a user