Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import time
  2. class Player:
  3. def __init__(self):
  4. self.y = 2
  5. self.x = 2
  6. self.steps = 0
  7. self.rotation = 1
  8. self.last = 1
  9. self.p = 0
  10. def move(self, map, m_y, m_x):
  11. map[self.y][self.x]= str(self.last)
  12. 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
  13. map[self.y+m_y][self.x+m_x]= "p"
  14. self.y += m_y
  15. self.x += m_x
  16. self.steps = self.steps+1
  17.  
  18.  
  19. def check(self, map):
  20. rotation = {
  21. 0: (1,0),
  22. 1: (0,1),
  23. 2: (-1,0),
  24. 3: (0,-1)
  25. }
  26. male = 100
  27. male_rot = 100
  28. # print(map, self.y, self.x)
  29. for i in range(4):
  30. self.steps += 1
  31. m_y,m_x = rotation[(i+self.rotation)%4]
  32. # print("in for rot = {}, self.rot = {}, map = {}, i={}".format(rotation[(i + self.rotation) % 4],
  33. # (self.rotation + i - 1) % 4,
  34. # map[self.y + m_y][self.x + m_x], i))
  35. if (map[self.y + m_y][self.x + m_x] == " "):
  36. # self.rotation = (self.rotation + i - 1) % 4
  37.  
  38. male = -1
  39. male_rot = i
  40. # return m_y, m_x
  41.  
  42. elif (map[self.y + m_y][self.x + m_x] =="k"):
  43. print("hopaj siupaj wygrałeś!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", self.steps+1)
  44. time.sleep(34.5)
  45. self.rotation = (self.rotation + i - 1) % 4
  46. return m_y, m_x
  47. else:
  48. p = map[self.y+m_y][self.x+m_x] if map[self.y+m_y][self.x+m_x] != "x" else 100
  49. p = int(p)
  50. if (p < male):
  51. male = p
  52. male_rot = i
  53.  
  54.  
  55.  
  56. # print("after for rot = {}, self.rot = {}, map = {}, i={}".format(rotation[(i + self.rotation) % 4],
  57. # (self.rotation + i - 1) % 4,
  58. # map[self.y + m_y][self.x + m_x], i))
  59.  
  60.  
  61. m_y,m_x = rotation[(male_rot + self.rotation)%4]
  62. self.rotation = (self.rotation + male_rot -1)%4
  63. return m_y,m_x
  64.  
  65.  
  66.  
  67. class Labirynt:
  68. def __init__(self, name):
  69. labirynt_file = open(name, "r")
  70. labirynt_map = labirynt_file.readlines()
  71. labirynt_file.close()
  72. for index_map, string_map in enumerate(labirynt_map):
  73. labirynt_map[index_map] = list(string_map)[:-1]
  74. # print(labirynt_map)
  75. self.map = labirynt_map
  76.  
  77. def print_map(self):
  78. # print('\033[91m'+"-"*10+'\033[92m')
  79. for i in self.map:
  80. i = "".join(i)
  81. i = i.replace(" ", ".")
  82. print('\033[92m'+i)
  83. # print()
  84.  
  85. def delete_map(self):
  86. pass
  87. def go():
  88. y, x = player.check(labirynt.map)
  89. player.move(labirynt.map, y, x)
  90.  
  91.  
  92. labirynt = Labirynt("lab2.txt")
  93. labirynt.print_map()
  94.  
  95. player = Player()
  96. player.move(labirynt.map, 2, 2)
  97. #labirynt.print_map()
  98.  
  99.  
  100. while(labirynt.map!= (0,0)):
  101.  
  102. go()
  103. #labirynt.print_map()
  104. # time.sleep(0.3)
  105.  
  106. # co do zrobienia mam:
  107. # - powrót do danego punktu i start dalej
  108. # - cofam się tylko gdy nie mam innej drogi
  109. # -
  110. #
  111. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement