Auto moving

This commit is contained in:
Andrzej 2021-04-29 18:07:47 +02:00
parent cf6bcebf1f
commit a170012828
8 changed files with 35 additions and 25 deletions

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (venv)" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" /> <content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.7 (venv)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -1,5 +1,6 @@
import os import os
import random import random
import time
from tkinter import * from tkinter import *
from bin.Classess.Field import Field from bin.Classess.Field import Field
@ -17,6 +18,8 @@ IMAGE_SIZE = 50
AMOUNT_OF_MINES = 10 AMOUNT_OF_MINES = 10
DELAY_TIME = 0.5
# Creating objects # Creating objects
player = Player() player = Player()
field = Field() field = Field()
@ -60,7 +63,7 @@ def DrawingLargeImage():
field.PuttingLargeImage(large_img_name) field.PuttingLargeImage(large_img_name)
def Next_direction(side): def NextDirection(action):
# Define next direction # Define next direction
current_direction = player.direction current_direction = player.direction
t = -1 t = -1
@ -70,9 +73,9 @@ def Next_direction(side):
break break
# Write next direction to Player # Write next direction to Player
if side == "Right": if action == "Right":
player.direction = player.directions[(t + 1) % 4] player.direction = player.directions[(t + 1) % 4]
elif side == "Left": elif action == "Left":
player.direction = player.directions[(t - 1) % 4] player.direction = player.directions[(t - 1) % 4]
return player.direction return player.direction
@ -89,31 +92,26 @@ def MovingForward():
field.small_field_canvas.move(player.image_canvas_id, 0, player.step) field.small_field_canvas.move(player.image_canvas_id, 0, player.step)
def Moving(event): def Moving(action):
# Moving # Moving
if event.keysym == "Right": if action == "Right":
# player.MovingRight() # player.MovingRight()
field.Moving() field.Moving()
Fill(False) Fill(False)
next_direction = Next_direction(event.keysym) next_direction = NextDirection(action)
Arrow(next_direction) Arrow(next_direction)
elif event.keysym == "Left": elif action == "Left":
# player.MovingLeft() # player.MovingLeft()
field.Moving() field.Moving()
Fill(False) Fill(False)
next_direction = Next_direction(event.keysym) next_direction = NextDirection(action)
Arrow(next_direction) Arrow(next_direction)
elif event.keysym == "Up": elif action == "Up":
player.Moving() player.Moving()
field.Moving() field.Moving()
Fill(False) Fill(False)
MovingForward() MovingForward()
Arrow(player.direction) Arrow(player.direction)
# elif event.keysym == "space":
# player.MovingDown()
# field.Moving()
# Fill()
# Arrow(player.arrow_south_image)
def ImagesInArray(directory, array): def ImagesInArray(directory, array):
@ -153,11 +151,11 @@ def CellDesignation(array, color):
break break
def Action(event): def Action(action):
if event.keysym in ["Right", "Left", "Up", "space"]: if action in ["Right", "Left", "Up", "space"]:
Moving(event) Moving(action)
elif event.keysym in ["1", "2"]: elif action in ["1", "2"]:
if event.keysym == "1": if action == "1":
CellDesignation(field.state_of_cell_array, "red") CellDesignation(field.state_of_cell_array, "red")
else: else:
CellDesignation(field.state_of_cell_array, "green") CellDesignation(field.state_of_cell_array, "green")
@ -184,15 +182,15 @@ def MouseClickEvent(event):
start_position = field.small_field_canvas.coords(player.image_canvas_id) start_position = field.small_field_canvas.coords(player.image_canvas_id)
end_position = [] end_position = []
print("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1])) # print("Pierwsza pozycja: {} {}".format(start_position[0], start_position[1]))
for i in range(0, len(field.canvas_small_images)): for i in range(0, len(field.canvas_small_images)):
img_coords = field.small_field_canvas.coords(field.canvas_small_images[i]) img_coords = field.small_field_canvas.coords(field.canvas_small_images[i])
if (img_coords[0] <= event.x and event.x <= img_coords[0] + IMAGE_SIZE) and (img_coords[1] <= event.y and event.y <= img_coords[1] + IMAGE_SIZE): if (img_coords[0] <= event.x and event.x <= img_coords[0] + IMAGE_SIZE) and (img_coords[1] <= event.y and event.y <= img_coords[1] + IMAGE_SIZE):
end_position = img_coords end_position = img_coords
if len(end_position) == 2: # if len(end_position) == 2:
print("Koncowa pozycja: {} {}".format(end_position[0], end_position[1])) # print("Koncowa pozycja: {} {}".format(end_position[0], end_position[1]))
node = nd.Node() node = nd.Node()
if len(fringe) == 0: if len(fringe) == 0:
@ -230,6 +228,8 @@ def MouseClickEvent(event):
print(action_list) print(action_list)
# Start moving
AutoMove()
def PutMines(mines_array): def PutMines(mines_array):
@ -293,6 +293,16 @@ def MinesInArrays(mines_array, directory, imgs_array):
imgs_array[mines_array[i].array_x][mines_array[i].array_y] = temp_array[i] imgs_array[mines_array[i].array_x][mines_array[i].array_y] = temp_array[i]
def AutoMove():
for action in action_list:
# Program wait for better illustration
time.sleep(DELAY_TIME)
# Move once
Action(action)
# Update main window
field.win.update()
def main(): def main():
# Creating the main window of an application # Creating the main window of an application
win_size = f'{WINDOW_X}x{WINDOW_Y}' win_size = f'{WINDOW_X}x{WINDOW_Y}'
@ -336,7 +346,7 @@ def main():
# Rectangle(True, "None") # Rectangle(True, "None")
# Rectangle() # Rectangle()
# Binding keyboard press to function # Binding keyboard press to function
field.win.bind("<Key>", Action) # field.win.bind("<Key>", Action)
field.small_field_canvas.bind("<Button-1>", MouseClickEvent) field.small_field_canvas.bind("<Button-1>", MouseClickEvent)
# Starting mainloop for window # Starting mainloop for window
field.win.mainloop() field.win.mainloop()