Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. import pygame, math, sys, random
  2. import maze
  3. RANGE_X = 1680;
  4. RANGE_Y = 1000;
  5.  
  6. COL_UNVISITED = (0,0,0)
  7. COL_VISITED = (255,255,255)
  8.  
  9. class Coord(object):
  10. x = 0
  11. y = 0
  12. visited = False
  13. def __init__(self, x, y):
  14. self.x = x;
  15. self.y = y;
  16.  
  17. def __repr__(self):
  18. return "(X:" + str(self.x) + ",Y:" + str(self.y) + ",V:" + str(self.visited) + ")"
  19.  
  20. def isVisited(self):
  21. return self.visited
  22.  
  23. def visit(self, surface, visitFrom):
  24. self.visited = True
  25. surface.set_at(((self.x*2)+1,(self.y*2)+1),COL_VISITED)
  26. surface.set_at(((visitFrom.x*2)+1,(visitFrom.y*2)+1),COL_VISITED)
  27. getDir = (visitFrom.x - self.x,visitFrom.y - self.y)
  28. surface.set_at(((self.x*2+1)+getDir[0],(self.y*2+1)+getDir[1]),COL_VISITED)
  29.  
  30. def getDirection((x1,y1),(x2,y2)):
  31. d = (x1 - x2,y1 - y2)
  32. if(d[0] == 0 and d[1] < 0):
  33. return 0;
  34. if(d[0] > 0 and d[1] == 0):
  35. return 1;
  36. if(d[0] == 0 and d[1] > 0):
  37. return 2;
  38. if(d[0] < 0 and d[1] == 0):
  39. return 3;
  40.  
  41. def getNeighbours(x,y):
  42. nbpos = [(x,y-1),(x+1,y),(x,y+1),(x-1,y)]
  43. neighbours = [(0,0) for i in xrange(4)]
  44. for (vx,vy) in nbpos:
  45. if(vx >= 0 and vx < RANGE_X/4):
  46. if(vy >= 0 and vy < RANGE_Y/4):
  47. neighbours[getDirection((vx,vy),(x,y))] = (vx,vy)
  48. return neighbours
  49.  
  50. def isValidNeighbour(d,nbs):
  51. if d < 0:
  52. return False
  53. if nbs[d] == (0,0):
  54. return False
  55. else:
  56. return True
  57.  
  58. def getFreeNeighbours(field,x,y):
  59. nbs = getNeighbours(x,y)
  60. neighbours = []
  61. for (vx,vy) in nbs:
  62. if(vx+vy > 0):
  63. coord = field[vx][vy]
  64. if (coord.isVisited()) == False:
  65. neighbours.append(coord)
  66. return neighbours
  67.  
  68. def constructMaze(surface):
  69. history = []
  70. step = 0
  71. maxl = 0
  72. p = field[0][0]
  73. history.append(p)
  74. p.visit(surface,field[1][0])
  75. while 1:
  76. step+=1
  77. nbs = getFreeNeighbours(field,p.x,p.y)
  78. rnd = -1
  79. np = p
  80. pygame.time.delay(100)
  81. if(step%30==0):
  82. #pygame.time.delay(100)
  83. pygame.transform.scale(screenbuf,(RANGE_X+1, RANGE_Y+1),screen)
  84. pygame.display.update()
  85. if(len(nbs)-1) >= 0:
  86. rnd = random.randint(0,len(nbs)-1)
  87. np = field[nbs[rnd].x][nbs[rnd].y]
  88. np.visit(surface,p)
  89. p = np
  90. history.append(p)
  91. else:
  92. if(len(history) > maxl):
  93. maxl = len(history)
  94. history.remove(np)
  95. p = history[len(history)-2]
  96. if(len(history) == 1):
  97. #pygame.time.delay(200)
  98. pygame.transform.scale(screenbuf,(RANGE_X+1, RANGE_Y+1),screen)
  99. pygame.display.update()
  100. print("Done! step count: " + str(step))
  101. print("Max history length: " + str(maxl))
  102. break
  103. return field
  104.  
  105. screen = pygame.display.set_mode((RANGE_X+1, RANGE_Y+1))
  106. screenbuf = pygame.Surface(((RANGE_X/2)+1, (RANGE_Y/2)+1))
  107. field = [[Coord(x,y) for y in xrange(RANGE_Y/2)] for x in xrange(RANGE_X/2)]
  108. screen.fill(COL_UNVISITED)
  109. pygame.display.update()
  110. bg = pygame.Surface((6,6))
  111.  
  112. while 1:
  113. for event in pygame.event.get():
  114. if event.type == (pygame.QUIT):
  115. sys.exit()
  116. if event.type == (pygame.KEYDOWN):
  117. constructMaze(screenbuf)
  118. pygame.display.update()
  119. pygame.time.delay(1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement