Global refactor

This commit is contained in:
Andrzej 2021-03-19 17:45:28 +01:00
parent 9a0ecd3302
commit ae7b1b699f
2 changed files with 192 additions and 74 deletions

View File

@ -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') 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()
window.bind("<Key>", Moving) # 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')
def Field(): class Field(object):
x = X_START def __init__(self):
y = Y_START self.width = 533
for i in range(10): self.height = 533
for j in range(10): self.image_size = 50
canvas.create_image(x, y, anchor=NW, image=img) self.rows = 10
x += IMAGE_SIZE + X_START self.cows = 10
y += IMAGE_SIZE + Y_START self.x_start = 3
x = X_START self.y_start = 3
# Putting images
def Moving(event): def Fill(self):
global current_x x = self.x_start
global current_y y = self.y_start
if event.keysym == "Right": for i in range(self.cows):
if current_x + STEP < FRAME_WIDTH: for j in range(self.rows):
current_x += STEP canvas.create_image(x, y, anchor=NW, image=img)
canvas.delete('all') x += self.image_size + self.x_start
Field() y += self.image_size + self.y_start
Rectangle() x = self.x_start
elif event.keysym == "Left":
if current_x - STEP >= X_START:
current_x -= STEP
canvas.delete('all')
Field()
Rectangle()
elif event.keysym == "Up":
if current_y - STEP >= Y_START:
current_y -= STEP
canvas.delete('all')
Field()
Rectangle()
elif event.keysym == "Down":
if current_y + STEP < FRAME_HEIGHT:
current_y += STEP
canvas.delete('all')
Field()
Rectangle()
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
View 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()