import os, sys import pygame from pygame.locals import * import random #Variation v = 5 #3x3 Islands high and low islLow = 30 islHigh = 60 #5x5 Islands high and low islLow2 = 150 islHigh2 = 210 #SWITCH print rough map roughmap = 0 #Number of runs through smoothing proceedure. smoothruns = 2 #Characters ##Lowest (water) char1 = " " ##just above sea level (beach?) char2 = "." ##Land char3 = '"' ##elevation char4 = "#" ##hill char5 = "O" ##mountain char6 = "0" ##peak char7 = "^" # map width = 80 height = 60 #Max array values x,y maxx = width - 4 maxy = height - 4 map = [[0 for w in range(width)] for h in range(height)] class Box(pygame.sprite.Sprite): def __init__(self, color, initial_position): # All sprite classes should extend pygame.sprite.Sprite. This # gives you several important internal methods that you probably # don't need or want to write yourself. Even if you do rewrite # the internal methods, you should extend Sprite, so things like # isinstance(obj, pygame.sprite.Sprite) return true on it. pygame.sprite.Sprite.__init__(self) # Create the image that will be displayed and fill it with the # right color. self.image = pygame.Surface([10, 10]) self.image.fill(color) # Make our top-left corner the passed-in location. self.rect = self.image.get_rect() self.rect.topleft = initial_position sprites = [] #### #Print map #### def printMapGame(): posx = 0 posy = 0 for y in map: for x in y: if (x == 1): b = Box([255, 232, 107], [(10*posx), (10*posy)]) # Make the box red in the top left sprites.append(b) if (x == 2): b = Box([184, 255, 107], [(10*posx), (10*posy)]) # Make the box red in the top left sprites.append(b) if (x == 3): b = Box([111, 195, 21], [(10*posx), (10*posy)]) # Make the box red in the top left sprites.append(b) if (x == 4): b = Box([113, 124, 42], [(10*posx), (10*posy)]) # Make the box red in the top left sprites.append(b) if (x == 5): b = Box([181, 181, 181], [(10*posx), (10*posy)]) # Make the box red in the top left sprites.append(b) if (x > 5): b = Box([94, 94, 94], [(10*posx), (10*posy)]) # Make the box red in the top left sprites.append(b) posx +=1 posy +=1 posx = 0 #### #Print map #### def printMap(): print "-------------------------------------------------------------------" line = "" for y in map: for x in y: if (x <= 0): line += char1 if (x == 1): line += char2 if (x == 2): line += char3 if (x == 3): line += char4 if (x == 4): line += char5 if (x == 5): line += char6 if (x > 5): line += char7 print line line = "" #### #Set a 3x3 square of blocks to a height #### def largeBox(x,y,set): map[y-1][x-1] += set map[y][x-1] += set map[y+1][x-1] += set map[y-1][x] += set map[y][x] += set map[y+1][x] += set map[y-1][x+1] += set map[y][x+1] += set map[y+1][x+1] += set #### #Set a 5x5 square of blocks to a height #### def largeBox2(x,y,set): map[y-2][x-2] += set map[y-2][x-1] += set map[y-2][x] += set map[y-2][x+1] += set map[y-2][x+2] += set map[y-1][x-2] += set map[y-1][x+2] += set map[y-1][x-2] += set map[y][x-2] += set map[y][x+2] += set map[y+1][x-2] += set map[y+1][x+2] += set map[y+2][x-2] += set map[y+2][x-1] += set map[y+2][x] += set map[y+2][x+1] += set map[y+2][x+2] += set map[y-1][x-2] += set map[y-1][x-1] += set map[y][x-1] += set map[y+1][x-1] += set map[y-1][x] += set map[y][x] += set map[y+1][x] += set map[y-1][x+1] += set map[y][x+1] += set map[y+1][x+1] += set #### #Take the average of all surrounding squares and smooth out the map #### def getSurround(x,y, variant): new = 0 #print str(x) + "," + str(y) if (y > 0): new += map[y-1][x] if (y > 0): new += map[y-1][x] if (x > 0): new += map[y][x-1] if (y > 0 and x > 0): new += map[y-1][x-1] if (y < maxy and x < maxx): new += map[y+1][x+1] if (y < maxy): new += map[y+1][x] if (x < maxx): new += map[y][x+1] if (y < maxy and x > maxx): new += map[y+1][x-1] if (y > maxy and x < maxx): new += map[y-1][x+1] new += map[y][x] new += variant new = new/9 map[y][x] = new #### #Start by setting island blocks to 5 #### #3x3 random.seed() islands = random.randint(islLow,islHigh) for t in xrange(islands): rx = random.randint(0,maxx) ry = random.randint(0,maxy) largeBox(rx,ry,5) #5x5 islands2 = random.randint(islLow2,islHigh2) for t in xrange(islands2): rx = random.randint(0,maxx) ry = random.randint(0,maxy) largeBox2(rx,ry,5) if (roughmap): printMap() #### #Go through block by block and round the value based on what's around it tweeked with variation #### for i in range(0,smoothruns): random.seed() thisx = 0 thisy = 0 for y in map: for x in y: variant = random.randint((-1*v),v) getSurround(thisx, thisy, variant) thisx += 1 thisy += 1 thisx = 0 pygame.init() screen = pygame.display.set_mode((800, 600)) pygame.display.set_caption('Heightmap') pygame.mouse.set_visible(0) clock = pygame.time.Clock() background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((0, 0, 250)) screen.blit(background, (0, 0)) pygame.display.flip() ret = 1 #### #Print map with characters #### printMap() printMapGame() while ret == 1: clock.tick(60) for event in pygame.event.get(): if event.type == QUIT: ret = 0 elif event.type == KEYDOWN and event.key == K_ESCAPE: ret = 0 screen.blit(background, (0, 0)) for x in sprites: screen.blit(x.image, x.rect) pygame.display.flip()