Change way to moving

This commit is contained in:
Andrzej 2021-04-24 03:05:17 +02:00
parent 23f793a79f
commit 7113805f98
8 changed files with 91 additions and 26 deletions

View File

@ -16,26 +16,34 @@ class Player(object):
self.step = IMAGE_SIZE + self.x_start self.step = IMAGE_SIZE + self.x_start
self.current_array_x = 0 self.current_array_x = 0
self.current_array_y = 0 self.current_array_y = 0
self.direction = "east"
self.directions = ["north", "east", "south", "west"]
self.arrow_north_image = None
self.arrow_south_image = None
self.arrow_west_image = None
self.arrow_east_image = None
def MovingRight(self): def MovingRight(self):
if self.current_x + self.step < FRAME_WIDTH: if self.current_x + self.step < FRAME_WIDTH:
self.current_x += self.step self.current_x += self.step
self.current_array_x += 1 self.current_array_x += 1
elif self.current_y + self.step < FRAME_HEIGHT: # # Changes the line down
self.current_x = self.x_start # elif self.current_y + self.step < FRAME_HEIGHT:
self.current_array_x = 0 # self.current_x = self.x_start
self.current_array_y += 1 # self.current_array_x = 0
self.current_y += self.step # self.current_array_y += 1
# self.current_y += self.step
def MovingLeft(self): def MovingLeft(self):
if self.current_x - self.step >= self.x_start: if self.current_x - self.step >= self.x_start:
self.current_x -= self.step self.current_x -= self.step
self.current_array_x -= 1 self.current_array_x -= 1
elif self.current_y - self.step >= self.y_start: # # Changes the line up
self.current_x = FRAME_WIDTH - self.step # elif self.current_y - self.step >= self.y_start:
self.current_array_x = 9 # self.current_x = FRAME_WIDTH - self.step
self.current_array_y -= 1 # self.current_array_x = 9
self.current_y -= self.step # self.current_array_y -= 1
# self.current_y -= self.step
def MovingUp(self): def MovingUp(self):
if self.current_y - self.step >= self.y_start: if self.current_y - self.step >= self.y_start:
@ -46,3 +54,13 @@ class Player(object):
if self.current_y + self.step < FRAME_HEIGHT: if self.current_y + self.step < FRAME_HEIGHT:
self.current_y += self.step self.current_y += self.step
self.current_array_y += 1 self.current_array_y += 1
def Moving(self):
if self.direction == "north":
self.MovingUp()
if self.direction == "south":
self.MovingDown()
if self.direction == "west":
self.MovingLeft()
if self.direction == "east":
self.MovingRight()

View File

@ -1,10 +1,10 @@
import os
import random import random
from tkinter import * from tkinter import *
import os
from bin.classess.Player import Player
from bin.classess.Field import Field from bin.classess.Field import Field
from bin.classess.Mine import Mine from bin.classess.Mine import Mine
from bin.classess.Player import Player
WINDOW_X = 533 + 1200 WINDOW_X = 533 + 1200
WINDOW_Y = 950 WINDOW_Y = 950
@ -21,6 +21,20 @@ player = Player()
field = Field() field = Field()
def Arrow(direction):
image = ""
if direction == "north":
image = player.arrow_north_image
elif direction == "south":
image = player.arrow_south_image
elif direction == "west":
image = player.arrow_west_image
elif direction == "east":
image = player.arrow_east_image
field.small_field_canvas.create_image(player.current_x, player.current_y, anchor=NW, image=image)
# Putting images # Putting images
def Fill(): def Fill():
field.PuttingSmallImages() field.PuttingSmallImages()
@ -46,28 +60,48 @@ def Rectangle():
player.current_y + player.step - 2, width=3, outline='blue2') player.current_y + player.step - 2, width=3, outline='blue2')
def Next_direction(side):
# Define next direction
current_direction = player.direction
t = -1
for i in range(4):
if player.directions[i] == current_direction:
t = i
break
# Write next direction to Player
if side == "Right":
player.direction = player.directions[(t + 1) % 4]
elif side == "Left":
player.direction = player.directions[(t - 1) % 4]
return player.direction
def Moving(event): def Moving(event):
# Moving # Moving
if event.keysym == "Right": if event.keysym == "Right":
player.MovingRight() # player.MovingRight()
field.Moving() field.Moving()
Fill() Fill()
Rectangle() next_direction = Next_direction(event.keysym)
Arrow(next_direction)
elif event.keysym == "Left": elif event.keysym == "Left":
player.MovingLeft() # player.MovingLeft()
field.Moving() field.Moving()
Fill() Fill()
Rectangle() next_direction = Next_direction(event.keysym)
Arrow(next_direction)
elif event.keysym == "Up": elif event.keysym == "Up":
player.MovingUp() player.Moving()
field.Moving() field.Moving()
Fill() Fill()
Rectangle() Arrow(player.direction)
elif event.keysym == "Down": # elif event.keysym == "space":
player.MovingDown() # player.MovingDown()
field.Moving() # field.Moving()
Fill() # Fill()
Rectangle() # Arrow(player.arrow_south_image)
def ImagesInArray(directory, array): def ImagesInArray(directory, array):
@ -108,7 +142,7 @@ def CellDesignation(array, color):
def Action(event): def Action(event):
if event.keysym in ["Right", "Left", "Up", "Down"]: if event.keysym in ["Right", "Left", "Up", "space"]:
Moving(event) Moving(event)
elif event.keysym in ["1", "2"]: elif event.keysym in ["1", "2"]:
if event.keysym == "1": if event.keysym == "1":
@ -199,10 +233,23 @@ def main():
large_directory = "../../files/large_images" large_directory = "../../files/large_images"
ImagesInArray(large_directory, field.large_image_array) ImagesInArray(large_directory, field.large_image_array)
# Add arrow image to Player class
images = []
for file in os.listdir("../../files/arrow"):
path = f"../../files/arrow/{file}"
img = PhotoImage(master=field.small_field_canvas, file=path)
images.append(img)
player.arrow_east_image = images[0]
player.arrow_north_image = images[1]
player.arrow_south_image = images[2]
player.arrow_west_image = images[3]
# Filling window with images # Filling window with images
Fill() Fill()
# Drawing rectangle (player) # Drawing arrow (player)
Rectangle() Arrow(player.direction)
# Rectangle()
# Binding keyboard press to function # Binding keyboard press to function
field.win.bind("<Key>", Action) field.win.bind("<Key>", Action)
# Starting mainloop for window # Starting mainloop for window

BIN
files/arrow/arrow_east.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 854 B

BIN
files/arrow/arrow_north.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 847 B

BIN
files/arrow/arrow_south.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 844 B

BIN
files/arrow/arrow_west.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 853 B