Fixed moves

This commit is contained in:
Krzysztof Surażyński 2021-05-26 09:25:15 +02:00
parent b2cd21b5d8
commit ff8f1bf6d0
6 changed files with 20 additions and 39 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
venv venv
.idea .idea
__pycache__/

View File

@ -21,4 +21,6 @@ R_RIGHT = "R_RIGHT"
R_LEFT = "R_LEFT" R_LEFT = "R_LEFT"
R_UP = "R_UP" R_UP = "R_UP"
R_DOWN = "R_DOWN" R_DOWN = "R_DOWN"
MOVE="MOVE"
NOTHING="NOTHING"

View File

@ -11,7 +11,7 @@ class Truck:
def __init__(self, x=0, y=0): def __init__(self, x=0, y=0):
self.abstractX = x self.abstractX = x
self.abstractY = y self.abstractY = y
self.direction = R_RIGHT self.direction = 90
asset_path = os.path.join(os.path.dirname(__file__), '..', '..', 'assets', 'garbage-truck.png') asset_path = os.path.join(os.path.dirname(__file__), '..', '..', 'assets', 'garbage-truck.png')
self.backAsset = pygame.transform.scale(pygame.image.load(asset_path), (DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE)) self.backAsset = pygame.transform.scale(pygame.image.load(asset_path), (DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE))
self.asset = pygame.transform.scale(pygame.image.load(asset_path), (DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE)) self.asset = pygame.transform.scale(pygame.image.load(asset_path), (DEFAULT_ASSET_SIZE, DEFAULT_ASSET_SIZE))
@ -20,33 +20,22 @@ class Truck:
self.velocity = DEFAULT_ASSET_SIZE self.velocity = DEFAULT_ASSET_SIZE
def rotate_up(self): def rotate_up(self):
self.asset = self.backAsset
self.asset = pygame.transform.rotate(self.asset, 90) self.asset = pygame.transform.rotate(self.asset, 90)
self.direction = R_UP self.direction = (self.direction + 90) % 360
def rotate_down(self): def rotate_down(self):
self.asset = self.backAsset
self.asset = pygame.transform.rotate(self.asset, -90) self.asset = pygame.transform.rotate(self.asset, -90)
self.direction = R_DOWN self.direction = (self.direction - 90) % 360
def rotate_right(self):
self.asset = self.backAsset
self.direction = R_RIGHT
def rotate_left(self):
self.asset = self.backAsset
self.asset = pygame.transform.rotate(self.asset, 180)
self.asset = pygame.transform.flip(self.asset, False, True)
self.direction = R_LEFT
def move(self, map): def move(self, map):
if self.direction == R_UP: print(self.direction)
if self.direction == 180:
self.move_up(map) self.move_up(map)
if self.direction == R_DOWN: if self.direction == 0:
self.move_down(map) self.move_down(map)
if self.direction == R_RIGHT: if self.direction == 90:
self.move_right(map) self.move_right(map)
if self.direction == R_LEFT: if self.direction == 270:
self.move_left(map) self.move_left(map)
def replace(self, map, oldX, oldY): def replace(self, map, oldX, oldY):

View File

@ -44,6 +44,7 @@ class EnvMap:
no = None no = None
if o.__class__.__name__ == 'Truck': if o.__class__.__name__ == 'Truck':
no = Truck(x, y) no = Truck(x, y)
no.direction = o.direction
newEnvMap.truck = no newEnvMap.truck = no
if o.__class__.__name__ == 'Trash': if o.__class__.__name__ == 'Trash':
@ -125,18 +126,11 @@ class PathFinder():
altState3 = state.get_map_copy() altState3 = state.get_map_copy()
t3 = altState3.map[truck.abstractX][truck.abstractY] t3 = altState3.map[truck.abstractX][truck.abstractY]
t3.rotate_right()
t3.move(altState3.map) t3.move(altState3.map)
altState4 = state.get_map_copy()
t4 = altState4.map[truck.abstractX][truck.abstractY]
t4.rotate_left()
t4.move(altState4.map)
pack.append([R_UP, altState1]) pack.append([R_UP, altState1])
pack.append([R_DOWN, altState2]) pack.append([R_DOWN, altState2])
pack.append([R_RIGHT, altState3]) pack.append([NOTHING, altState3])
pack.append([R_LEFT, altState4])
return pack return pack
@ -145,6 +139,9 @@ class PathFinder():
for y in range(DEFAULT_MAP_SIZE): for y in range(DEFAULT_MAP_SIZE):
a = s1.map[x][y] a = s1.map[x][y]
b = s2.map[x][y] b = s2.map[x][y]
if a.__class__.__name__ == 'Truck' and b.__class__.__name__ == 'Truck':
if a.direction != b.direction:
return False
if a.__class__.__name__ != b.__class__.__name__: if a.__class__.__name__ != b.__class__.__name__:
return False return False
return True return True
@ -202,10 +199,6 @@ class Environment:
self.envMap.truck.rotate_up() self.envMap.truck.rotate_up()
if key == pygame.K_s: if key == pygame.K_s:
self.envMap.truck.rotate_down() self.envMap.truck.rotate_down()
if key == pygame.K_d:
self.envMap.truck.rotate_right()
if key == pygame.K_a:
self.envMap.truck.rotate_left()
if key == pygame.K_o: if key == pygame.K_o:
actions = self.pathFinder.graphsearch(self.envMap.get_map_copy()) actions = self.pathFinder.graphsearch(self.envMap.get_map_copy())
self.navigate(actions) self.navigate(actions)
@ -217,18 +210,14 @@ class Environment:
action = actions.pop() action = actions.pop()
next = pygame.time.get_ticks() + 300 next = pygame.time.get_ticks() + 300
self.update() self.update()
ev=None
if action == R_UP: if action == R_UP:
ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_w}) ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_w})
if action == R_DOWN: if action == R_DOWN:
ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_s}) ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_s})
if action == R_RIGHT: if ev:
ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_d}) pygame.event.post(ev)
if action == R_LEFT:
ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_a})
pygame.event.post(ev)
ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_SPACE}) ev = pygame.event.Event(pygame.KEYDOWN, {"key": pygame.K_SPACE})
pygame.event.post(ev) pygame.event.post(ev)