本文整理汇总了Python中direct.showbase.RandomNumGen.RandomNumGen.randint方法的典型用法代码示例。如果您正苦于以下问题:Python RandomNumGen.randint方法的具体用法?Python RandomNumGen.randint怎么用?Python RandomNumGen.randint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类direct.showbase.RandomNumGen.RandomNumGen
的用法示例。
在下文中一共展示了RandomNumGen.randint方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ADBakery
# 需要导入模块: from direct.showbase.RandomNumGen import RandomNumGen [as 别名]
# 或者: from direct.showbase.RandomNumGen.RandomNumGen import randint [as 别名]
class ADBakery(Bakery):
"""
A factory for tiles based on panda3d's perlin noise
"""
def __init__(self, editorFile, bakeryFolder):
#id is a seed for the map and unique name for any cached heightmap images
self.dice = RandomNumGen(TimeVal().getUsec())
self.id = self.dice.randint(2, 1000000)
# the overall smoothness/roughness of the terrain
smoothness = 80
# how quickly altitude and roughness shift
self.consistency = smoothness * 8
# waterHeight is expressed as a multiplier to the max height
self.waterHeight = 0.3
# for realism the flatHeight should be at or very close to waterHeight
self.flatHeight = self.waterHeight + 0.04
#creates noise objects that will be used by the getHeight function
"""Create perlin noise."""
# See getHeight() for more details....
# where perlin 1 is low terrain will be mostly low and flat
# where it is high terrain will be higher and slopes will be exagerrated
# increase perlin1 to create larger areas of geographic consistency
self.perlin1 = StackedPerlinNoise2()
perlin1a = PerlinNoise2(0, 0, 256, seed=self.id)
perlin1a.setScale(self.consistency)
self.perlin1.addLevel(perlin1a)
perlin1b = PerlinNoise2(0, 0, 256, seed=self.id * 2 + 123)
perlin1b.setScale(self.consistency / 2)
self.perlin1.addLevel(perlin1b, 1 / 2)
# perlin2 creates the noticeable noise in the terrain
# without perlin2 everything would look unnaturally smooth and regular
# increase perlin2 to make the terrain smoother
self.perlin2 = StackedPerlinNoise2()
frequencySpread = 3.0
amplitudeSpread = 3.4
perlin2a = PerlinNoise2(0, 0, 256, seed=self.id * 2)
perlin2a.setScale(smoothness)
self.perlin2.addLevel(perlin2a)
perlin2b = PerlinNoise2(0, 0, 256, seed=self.id * 3 + 3)
perlin2b.setScale(smoothness / frequencySpread)
self.perlin2.addLevel(perlin2b, 1 / amplitudeSpread)
perlin2c = PerlinNoise2(0, 0, 256, seed=self.id * 4 + 4)
perlin2c.setScale(smoothness / (frequencySpread * frequencySpread))
self.perlin2.addLevel(perlin2c, 1 / (amplitudeSpread * amplitudeSpread))
perlin2d = PerlinNoise2(0, 0, 256, seed=self.id * 5 + 5)
perlin2d.setScale(smoothness / (math.pow(frequencySpread, 3)))
self.perlin2.addLevel(perlin2d, 1 / (math.pow(amplitudeSpread, 3)))
perlin2e = PerlinNoise2(0, 0, 256, seed=self.id * 6 + 6)
perlin2e.setScale(smoothness / (math.pow(frequencySpread, 4)))
self.perlin2.addLevel(perlin2e, 1 / (math.pow(amplitudeSpread, 4)))
def hasTile(self, xStart, yStart, tileSize):
"""
If one is using a cashed tile source instead of a live bakery, this would be sometimes be false
"""
return True
def getTile(self, xStart, yStart, scale):
"""
returns a tile for the specified positions and size
"""
sizeY = tileMapSize
sizeX = tileMapSize
getHeight = self.getHeight
noiseTex=Texture("NoiseTex")
noiseTex.setup2dTexture(sizeX, sizeY, Texture.TUnsignedByte, Texture.FRgb)
p=noiseTex.modifyRamImage()
step=noiseTex.getNumComponents()*noiseTex.getComponentWidth()
scalar=.4
for y in range(sizeY):
yPos=scalar*(1.0*y*scale/(sizeY-1)+yStart)
for x in range(sizeX):
height = getHeight(scalar*(1.0*x*scale/(sizeX-1) + xStart), yPos)
r=min(255,max(0,height*256))
g=r*256
b=g*256
index = (sizeX * y + x)*step
p.setElement(index, b%256)#Blue
p.setElement(index+1, g%256)#Green
p.setElement(index+2, r)#Red
return Tile({"height":Map("height", noiseTex)},[], xStart, yStart, scale)
def asyncGetTile(self, xStart, yStart, scale, callback, callbackParams=[]):
"""
like getTile, but calls callback(tile,*callbackParams) when done
"""
callback(self.getTile(xStart, yStart, scale), *callbackParams)
def getHeight(self, x, y):
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: from direct.showbase.RandomNumGen import RandomNumGen [as 别名]
# 或者: from direct.showbase.RandomNumGen.RandomNumGen import randint [as 别名]
class CogdoMazeFactory:
def __init__(self, randomNumGen, width, height, frameWallThickness = Globals.FrameWallThickness, cogdoMazeData = CogdoMazeData):
self._rng = RandomNumGen(randomNumGen)
self.width = width
self.height = height
self.frameWallThickness = frameWallThickness
self._cogdoMazeData = cogdoMazeData
self.quadrantSize = self._cogdoMazeData.QuadrantSize
self.cellWidth = self._cogdoMazeData.QuadrantCellWidth
def getMazeData(self):
if not hasattr(self, '_data'):
self._generateMazeData()
return self._data
def createCogdoMaze(self, flattenModel = True):
if not hasattr(self, '_maze'):
self._loadAndBuildMazeModel(flatten=flattenModel)
return CogdoMaze(self._model, self._data, self.cellWidth)
def _gatherQuadrantData(self):
self.openBarriers = []
barrierItems = range(Globals.TotalBarriers)
self._rng.shuffle(barrierItems)
for i in barrierItems[0:len(barrierItems) - Globals.NumBarriers]:
self.openBarriers.append(i)
self.quadrantData = []
quadrantKeys = self._cogdoMazeData.QuadrantCollisions.keys()
self._rng.shuffle(quadrantKeys)
i = 0
for y in range(self.height):
for x in range(self.width):
key = quadrantKeys[i]
collTable = self._cogdoMazeData.QuadrantCollisions[key]
angle = self._cogdoMazeData.QuadrantAngles[self._rng.randint(0, len(self._cogdoMazeData.QuadrantAngles) - 1)]
self.quadrantData.append((key, collTable[angle], angle))
i += 1
if x * y >= self._cogdoMazeData.NumQuadrants:
i = 0
def _generateBarrierData(self):
data = []
for y in range(self.height):
data.append([])
for x in range(self.width):
if x == self.width - 1:
ax = -1
else:
ax = 1
if y == self.height - 1:
ay = -1
else:
ay = 1
data[y].append([ax, ay])
dirUp = 0
dirDown = 1
dirLeft = 2
dirRight = 3
def getAvailableDirections(ax, ay, ignore = None):
dirs = []
if ax - 1 >= 0 and data[ay][ax - 1][BARRIER_DATA_RIGHT] == 1 and (ax, ay) != ignore:
dirs.append(dirLeft)
if ax + 1 < self.width and data[ay][ax][BARRIER_DATA_RIGHT] == 1 and (ax, ay) != ignore:
dirs.append(dirRight)
if ay - 1 >= 0 and data[ay - 1][ax][BARRIER_DATA_TOP] == 1 and (ax, ay) != ignore:
dirs.append(dirDown)
if ay + 1 < self.height and data[ay][ax][BARRIER_DATA_TOP] == 1 and (ax, ay) != ignore:
dirs.append(dirUp)
return dirs
visited = []
def tryVisitNeighbor(ax, ay, ad):
if ad == dirUp:
if data[ay][ax] in visited:
return None
visited.append(data[ay][ax])
data[ay][ax][BARRIER_DATA_TOP] = 0
ay += 1
elif ad == dirDown:
if data[ay - 1][ax] in visited:
return None
visited.append(data[ay - 1][ax])
data[ay - 1][ax][BARRIER_DATA_TOP] = 0
ay -= 1
elif ad == dirLeft:
if data[ay][ax - 1] in visited:
return None
visited.append(data[ay][ax - 1])
data[ay][ax - 1][BARRIER_DATA_RIGHT] = 0
ax -= 1
elif ad == dirRight:
if data[ay][ax] in visited:
return None
visited.append(data[ay][ax])
data[ay][ax][BARRIER_DATA_RIGHT] = 0
#.........这里部分代码省略.........
示例3: MazeAI
# 需要导入模块: from direct.showbase.RandomNumGen import RandomNumGen [as 别名]
# 或者: from direct.showbase.RandomNumGen.RandomNumGen import randint [as 别名]
#.........这里部分代码省略.........
self.visTrav.traverse(self.visCollWalls)
# Now see which segments detected a collision.
for entry in self.visQueue.getEntries():
seg = entry.getFrom()
if seg in segs:
del segs[seg]
# If any are left, we've got a line of sight.
if segs:
return True
# If none are left, we're blocked.
return False
def __generateMazeSteps(self):
""" Moves all of the active paths forward one step. """
if not self.paths:
# Ran out of open paths. Go find a likely square to pick
# up from again.
nextStep = self.__findEmptySquare()
self.paths.append(nextStep)
paths = self.paths
self.paths = []
for sx, sy, dir in paths:
self.__generateMazeOneStep(sx, sy, dir)
def __generateMazeOneStep(self, sx, sy, dir):
""" Moves this path forward one step. """
if self.random.randint(0, 99) < self.StopChance:
# Abandon this path.
return
numNextSteps = 1
while numNextSteps < 4 and self.random.randint(0, 99) < self.ForkChance:
# Consider a fork.
numNextSteps += 1
nextDirs = Globals.AllDirsList[:]
nextDirs.remove(dir)
self.random.shuffle(nextDirs)
if self.random.randint(0, 99) < self.TurnChance:
# Consider a turn. Put the current direction at the end.
nextDirs.append(dir)
else:
# Don't consider a turn. Put the current direction at the
# front.
nextDirs = [dir] + nextDirs
for dir in nextDirs:
nextStep = self.__makeStep(sx, sy, dir)
if nextStep:
# That step was valid, save the current path for next
# pass.
self.numSquares -= 1
self.paths.append(nextStep)
numNextSteps -= 1
if numNextSteps == 0:
return
# Try the next direction.