Skip to content
universe.py 2.33 KiB
Newer Older
Kevin Whitaker's avatar
Kevin Whitaker committed
# Copyright (c)2012 Eyecreate Studios
import random

class universe:

	def __init__(self):
		self.width=200
		self.height=200
		random.seed(None)
		self.winLocation=[random.randint(0,self.width),random.randint(0,self.height)]
		self.grid = [[0 for col in range(self.width)] for row in range(self.height)]
		self.startLocation=self.getCloseLocation(self.winLocation[0],self.winLocation[1])
		#Uncomment these if you like cheating
		#print self.startLocation
		#print self.winLocation
		#print "distx:"+str(abs(self.startLocation[0]-self.winLocation[0]))+" disty:"+str(abs(self.startLocation[1]-self.winLocation[1]))
		#print "approx fuel to destination:"+str(abs(self.startLocation[0]-self.winLocation[0])+abs(self.startLocation[1]-self.winLocation[1]))

	def getView(self,cX,cY,width,height):
		screen = [[0 for row in range(5)] for col in range(8)] #for now, grid size is hardcoded. 
		rangeX = range(cX-4,cX+4)
		rangeY = range(cY-2,cY+3)
		# check if view is outside of bounds
		if cX-4<0: rangeX = range(width)
		if cY-2<0: rangeY = range(height)
		if cX+4>self.width: rangeX = range(self.width-width,self.width)
		if cY+3>self.height: rangeY = range(self.height-height,self.height)
		for rIndex in rangeX:
			for cIndex in rangeY:
				screen[rIndex-rangeX[0]][cIndex-rangeY[0]]=self.grid[rIndex][cIndex]
		return screen,[cX-rangeX[0],cY-rangeY[0]]
		
	def getCloseLocation(self,locX,locY):
		return self.quickCoordinateCheck(random.randint(locX-(self.width/4),locX+(self.width/4)),random.randint(locY-(self.height/4),locY+(self.height/4)),self.width/2)

	def getStartLocation(self):
		return self.startLocation

	def getWinLocation(self):
		return self.winLocation

	def quickCoordinateCheck(self,numX,numY,offset):
		#make sure ship start is on map and within fuel distance.
		retX=numX
		retY=numY
		if numX<0: retX=0
		if numX>self.width: retX=self.width-offset
		if numY<0: retY=0
		if numY>self.height: retY=self.height-offset
		return [retX,retY]
		

	def newRandomPlanet(self):
		return planet(None)

	def generatePlanets(self):
		for rIndex in range(len(self.grid)):
			for cIndex in range(len(self.grid[rIndex])):
				if random.randint(0,10)>7:
					self.grid[rIndex][cIndex]=self.newRandomPlanet()
		self.grid[self.winLocation[0]][self.winLocation[1]]=planet("winner")

class planet:
	
	def __init__(self,pType):
		self.pType=pType
		
	def getType(self):
		return self.pType