def draw(img, screen, grid, bgGrid, screenW, screenH): #originPoint, viewH viewW (IN GRIDS) origin = grid.origin for y in range(0, screenH): for x in range(0, screenW): p = entities.Point(x + origin.x, y + origin.y) value = model.get_cell(grid, p) bgValue = model.get_cell(bgGrid, p) #Background handling - concrete and grass screen.blit(img.Background, (x * CELL_SIZE, y * CELL_SIZE)) if bgValue == model.CONCRETE: screen.blit(img.Concrete, (x * CELL_SIZE, y * CELL_SIZE)) #Entity handling if value == model.GATHERER: g = grid.entityList[controller.findEntity(grid.entityList, p)] if g.move: screen.blit(img.gathSprite, (x * CELL_SIZE, y * CELL_SIZE)) else: screen.blit(img.consumeList[g.animationLoop], (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.GENERATOR: g = grid.entityList[controller.findEntity(grid.entityList, p)] screen.blit(img.marketList[g.times], (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.RESOURCE: screen.blit(img.resSprite, (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.OBSTACLE: screen.blit(img.Rock, (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.TRAIL: screen.blit(img.Trail, (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.TRANSFORM: g = grid.entityList[controller.findEntity(grid.entityList, p)] screen.blit(img.transformList[g.times - 1], (x * CELL_SIZE, y * CELL_SIZE)) #if click-drag is used to place these, and then tried to transform, #get error mPointx = grid.mouseHover.x + origin.x mPointy = grid.mouseHover.y + origin.y mPoint = entities.Point(mPointx, mPointy) hoverVal = model.get_cell(grid, mPoint) if hoverVal == model.EMPTY: screen.blit( img.greenBox, (grid.mouseHover.x * CELL_SIZE, grid.mouseHover.y * CELL_SIZE)) else: screen.blit( img.redBox, (grid.mouseHover.x * CELL_SIZE, grid.mouseHover.y * CELL_SIZE))
def draw(img, screen, grid, bgGrid, screenW, screenH): #originPoint, viewH viewW (IN GRIDS) origin = grid.origin for y in range(0, screenH): for x in range(0, screenW): p = entities.Point(x + origin.x, y + origin.y) value = model.get_cell(grid, p) bgValue = model.get_cell(bgGrid, p) #Background handling - concrete and grass screen.blit(img.Background, (x * CELL_SIZE, y * CELL_SIZE)) if bgValue == model.CONCRETE: screen.blit(img.Concrete, (x * CELL_SIZE, y * CELL_SIZE)) #Entity handling if value == model.GATHERER: g = grid.entityList[controller.findEntity(grid.entityList, p)] if g.move: screen.blit(img.gathSprite, (x * CELL_SIZE, y * CELL_SIZE)) else: screen.blit(img.consumeList[g.animationLoop], (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.GENERATOR: g = grid.entityList[controller.findEntity(grid.entityList, p)] screen.blit(img.marketList[g.times], (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.RESOURCE: screen.blit(img.resSprite, (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.OBSTACLE: screen.blit(img.Rock, (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.TRAIL: screen.blit(img.Trail, (x * CELL_SIZE, y * CELL_SIZE)) elif value == model.TRANSFORM: g = grid.entityList[controller.findEntity(grid.entityList, p)] screen.blit(img.transformList[g.times - 1], (x * CELL_SIZE, y * CELL_SIZE)) #if click-drag is used to place these, and then tried to transform, #get error mPointx = grid.mouseHover.x + origin.x mPointy = grid.mouseHover.y + origin.y mPoint = entities.Point(mPointx, mPointy) hoverVal = model.get_cell(grid, mPoint) if hoverVal == model.EMPTY: screen.blit(img.greenBox, (grid.mouseHover.x * CELL_SIZE, grid.mouseHover.y * CELL_SIZE)) else: screen.blit(img.redBox, (grid.mouseHover.x * CELL_SIZE, grid.mouseHover.y * CELL_SIZE))
def resourceClick(grid, point, entityL): if model.get_cell(grid, point) == 3: print ("Clicked Resource at ", point.x, point.y) return model.spawnResources(grid, point, entityL, 2, 1) else: print ("Did not click resource") return []
def save(grid, bgGrid): entityList = grid.entityList f = open('gaia.sav', 'w') for entity in entityList: if isinstance(entity, entities.CSCStudent): f.write('gatherer ' + str(entity.position.x) + ' ' + str(entity.position.y) + ' ' + str(entity.rate) + '\n') elif isinstance(entity, entities.CampusMarket): f.write('generator ' + str(entity.position.x) + ' ' + str(entity.position.y) + '\n') elif isinstance(entity, entities.MonsterEnergy): f.write('resource ' + str(entity.position.x) + ' ' + str(entity.position.y) + '\n') elif isinstance(entity, entities.Obstacle): f.write('obstacle ' + str(entity.position.x) + ' ' + str(entity.position.y) + '\n') for x in range(0, bgGrid.width): for y in range(0, bgGrid.height): p = entities.Point(x, y) if model.get_cell(bgGrid, p) == model.CONCRETE: f.write('concrete ' + str(x) + ' ' + str(y) + '\n') f.close() print("You saved the file!")
def determineNewGathererPosition(grid, gatherer, resource): #Determines position one grixel toward the CLOSEST resource in both the x and y axis #Eventually, will determine the closest resource of a list of resources oldx = gatherer.position.x oldy = gatherer.position.y oldpoint = entities.Point(oldx, oldy) if resource.position.x > gatherer.position.x: gatherer.position.x += 1 elif resource.position.x < gatherer.position.x: gatherer.position.x -= 1 if model.get_cell(grid, gatherer.position) == 4: gatherer.position.x = oldx #Handling Y if resource.position.y > gatherer.position.y: gatherer.position.y += 1 elif resource.position.y < gatherer.position.y: gatherer.position.y -= 1 if model.get_cell(grid, gatherer.position) == 4: gatherer.position.y = oldy if not samePt(gatherer.position, oldpoint): model.set_cell(grid, oldpoint, 5)
def determineNewGathererPosition(grid, gat, res): #Determines position one grixel toward the CLOSEST resource in both the x and y axis #Eventually, will determine the closest resource of a list of resources newGathP = entities.Point(gat.position.x, gat.position.y) diry = gat.diry dirx = gat.dirx if not isinstance(res, entities.MonsterEnergy): return if not (gat.position.x == res.position.x) and not gat.priY: direction = res.position.x - gat.position.x unitDir = int(direction / abs(direction)) #will be either +1 or -1 newGathP.x += unitDir cellVal = model.get_cell(grid, newGathP) if not canMove(cellVal): newGathP.x -= unitDir newGathP.y += diry cellVal = model.get_cell(grid, newGathP) if not canMove(cellVal): newGathP.y -= 2 * diry cellVal = model.get_cell(grid, newGathP) if not canMove(cellVal): newGathP.y -= diry newGathP.x -= unitDir gat.priY = True elif not gat.position.y == res.position.y or gat.priY: direction = res.position.y - gat.position.y if direction == 0: direction = 1 unitDir = int(direction / abs(direction) ) newGathP.y += unitDir gat.priY = False cellVal = model.get_cell(grid, newGathP) if not canMove(cellVal): newGathP.y -= unitDir newGathP.x += dirx gat.priY = True cellVal = model.get_cell(grid, newGathP) if not canMove(cellVal): newGathP.x -= 2 * dirx cellVal = model.get_cell(grid, newGathP) if not canMove(cellVal): newGathP.x -= dirx newGathP.y -= unitDir gat.priY = False gat.position = newGathP
def updateGatherers(grid): resList = listOfResources(grid.entityList) gathList = listOfGatherers(grid.entityList) for gatherer in gathList: if gatherer.move == True: if isinstance(gatherer.aim, entities.MonsterEnergy): pass if not (model.get_cell(grid, gatherer.aim.position) == model.RESOURCE): gatherer.aim = None else: if len(resList) < 1: gatherer.aim = None elif gatherer.direction == entities.NEAR: gatherer.aim = determineNearest(resList, gatherer) elif gatherer.direction == entities.FAR: gatherer.aim = determineFarthest(resList, gatherer) if not gatherer.aim == None: setDirYandX(gatherer, gatherer.aim)
def handleRightClicks(grid, point): if not model.get_cell(grid, point) == 0: model.set_cell(grid, point, 0) for entity in grid.entityList: if samePt(entity.position, point): grid.entityList.remove(entity)
def removePrevInCell(grid, p): if not model.get_cell(grid, p) == 0: a = findEntity(grid.entityList, p) grid.entityList.pop(a)