|
发表于 2017-4-15 09:40:41
|
显示全部楼层
百度试试
- import pygame
- from pygame.locals import *
- import Mazing
- def main():
- pygame.init()
- screen = pygame.display.set_mode((600, 600))
- pygame.display.set_caption('Basic Pygame program')
- background = pygame.Surface(screen.get_size())
- background = background.convert()
- background.fill((250, 250, 250))
-
- screen.blit(background, (0, 0))
- pygame.display.flip()
- room = Mazing.Room(background, 30, 30, 10, 10, (10, 10))
- room.generate()
- room.draw()
- while 1:
- for event in pygame.event.get():
- if event.type == QUIT:
- return
-
- screen.blit(background, (0, 0))
- pygame.display.flip()
- if __name__ == '__main__':
- main()
- #=======Mazing.py========
- import MazingUnit
- import MazingWall
- import MazingGenerator
- class Room:
-
- def __init__(self, surface, roomWidthByUnit, roomHighByUnit, unitWidth, unitHigh, startPos):
- self.__surface = surface
- self.__unitWidth = unitWidth
- self.__unitHigh = unitHigh
- self.__roomWidthByUnit = roomWidthByUnit
- self.__roomHighByUnit = roomHighByUnit
- self.__startPos = startPos
-
- self._unitMap = {}
- self._wallMap_V = {}
- self._wallMap_H = {}
-
- for x in range(0, roomWidthByUnit):
- for y in range(0, roomHighByUnit):
- (startX, startY) = startPos
- self._unitMap[(x,y)] = MazingUnit.Unit(
- surface,
- unitWidth,
- unitHigh,
- (startX + x * unitWidth, startY + y * unitHigh),
- (x,y))
-
- for x in range(0, roomWidthByUnit):
- for y in range(0, roomHighByUnit):
- if x > 0:
- self._unitMap[(x-1,y)]._unit_2 = \
- self._unitMap[(x,y)]
- if y > 0:
- self._unitMap[(x,y-1)]._unit_3 = \
- self._unitMap[(x,y)]
- if x != roomWidthByUnit - 1:
- self._unitMap[(x+1,y)]._unit_0 = \
- self._unitMap[(x,y)]
- if y != roomHighByUnit - 1:
- self._unitMap[(x,y+1)]._unit_1 = \
- self._unitMap[(x,y)]
-
- for x in range(0, roomWidthByUnit + 1):
- for y in range(0, roomHighByUnit + 1):
- (startX, startY) = startPos
- self._wallMap_H[(x,y)] = MazingWall.Wall(surface, (startX + x * unitWidth, startY + y * unitHigh), (startX + (x+1) * unitWidth, startY + y * unitHigh), (x,y))
- self._wallMap_V[(x,y)] = MazingWall.Wall(surface, (startX + x * unitWidth, startY + y * unitHigh), (startX + x * unitWidth, startY + (y+1) * unitHigh), (x,y))
- if x != roomWidthByUnit and y != roomHighByUnit:
- self._unitMap[(x,y)]._wall_1 = self._wallMap_H[(x,y)]
- self._unitMap[(x,y)]._wall_0 = self._wallMap_V[(x,y)]
- self._wallMap_V[(x,y)]._isBlock = True
- self._wallMap_H[(x,y)]._isBlock = True
- if x != roomWidthByUnit and y > 0:
- self._unitMap[(x,y-1)]._wall_3 = self._wallMap_H[(x,y)]
- self._wallMap_H[(x,y)]._isBlock = True
- if x > 0 and y != roomHighByUnit:
- self._unitMap[(x-1,y)]._wall_2 = self._wallMap_V[(x,y)]
- self._wallMap_V[(x,y)]._isBlock = True
-
- def getRoomWidthByUnit(self):
- return self.__roomWidthByUnit
- def getRoomHighByUnit(self):
- return self.__roomHighByUnit
-
- def draw(self):
- for x in range(0, self.__roomWidthByUnit + 1):
- for y in range(0, self.__roomHighByUnit + 1):
- self._wallMap_H[(x,y)].draw()
- self._wallMap_V[(x,y)].draw()
-
-
- def generate(self):
- gen = MazingGenerator.Generator()
- gen.randomWallGen(self)
- gen.cutWind(self)
-
-
- #=======MazingGenerator.py============
- import random
- import time
- class Generator:
- def __init__(self):
- self.__windList = []
- self.__unitMapCopy = {}
- def randomWallGen(self, room):
- threshold = 5
- random.seed(time.gmtime())
- for x in range(0, room.getRoomWidthByUnit()):
- for y in range(1, room.getRoomHighByUnit()):
- i = random.randint(1, 10)
- if i > threshold:
- room._wallMap_H[(x,y)]._isBlock = False
- return True
-
- def cutWind(self, room):
- while(1):
- self.__unitMapCopy = room._unitMap.copy()
- self.__windList = []
- while(0 != len(self.__unitMapCopy)):
- wind = []
- self.__findAndCutOneWind(wind)
- self.__windList.append(wind)
- for wind in self.__windList:
- self.__linkWind(wind)
- room._unitMap.clear()
- for wind in self.__windList:
- for unit in wind:
- room._unitMap[unit.getPosByUnit()] = unit
- print("wind list len = %d" % len(self.__windList))
- if 1 == len(self.__windList):
- break
-
-
- def __findAndCutOneWind(self, wind):
- print ("Enter __findAndCutOneWind")
- keys = self.__unitMapCopy.keys()
- unit = self.__unitMapCopy.pop(keys[0])
- wind.append(unit)
- print(unit.getPosByUnit())
- i = -1
- while(i < len(wind) - 1):
- i = i + 1
- unit = wind[i]
- if None != unit.move_0() and unit.move_0() not in wind:
- self.__unitMapCopy.pop(unit.move_0().getPosByUnit())
- wind.append(unit.move_0())
- print(unit.getPosByUnit())
- if None != unit.move_1() and unit.move_1() not in wind:
- self.__unitMapCopy.pop(unit.move_1().getPosByUnit())
- wind.append(unit.move_1())
- print(unit.getPosByUnit())
- if None != unit.move_2() and unit.move_2() not in wind:
- self.__unitMapCopy.pop(unit.move_2().getPosByUnit())
- wind.append(unit.move_2())
- print(unit.getPosByUnit())
- if None != unit.move_3() and unit.move_3() not in wind :
- self.__unitMapCopy.pop(unit.move_3().getPosByUnit())
- wind.append(unit.move_3())
- print(unit.getPosByUnit())
-
- def __linkWind(self, wind):
- cutNum = 0
- for unit in wind:
- if cutNum >= 1:
- break
- i = random.randint(0,4)
- if 0 == i:
- if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
- unit._wall_0._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
- unit._wall_1._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
- unit._wall_2._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
- unit._wall_3._isBlock = False
- cutNum = cutNum + 1
- continue
- if 1 == i:
- if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
- unit._wall_3._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
- unit._wall_0._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
- unit._wall_1._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
- unit._wall_2._isBlock = False
- cutNum = cutNum + 1
- continue
- if 2 == i:
- if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
- unit._wall_2._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
- unit._wall_3._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
- unit._wall_0._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
- unit._wall_1._isBlock = False
- cutNum = cutNum + 1
- continue
- if 3 == i:
- if None == unit.move_1() and None != unit._unit_1 and unit._unit_1 not in wind:
- unit._wall_1._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_2() and None != unit._unit_2 and unit._unit_2 not in wind:
- unit._wall_2._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_3() and None != unit._unit_3 and unit._unit_3 not in wind:
- unit._wall_3._isBlock = False
- cutNum = cutNum + 1
- continue
- if None == unit.move_0() and None != unit._unit_0 and unit._unit_0 not in wind:
- unit._wall_0._isBlock = False
- cutNum = cutNum + 1
- continue
- #=========MazingUnit.py=========
- class Unit:
- def __init__(self, surface, width, high, posStart, posByUnit):
- self.__surface = surface
- self.__width = width
- self.__high = high
- self.__posStart = posStart
- self.__posByUnit = posByUnit
-
- self._wall_0 = None
- self._wall_1 = None
- self._wall_2 = None
- self._wall_3 = None
- self._unit_0 = None
- self._unit_1 = None
- self._unit_2 = None
- self._unit_3 = None
-
- def getPosByUnit(self):
- return self.__posByUnit
-
- def getPosStart(self):
- return self.__posStart
-
- def move_0(self):
- if False == self._wall_0._isBlock:
- return self._unit_0
- else:
- return None
-
- def move_1(self):
- if False == self._wall_1._isBlock:
- return self._unit_1
- else:
- return None
-
- def move_2(self):
- if False == self._wall_2._isBlock:
- return self._unit_2
- else:
- return None
-
- def move_3(self):
- if False == self._wall_3._isBlock:
- return self._unit_3
- else:
- return None
-
- #=======MazingWall.py===============
- import pygame
- from pygame.locals import *
- class Wall:
- def __init__(self, surface, posStartByUnit, posEndByUnit, pos):
- self.__surface = surface
- self.__posStartByUnit = posStartByUnit
- self.__posEndByUnit = posEndByUnit
- self.__pos = pos
- self._isBlock = False
-
- def draw(self):
- if True == self._isBlock:
- pygame.draw.line(self.__surface, (10, 10, 10), self.__posStartByUnit, self.__posEndByUnit, 1)
复制代码 |
|