zolinthecow

Untitled

Nov 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.24 KB | None | 0 0
  1. import pygame, random, math
  2.  
  3. class Ghost(pygame.sprite.Sprite):
  4.  
  5. global LEFT, RIGHT, UP, DOWN
  6. LEFT = "left"
  7. RIGHT = "right"
  8. UP = "up"
  9. DOWN = "down"
  10.  
  11. def __init__(self, color, x, y, wallsVert, wallsHor, grid):
  12. super().__init__()
  13.  
  14. pygame.sprite.Sprite.__init__(self)
  15.  
  16. self.image = pygame.Surface([20,20])
  17.  
  18. pygame.draw.rect(self.image, color, [0,0,20,20])
  19.  
  20. self.rect = self.image.get_rect()
  21. self.rect.x = x
  22. self.rect.y = y
  23.  
  24. self.direction = 0
  25. self.directionQueue = 0
  26.  
  27. self.wallsVert = wallsVert
  28. self.wallsHor = wallsHor
  29.  
  30. self.grid = grid
  31.  
  32. def draw(self, DISPLAYSURF):
  33. DISPLAYSURF.blit(self.image, self.rect)
  34.  
  35. def move(self):
  36. if self.direction == UP:
  37. self.rect.centery -= 2
  38. if len(pygame.sprite.spritecollide(self, self.wallsVert[int(self.rect.left / 20)], False)) != 0:
  39. self.rect.centery += 2
  40. elif self.direction == DOWN:
  41. self.rect.centery += 2
  42. if len(pygame.sprite.spritecollide(self, self.wallsVert[int(self.rect.left / 20)], False)) != 0:
  43. self.rect.centery -= 2
  44. elif self.direction == LEFT:
  45. self.rect.centerx -= 2
  46. if self.rect.centerx == 0:
  47. self.rect.centerx = 610
  48. if len(pygame.sprite.spritecollide(self, self.wallsHor[int(self.rect.top / 20)], False)) != 0:
  49. self.rect.centerx += 2
  50. elif self.direction == RIGHT:
  51. self.rect.centerx += 2
  52. if self.rect.centerx == 610:
  53. self.rect.centerx = 0
  54. if len(pygame.sprite.spritecollide(self, self.wallsHor[int(self.rect.top / 20)], False)) != 0:
  55. self.rect.centerx -= 2
  56.  
  57. def chase(self, x, y):
  58.  
  59. p = self.shortestPath(self.rect.x, self.rect.y)
  60. tot = self.path((int(y / 20), int(x / 20)), (int(self.rect.y / 20), int(self.rect.x / 20)), p)
  61. to = tot[1]
  62.  
  63. if to[1] == int(self.rect.x / 20) - 1:
  64. self.directionQueue = LEFT
  65. if self.canTurn():
  66. self.direction = LEFT
  67. elif to[1] == int(self.rect.x / 20) + 1:
  68. self.directionQueue = RIGHT
  69. if self.canTurn():
  70. self.direction = RIGHT
  71. elif to[0] == int(self.rect.y / 20) + 1:
  72. self.directionQueue = DOWN
  73. if self.canTurn():
  74. self.direction = DOWN
  75. elif to[0] == int(self.rect.y / 20) - 1:
  76. self.directionQueue = UP
  77. if self.canTurn():
  78. self.direction = UP
  79.  
  80. self.move()
  81.  
  82.  
  83. def shortestPath(self, x, y):
  84. x /= 20
  85. y /= 20
  86.  
  87. x = int(x)
  88. y = int(y)
  89.  
  90. ai = [0, 0, 1, -1]
  91. bi = [1, -1, 0, 0]
  92.  
  93. dist = []
  94.  
  95. for i in range(0, 32):
  96. t = []
  97. for j in range(0, 31):
  98. t.append(1000000)
  99. dist.append(t)
  100.  
  101. q = [(y, x)]
  102. p = []
  103.  
  104. for i in range(0, 32):
  105. t = []
  106. for j in range(0, 31):
  107. t.append(None)
  108. p.append(t)
  109. dist[y][x] = 0
  110.  
  111. while len(q) != 0:
  112. u = q[0]
  113. q.pop(0)
  114. for j in range(0, 4):
  115.  
  116. if 0 <= u[0] + ai[j] < 32 and 0 <= u[1] + bi[j] < 31 and dist[u[0] + ai[j]][u[1] + bi[j]] == 1000000 and self.grid[u[0] + ai[j]][u[1] + bi[j]] != 0:
  117. dist[u[0] + ai[j]][u[1] + bi[j]] = dist[u[0]][u[1]] + 1
  118. p[u[0] + ai[j]][u[1] + bi[j]] = u
  119. q.append((u[0] + ai[j], u[1] + bi[j]))
  120. return p
  121.  
  122. def path(self, u, s, p):
  123. ans = []
  124. if u == s:
  125. ans.append(s)
  126. return ans
  127. ans = self.path(p[u[0]][u[1]], s, p)
  128. ans.append(u)
  129. return ans
  130.  
  131. def canTurn(self):
  132. if self.rect.left % 20 == 0 and self.rect.top % 20 == 0:
  133. x = int(self.rect.left / 20)
  134. y = int(self.rect.top / 20)
  135. if self.directionQueue == UP and self.grid[y - 1][x] != 0:
  136. return True
  137. elif self.directionQueue == DOWN and self.grid[y + 1][x] != 0:
  138. return True
  139. elif self.directionQueue == RIGHT and self.grid[y][x + 1] != 0:
  140. return True
  141. elif self.directionQueue == LEFT and self.grid[y][x - 1] != 0:
  142. return True
  143.  
  144. return False
  145.  
  146. def randMove(self):
  147. if self.rect.left % 20 == 0 and self.rect.top % 20 == 0:
  148. lx = self.rect.left / 20
  149. ly = self.rect.top / 20
  150.  
  151. if ly == 15 and lx == 0 and self.direction == LEFT:
  152. self.rect.left = 600
  153. lx = 30
  154. if ly == 15 and lx == 30 and self.direction == RIGHT:
  155. self.rect.left = 0
  156. lx = 0
  157.  
  158. if self.direction == UP and self.grid[int(ly - 1)][int(lx)] == 0:
  159. if self.grid[int(ly)][int(lx - 1)] == 0:
  160. if self.grid[int(ly)][int(lx + 1)] == 0:
  161. self.direction = DOWN
  162. else:
  163. self.direction = RIGHT
  164. elif self.grid[int(ly)][int(lx + 1)] == 0:
  165. self.direction = LEFT
  166. else:
  167. if random.randint(0, 100) < 50:
  168. self.direction = LEFT
  169. else:
  170. self.direction = RIGHT
  171. elif self.direction == DOWN and self.grid[int(ly + 1)][int(lx)] == 0:
  172. if self.grid[int(ly)][int(lx - 1)] == 0:
  173. if self.grid[int(ly)][int(lx + 1)] == 0:
  174. self.direction = UP
  175. else:
  176. self.direction = RIGHT
  177. elif self.grid[int(ly)][int(lx + 1)] == 0:
  178. self.direction = LEFT
  179. else:
  180. if random.randint(0, 100) < 50:
  181. self.direction = LEFT
  182. else:
  183. self.direction = RIGHT
  184. elif self.direction == RIGHT and self.grid[int(ly)][int(lx + 1)] == 0:
  185. if self.grid[int(ly - 1)][int(lx)] == 0:
  186. if self.grid[int(ly + 1)][int(lx)] == 0:
  187. self.direction = LEFT
  188. else:
  189. self.direction = DOWN
  190. elif self.grid[int(ly + 1)][int(lx)] == 0:
  191. self.direction = UP
  192. else:
  193. if random.randint(0, 100) < 50:
  194. self.direction = UP
  195. else:
  196. self.direction = DOWN
  197. elif self.direction == LEFT and self.grid[int(ly)][int(lx - 1)] == 0:
  198. if self.grid[int(ly - 1)][int(lx)] == 0:
  199. if self.grid[int(ly + 1)][int(lx)] == 0:
  200. self.direction = RIGHT
  201. else:
  202. self.direction = DOWN
  203. elif self.grid[int(ly + 1)][int(lx)] == 0:
  204. self.direction = UP
  205. else:
  206. if random.randint(0, 100) < 50:
  207. self.direction = UP
  208. else:
  209. self.direction = DOWN
  210.  
  211. if self.direction == UP or self.direction == DOWN:
  212. if self.grid[int(ly)][int(lx - 1)] == 1 or self.grid[int(ly)][int(lx - 1)] == 2:
  213. if random.randint(0, 100) < 25:
  214. self.direction = LEFT
  215. elif self.grid[int(ly)][int(lx + 1)] == 1 or self.grid[int(ly)][int(lx + 1)] == 2:
  216. if random.randint(0, 100) < 25:
  217. self.direction = RIGHT
  218. elif self.direction == RIGHT or self.direction == LEFT:
  219. if self.grid[int(ly - 1)][int(lx)] == 1 or self.grid[int(ly - 1)][int(lx)] == 2:
  220. if random.randint(0, 100) < 25:
  221. self.direction = UP
  222. elif self.grid[int(ly + 1)][int(lx)] == 1 or self.grid[int(ly + 1)][int(lx)] == 2:
  223. if random.randint(0, 100) < 25:
  224. self.direction = DOWN
  225. self.move()
  226.  
  227. def areaPatrol(self, blx, bly, trx, tRy):
  228. if blx <= math.floor(self.rect.x / 20) <= trx and tRy <= math.floor(self.rect.y / 20) <= bly:
  229. print("HI")
  230. if self.rect.left % 20 == 0 and self.rect.top % 20 == 0:
  231. lx = self.rect.left / 20
  232. ly = self.rect.top / 20
  233.  
  234. if ly == 15 and lx == 0 and self.direction == LEFT:
  235. self.rect.left = 600
  236. lx = 30
  237. if ly == 15 and lx == 30 and self.direction == RIGHT:
  238. self.rect.left = 0
  239. lx = 0
  240.  
  241. if self.direction == UP and (self.grid[int(ly - 1)][int(lx)] == 0 or ly - 1 == bly):
  242. if self.grid[int(ly)][int(lx - 1)] == 0 and lx - 1 == blx:
  243. if self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
  244. self.direction = DOWN
  245. else:
  246. self.direction = RIGHT
  247. elif self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
  248. self.direction = LEFT
  249. else:
  250. if random.randint(0, 100) < 50:
  251. self.direction = LEFT
  252. else:
  253. self.direction = RIGHT
  254. elif self.direction == DOWN and (self.grid[int(ly + 1)][int(lx)] == 0 or ly + 1 == tRy):
  255. if self.grid[int(ly)][int(lx - 1)] == 0 and lx - 1 == blx:
  256. if self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
  257. self.direction = UP
  258. else:
  259. self.direction = RIGHT
  260. elif self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
  261. self.direction = LEFT
  262. else:
  263. if random.randint(0, 100) < 50:
  264. self.direction = LEFT
  265. else:
  266. self.direction = RIGHT
  267. elif self.direction == RIGHT and (self.grid[int(ly)][int(lx + 1)] == 0 or lx + 1 == trx):
  268. if self.grid[int(ly - 1)][int(lx)] == 0 and ly - 1 == bly:
  269. if self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
  270. self.direction = LEFT
  271. else:
  272. self.direction = DOWN
  273. elif self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
  274. self.direction = UP
  275. else:
  276. if random.randint(0, 100) < 50:
  277. self.direction = UP
  278. else:
  279. self.direction = DOWN
  280. elif self.direction == LEFT and (self.grid[int(ly)][int(lx - 1)] == 0 or lx - 1 == blx):
  281. if self.grid[int(ly - 1)][int(lx)] == 0 and ly - 1 == bly:
  282. if self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
  283. self.direction = RIGHT
  284. else:
  285. self.direction = DOWN
  286. elif self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
  287. self.direction = UP
  288. else:
  289. if random.randint(0, 100) < 50:
  290. self.direction = UP
  291. else:
  292. self.direction = DOWN
  293.  
  294. if self.direction == UP or self.direction == DOWN:
  295. if self.grid[int(ly)][int(lx - 1)] == 1 or self.grid[int(ly)][int(lx - 1)] == 2:
  296. if random.randint(0, 100) < 25:
  297. self.direction = LEFT
  298. elif self.grid[int(ly)][int(lx + 1)] == 1 or self.grid[int(ly)][int(lx + 1)] == 2:
  299. if random.randint(0, 100) < 25:
  300. self.direction = RIGHT
  301. elif self.direction == RIGHT or self.direction == LEFT:
  302. if self.grid[int(ly - 1)][int(lx)] == 1 or self.grid[int(ly - 1)][int(lx)] == 2:
  303. if random.randint(0, 100) < 25:
  304. self.direction = UP
  305. elif self.grid[int(ly + 1)][int(lx)] == 1 or self.grid[int(ly + 1)][int(lx)] == 2:
  306. if random.randint(0, 100) < 25:
  307. self.direction = DOWN
  308. self.move()
  309. else:
  310. if blx == 2 and bly == 13 and trx == 14 and tRy == 2:
  311. px = 4
  312. py = 5
  313. elif blx == 15 and bly == 13 and trx == 18 and tRy == 2:
  314. px = 24
  315. py = 6
  316. elif blx == 15 and bly == 29 and trx == 28 and tRy == 14:
  317. px = 24
  318. py = 25
  319. else:
  320. px = 5
  321. py = 25
  322. self.chase(px * 20, py * 20)
Add Comment
Please, Sign In to add comment