Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 4.09 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. import sys
  2. import math
  3. import pygame
  4.  
  5. pygame.init()
  6.  
  7. screen = pygame.display.set_mode((800,600))
  8.  
  9. scale = 20
  10. offsetX = scale/2
  11. offsetY = scale/2
  12.  
  13. black = (0, 0, 0)
  14. gray = (50, 50, 50)
  15. white = (255, 255, 255)
  16. red = (255, 0, 0)
  17.  
  18. class Board(object):
  19.         def __init__(self):
  20.                 self.nodes = []
  21.        
  22.         def populateNodes(self, width, height):
  23.                 for x in range(0, width):
  24.                         for y in range(0, height):
  25.                                 self.addNode(Node((x,y)))
  26.        
  27.         def addNode(self, node):
  28.                 self.nodes.append(node)
  29.        
  30.         def closeNode(self, point):
  31.                 for node in self.nodes:
  32.                         if node.point == point:
  33.                                 node.open = False
  34.                                 return
  35.        
  36.         def openNode(self, point):
  37.                 for node in self.nodes:
  38.                         if node.point == point:
  39.                                 node.open = True
  40.                                 return
  41.        
  42.         def clear(self):
  43.                 for node in self.nodes:
  44.                         node.open = True
  45.        
  46.         def getNodeAt(self, point):
  47.                 for node in self.nodes:
  48.                         if node.point == point and node.open:
  49.                                 return node
  50.                 return None
  51.        
  52.         def getNodeAtPixel(self, pixel):
  53.                 pX = math.floor(pixel[0]/scale)
  54.                 pY = math.floor(pixel[1]/scale)
  55.                 point = (pX, pY)
  56.                 return self.getNodeAt(point)
  57.        
  58.         def drawNodes(self):
  59.                 for node in self.nodes:
  60.                         oX, oY = node.point
  61.                         rect = (oX*scale, oY*scale, scale, scale)
  62.                         width = 1
  63.                         if not node.open:
  64.                                 width = 0
  65.                         pygame.draw.rect(screen, gray, rect, width)
  66.        
  67. class Node(object):
  68.         def __init__(self, point):
  69.                 self.open = True
  70.                 self.weight = 0
  71.                 self.distance = 0
  72.                 self.point = point
  73.        
  74.         def calculateWeight(self, creep):
  75.                 movementCost = self.calculateMovement(creep)
  76.                 distanceEstimate = self.calculateDistanceTo(creep)
  77.                 self.weight = movementCost+distanceEstimate
  78.        
  79.         def calculateMovement(self, creep):
  80.                 if self.point[0] == creep.destination[0] or self.point[1] == creep.destination[1]:
  81.                         return 10
  82.                 return 14
  83.        
  84.         def calculateDistanceTo(self, creep):
  85.                 dX = abs(creep.destination[0]-self.point[0])
  86.                 dY = abs(creep.destination[1]-self.point[1])
  87.                 return (dX+dY)*10
  88.                
  89.         def __repr__(self):
  90.                 return "<Node(X:'%d',Y:'%d',Weight:'%d')>" % (self.point[0], self.point[1], self.weight)
  91.                
  92. class Creep(object):
  93.         def __init__(self):
  94.                 self.board = None
  95.                 self.origin = ()
  96.                 self.point = ()
  97.                 self.destination = ()
  98.                 self.path = []
  99.                 self.pathStep = ()
  100.                 self.pathFound = False
  101.        
  102.         def attachToBoard(self, board, point):
  103.                 self.board = board
  104.                 self.point = point
  105.        
  106.         def findPath(self, destinationPoint):
  107.                 self.path = []
  108.                 self.pathStep = self.point
  109.                 self.pathFound = False
  110.                 self.destination = destinationPoint
  111.                
  112.                 while self.pathFound == False:
  113.                         self.pathStep = self.scan(self.pathStep).point
  114.                         self.path.append(self.pathStep)
  115.                         self.board.closeNode(self.pathStep)
  116.        
  117.         def scan(self, currentPoint):
  118.                 nextNode = None
  119.                 nodes = []
  120.                
  121.                 for x in range(-1, 2):
  122.                         for y in range(-1, 2):
  123.                                 nodeX = currentPoint[0]+x
  124.                                 nodeY = currentPoint[1]+y
  125.                                 nodes.append(self.board.getNodeAt((nodeX,nodeY)))
  126.                
  127.                 for node in nodes:
  128.                         if not node:
  129.                                 continue
  130.                         node.calculateWeight(self)
  131.                        
  132.                 return self.lightestNode(nodes)
  133.                
  134.         def lightestNode(self, nodes):
  135.                 nextNode = None
  136.                
  137.                 for node in nodes:
  138.                         if not node:
  139.                                 continue
  140.                         else:
  141.                                 if node.point == self.destination:
  142.                                         self.pathFound = True
  143.                                         return node
  144.                                 if not nextNode:
  145.                                         nextNode = node
  146.                                 elif nextNode.weight > node.weight:
  147.                                         nextNode = node
  148.                 return nextNode
  149.        
  150.         def drawPath(self):
  151.                 for point in self.path:
  152.                         oX, oY = point
  153.                         pygame.draw.circle(screen, red, ((oX*scale)+offsetX, (oY*scale)+offsetY), 5, 1)
  154.  
  155. board = Board()
  156. creep = Creep()
  157.  
  158. board.populateNodes(40,30)
  159. creep.attachToBoard(board, (5,5))
  160.  
  161. while 1:
  162.         for event in pygame.event.get():
  163.                 if event.type == pygame.MOUSEBUTTONUP:
  164.                         board.clear()
  165.                        
  166.                         board.closeNode((10,8))
  167.                         board.closeNode((10,9))
  168.                         board.closeNode((10,10))
  169.                         board.closeNode((10,11))
  170.                        
  171.                         board.closeNode((20,20))
  172.                         board.closeNode((20,19))
  173.                         board.closeNode((20,18))
  174.                        
  175.                         node = board.getNodeAtPixel(event.pos)
  176.                         creep.findPath(node.point)
  177.                 elif event.type == pygame.QUIT:
  178.                         sys.exit()
  179.        
  180.         screen.fill(black)
  181.         board.drawNodes()
  182.         creep.drawPath()
  183.         pygame.display.flip()