Created
June 20, 2012 20:07
-
-
Save tarmolehtpuu/2961903 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image | |
import random | |
TERRAIN_GRASS = 0 | |
TERRAIN_SAND = 1 | |
TERRAIN_WATER = 2 | |
TERRAIN_DIRT = 3 | |
COLORS = [ | |
(0x52, 0x65, 0x35, 0x00), | |
(0xFF, 0xCC, 0x99, 0x00), | |
(0x00, 0xBF, 0xFF, 0x00), | |
(0x78, 0x48, 0x00, 0x00) | |
] | |
PROBABILITIES = [ | |
[0.25, 0.5, 0.75, 1], | |
[0.25, 0.5, 0.75, 1], | |
[0.25, 0.5, 0.75, 1], | |
[0.25, 0.5, 0.75, 1], | |
] | |
class Markov(object): | |
def __init__(self, width, height): | |
self.width = width | |
self.height = height | |
def step(self): | |
r = random.random() | |
for index, probability in enumerate(PROBABILITIES[self.state]): | |
if r <= probability: | |
self.state = index | |
self.buffer.append(COLORS[self.state]) | |
break | |
def walk(self): | |
self.state = TERRAIN_GRASS | |
self.buffer = [] | |
for i in range(self.width * self.height): | |
self.step() | |
def dump(self, file): | |
image = Image.new("RGBA", (self.width, self.height)) | |
image.putdata(self.buffer) | |
image.save(file) | |
m = Markov(100, 100) | |
m.walk() | |
m.dump("map.jpeg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment