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
FRAME_WIDTH = 533
FRAME_HEIGHT = 533
# Size of small image
IMAGE_SIZE = 50
X_START = Y_START = 3
STEP = IMAGE_SIZE + X_START
current_x = 3
current_y = 3
def Rectangle():
global current_x
global current_y
canvas.create_rectangle(current_x, current_y, current_x + STEP - 2, current_y + STEP - 2, width=3, outline='red')
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
# t_x = current_x - STEP
# t_y = current_y - STEP
# canvas.create_rectangle(t_x, t_y, STEP, STEP, width=3, outline='white')
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()
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():
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
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
def Moving(event):
global current_x
global current_y
if event.keysym == "Right":
if current_x + STEP < FRAME_WIDTH:
current_x += STEP
canvas.delete('all')
Field()
Rectangle()
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()
# 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():
# This creates the main window of an application
# 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")
# 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
# Creating objects
global player
global field
field = Field()
player = Player()
# canvas.create_rectangle(X_START, Y_START, X_START + IMAGE_SIZE, Y_START + IMAGE_SIZE, width=3, outline='red')
# app = Sapper.
# global current_x
# global current_y
# current_x = 3
# current_y = 3
Field()
Rectangle()
window.bind("<Key>", Moving)
# 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()
# moving(window)
# window.mainloop()
if __name__ == '__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()