Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- class Player:
- def __init__(self):
- self.y = 2
- self.x = 2
- self.steps = 0
- self.rotation = 1
- self.last = 1
- self.p = 0
- def move(self, map, m_y, m_x):
- map[self.y][self.x]= str(self.last)
- self.last = 0 if map[self.y+m_y][self.x+m_x] == " " or "x" else int(map[self.y+m_y][self.x+m_x]) + 1
- map[self.y+m_y][self.x+m_x]= "p"
- self.y += m_y
- self.x += m_x
- self.steps = self.steps+1
- def check(self, map):
- rotation = {
- 0: (1,0),
- 1: (0,1),
- 2: (-1,0),
- 3: (0,-1)
- }
- male = 100
- male_rot = 100
- # print(map, self.y, self.x)
- for i in range(4):
- self.steps += 1
- m_y,m_x = rotation[(i+self.rotation)%4]
- # print("in for rot = {}, self.rot = {}, map = {}, i={}".format(rotation[(i + self.rotation) % 4],
- # (self.rotation + i - 1) % 4,
- # map[self.y + m_y][self.x + m_x], i))
- if (map[self.y + m_y][self.x + m_x] == " "):
- # self.rotation = (self.rotation + i - 1) % 4
- male = -1
- male_rot = i
- # return m_y, m_x
- elif (map[self.y + m_y][self.x + m_x] =="k"):
- print("hopaj siupaj wygrałeś!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", self.steps+1)
- time.sleep(34.5)
- self.rotation = (self.rotation + i - 1) % 4
- return m_y, m_x
- else:
- p = map[self.y+m_y][self.x+m_x] if map[self.y+m_y][self.x+m_x] != "x" else 100
- p = int(p)
- if (p < male):
- male = p
- male_rot = i
- # print("after for rot = {}, self.rot = {}, map = {}, i={}".format(rotation[(i + self.rotation) % 4],
- # (self.rotation + i - 1) % 4,
- # map[self.y + m_y][self.x + m_x], i))
- m_y,m_x = rotation[(male_rot + self.rotation)%4]
- self.rotation = (self.rotation + male_rot -1)%4
- return m_y,m_x
- class Labirynt:
- def __init__(self, name):
- labirynt_file = open(name, "r")
- labirynt_map = labirynt_file.readlines()
- labirynt_file.close()
- for index_map, string_map in enumerate(labirynt_map):
- labirynt_map[index_map] = list(string_map)[:-1]
- # print(labirynt_map)
- self.map = labirynt_map
- def print_map(self):
- # print('\033[91m'+"-"*10+'\033[92m')
- for i in self.map:
- i = "".join(i)
- i = i.replace(" ", ".")
- print('\033[92m'+i)
- # print()
- def delete_map(self):
- pass
- def go():
- y, x = player.check(labirynt.map)
- player.move(labirynt.map, y, x)
- labirynt = Labirynt("lab2.txt")
- labirynt.print_map()
- player = Player()
- player.move(labirynt.map, 2, 2)
- #labirynt.print_map()
- while(labirynt.map!= (0,0)):
- go()
- #labirynt.print_map()
- # time.sleep(0.3)
- # co do zrobienia mam:
- # - powrót do danego punktu i start dalej
- # - cofam się tylko gdy nie mam innej drogi
- # -
- #
- #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement