Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, random, math
- class Ghost(pygame.sprite.Sprite):
- global LEFT, RIGHT, UP, DOWN
- LEFT = "left"
- RIGHT = "right"
- UP = "up"
- DOWN = "down"
- def __init__(self, color, x, y, wallsVert, wallsHor, grid):
- super().__init__()
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.Surface([20,20])
- pygame.draw.rect(self.image, color, [0,0,20,20])
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- self.direction = 0
- self.directionQueue = 0
- self.wallsVert = wallsVert
- self.wallsHor = wallsHor
- self.grid = grid
- def draw(self, DISPLAYSURF):
- DISPLAYSURF.blit(self.image, self.rect)
- def move(self):
- if self.direction == UP:
- self.rect.centery -= 2
- if len(pygame.sprite.spritecollide(self, self.wallsVert[int(self.rect.left / 20)], False)) != 0:
- self.rect.centery += 2
- elif self.direction == DOWN:
- self.rect.centery += 2
- if len(pygame.sprite.spritecollide(self, self.wallsVert[int(self.rect.left / 20)], False)) != 0:
- self.rect.centery -= 2
- elif self.direction == LEFT:
- self.rect.centerx -= 2
- if self.rect.centerx == 0:
- self.rect.centerx = 610
- if len(pygame.sprite.spritecollide(self, self.wallsHor[int(self.rect.top / 20)], False)) != 0:
- self.rect.centerx += 2
- elif self.direction == RIGHT:
- self.rect.centerx += 2
- if self.rect.centerx == 610:
- self.rect.centerx = 0
- if len(pygame.sprite.spritecollide(self, self.wallsHor[int(self.rect.top / 20)], False)) != 0:
- self.rect.centerx -= 2
- def chase(self, x, y):
- p = self.shortestPath(self.rect.x, self.rect.y)
- tot = self.path((int(y / 20), int(x / 20)), (int(self.rect.y / 20), int(self.rect.x / 20)), p)
- to = tot[1]
- if to[1] == int(self.rect.x / 20) - 1:
- self.directionQueue = LEFT
- if self.canTurn():
- self.direction = LEFT
- elif to[1] == int(self.rect.x / 20) + 1:
- self.directionQueue = RIGHT
- if self.canTurn():
- self.direction = RIGHT
- elif to[0] == int(self.rect.y / 20) + 1:
- self.directionQueue = DOWN
- if self.canTurn():
- self.direction = DOWN
- elif to[0] == int(self.rect.y / 20) - 1:
- self.directionQueue = UP
- if self.canTurn():
- self.direction = UP
- self.move()
- def shortestPath(self, x, y):
- x /= 20
- y /= 20
- x = int(x)
- y = int(y)
- ai = [0, 0, 1, -1]
- bi = [1, -1, 0, 0]
- dist = []
- for i in range(0, 32):
- t = []
- for j in range(0, 31):
- t.append(1000000)
- dist.append(t)
- q = [(y, x)]
- p = []
- for i in range(0, 32):
- t = []
- for j in range(0, 31):
- t.append(None)
- p.append(t)
- dist[y][x] = 0
- while len(q) != 0:
- u = q[0]
- q.pop(0)
- for j in range(0, 4):
- 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:
- dist[u[0] + ai[j]][u[1] + bi[j]] = dist[u[0]][u[1]] + 1
- p[u[0] + ai[j]][u[1] + bi[j]] = u
- q.append((u[0] + ai[j], u[1] + bi[j]))
- return p
- def path(self, u, s, p):
- ans = []
- if u == s:
- ans.append(s)
- return ans
- ans = self.path(p[u[0]][u[1]], s, p)
- ans.append(u)
- return ans
- def canTurn(self):
- if self.rect.left % 20 == 0 and self.rect.top % 20 == 0:
- x = int(self.rect.left / 20)
- y = int(self.rect.top / 20)
- if self.directionQueue == UP and self.grid[y - 1][x] != 0:
- return True
- elif self.directionQueue == DOWN and self.grid[y + 1][x] != 0:
- return True
- elif self.directionQueue == RIGHT and self.grid[y][x + 1] != 0:
- return True
- elif self.directionQueue == LEFT and self.grid[y][x - 1] != 0:
- return True
- return False
- def randMove(self):
- if self.rect.left % 20 == 0 and self.rect.top % 20 == 0:
- lx = self.rect.left / 20
- ly = self.rect.top / 20
- if ly == 15 and lx == 0 and self.direction == LEFT:
- self.rect.left = 600
- lx = 30
- if ly == 15 and lx == 30 and self.direction == RIGHT:
- self.rect.left = 0
- lx = 0
- if self.direction == UP and self.grid[int(ly - 1)][int(lx)] == 0:
- if self.grid[int(ly)][int(lx - 1)] == 0:
- if self.grid[int(ly)][int(lx + 1)] == 0:
- self.direction = DOWN
- else:
- self.direction = RIGHT
- elif self.grid[int(ly)][int(lx + 1)] == 0:
- self.direction = LEFT
- else:
- if random.randint(0, 100) < 50:
- self.direction = LEFT
- else:
- self.direction = RIGHT
- elif self.direction == DOWN and self.grid[int(ly + 1)][int(lx)] == 0:
- if self.grid[int(ly)][int(lx - 1)] == 0:
- if self.grid[int(ly)][int(lx + 1)] == 0:
- self.direction = UP
- else:
- self.direction = RIGHT
- elif self.grid[int(ly)][int(lx + 1)] == 0:
- self.direction = LEFT
- else:
- if random.randint(0, 100) < 50:
- self.direction = LEFT
- else:
- self.direction = RIGHT
- elif self.direction == RIGHT and self.grid[int(ly)][int(lx + 1)] == 0:
- if self.grid[int(ly - 1)][int(lx)] == 0:
- if self.grid[int(ly + 1)][int(lx)] == 0:
- self.direction = LEFT
- else:
- self.direction = DOWN
- elif self.grid[int(ly + 1)][int(lx)] == 0:
- self.direction = UP
- else:
- if random.randint(0, 100) < 50:
- self.direction = UP
- else:
- self.direction = DOWN
- elif self.direction == LEFT and self.grid[int(ly)][int(lx - 1)] == 0:
- if self.grid[int(ly - 1)][int(lx)] == 0:
- if self.grid[int(ly + 1)][int(lx)] == 0:
- self.direction = RIGHT
- else:
- self.direction = DOWN
- elif self.grid[int(ly + 1)][int(lx)] == 0:
- self.direction = UP
- else:
- if random.randint(0, 100) < 50:
- self.direction = UP
- else:
- self.direction = DOWN
- if self.direction == UP or self.direction == DOWN:
- if self.grid[int(ly)][int(lx - 1)] == 1 or self.grid[int(ly)][int(lx - 1)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = LEFT
- elif self.grid[int(ly)][int(lx + 1)] == 1 or self.grid[int(ly)][int(lx + 1)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = RIGHT
- elif self.direction == RIGHT or self.direction == LEFT:
- if self.grid[int(ly - 1)][int(lx)] == 1 or self.grid[int(ly - 1)][int(lx)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = UP
- elif self.grid[int(ly + 1)][int(lx)] == 1 or self.grid[int(ly + 1)][int(lx)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = DOWN
- self.move()
- def areaPatrol(self, blx, bly, trx, tRy):
- if blx <= math.floor(self.rect.x / 20) <= trx and tRy <= math.floor(self.rect.y / 20) <= bly:
- print("HI")
- if self.rect.left % 20 == 0 and self.rect.top % 20 == 0:
- lx = self.rect.left / 20
- ly = self.rect.top / 20
- if ly == 15 and lx == 0 and self.direction == LEFT:
- self.rect.left = 600
- lx = 30
- if ly == 15 and lx == 30 and self.direction == RIGHT:
- self.rect.left = 0
- lx = 0
- if self.direction == UP and (self.grid[int(ly - 1)][int(lx)] == 0 or ly - 1 == bly):
- if self.grid[int(ly)][int(lx - 1)] == 0 and lx - 1 == blx:
- if self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
- self.direction = DOWN
- else:
- self.direction = RIGHT
- elif self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
- self.direction = LEFT
- else:
- if random.randint(0, 100) < 50:
- self.direction = LEFT
- else:
- self.direction = RIGHT
- elif self.direction == DOWN and (self.grid[int(ly + 1)][int(lx)] == 0 or ly + 1 == tRy):
- if self.grid[int(ly)][int(lx - 1)] == 0 and lx - 1 == blx:
- if self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
- self.direction = UP
- else:
- self.direction = RIGHT
- elif self.grid[int(ly)][int(lx + 1)] == 0 and lx + 1 == trx:
- self.direction = LEFT
- else:
- if random.randint(0, 100) < 50:
- self.direction = LEFT
- else:
- self.direction = RIGHT
- elif self.direction == RIGHT and (self.grid[int(ly)][int(lx + 1)] == 0 or lx + 1 == trx):
- if self.grid[int(ly - 1)][int(lx)] == 0 and ly - 1 == bly:
- if self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
- self.direction = LEFT
- else:
- self.direction = DOWN
- elif self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
- self.direction = UP
- else:
- if random.randint(0, 100) < 50:
- self.direction = UP
- else:
- self.direction = DOWN
- elif self.direction == LEFT and (self.grid[int(ly)][int(lx - 1)] == 0 or lx - 1 == blx):
- if self.grid[int(ly - 1)][int(lx)] == 0 and ly - 1 == bly:
- if self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
- self.direction = RIGHT
- else:
- self.direction = DOWN
- elif self.grid[int(ly + 1)][int(lx)] == 0 and ly + 1 == tRy:
- self.direction = UP
- else:
- if random.randint(0, 100) < 50:
- self.direction = UP
- else:
- self.direction = DOWN
- if self.direction == UP or self.direction == DOWN:
- if self.grid[int(ly)][int(lx - 1)] == 1 or self.grid[int(ly)][int(lx - 1)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = LEFT
- elif self.grid[int(ly)][int(lx + 1)] == 1 or self.grid[int(ly)][int(lx + 1)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = RIGHT
- elif self.direction == RIGHT or self.direction == LEFT:
- if self.grid[int(ly - 1)][int(lx)] == 1 or self.grid[int(ly - 1)][int(lx)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = UP
- elif self.grid[int(ly + 1)][int(lx)] == 1 or self.grid[int(ly + 1)][int(lx)] == 2:
- if random.randint(0, 100) < 25:
- self.direction = DOWN
- self.move()
- else:
- if blx == 2 and bly == 13 and trx == 14 and tRy == 2:
- px = 4
- py = 5
- elif blx == 15 and bly == 13 and trx == 18 and tRy == 2:
- px = 24
- py = 6
- elif blx == 15 and bly == 29 and trx == 28 and tRy == 14:
- px = 24
- py = 25
- else:
- px = 5
- py = 25
- self.chase(px * 20, py * 20)
Add Comment
Please, Sign In to add comment