You are on page 1of 28

# HEX METRICS:

# -
# / \ | +-------+

# / hex \ | / \

# / dir \ _ | / \
# | 4 5 | | | / \
# | \ / | |s | b \ /
# | 3-----0 | | | \ /
# | / \ | _ | \ /
# \ 2 1 / | | +-------+
# \ / |h |
# \ / _ |
# -
# |------|
# r
# |-------------|
# a
import os
import pygame
from pygame.locals import *
from sys import exit
from math import *
from random import randint
from pygame.time import *
def get_hex_vertices_pixels(x, y, s):
# given pixel coordinates (x, y), return set of 6 pixel coordinates for hex
centered on (x, y) with side length s
minor_hex_s = s
minor_hex_h = sin(radians(30.0)) * minor_hex_s # h==0.33333334
minor_relative_size = minor_hex_s + minor_hex_h
minor_hex_r = cos(radians(30.0)) * minor_hex_s # r==0.57735026
minor_hex_b = minor_hex_s + 2 * minor_hex_h # height of the entire hex; b==1
.3333334

minor_hex_a = 2 * minor_hex_r # width of the entire hex; a==1.1547005
minor_hex_width = minor_hex_a
minor_hex_height = minor_hex_s + minor_hex_h # This is equal to RELATIVE_S
IZE
minor_hex_x = x - minor_hex_r
minor_hex_y = y - minor_hex_s
p1 = (minor_hex_x+minor_hex_r, minor_hex_y)
p2 = (minor_hex_x, minor_hex_y+minor_hex_h)
p3 = (minor_hex_x, minor_hex_y+1*minor_relative_size)
p4 = (minor_hex_x+minor_hex_r, minor_hex_y+minor_hex_b)
p5 = (minor_hex_x+minor_hex_a, minor_hex_y+1*minor_relative_size)
p6 = (minor_hex_x+minor_hex_a, minor_hex_y+minor_hex_h)
return (p1, p2, p3, p4, p5, p6)
# -- get_hex_vertices_pixels --
def draw_sub_hexes(x, y, surface, color, thickness):
# given hex coord (x, y), draw sub hexes
# need a function that draws subhexes based on a pixel point and a hex size
coord_x = x
coord_y = y
pixel_x = coord_x * HEX_A
if coord_y % 2 > 0:
pixel_x += HEX_R
pixel_y = coord_y * RELATIVE_SIZE
hex_center = (int(round(pixel_x + HEX_R)), int(round(pixel_y + HEX_S)))
pixel_coords = get_hex_vertices_pixels(hex_center[0], hex_center[1], HEX_S/3
)
rand = randint(1, 100)
pygame.draw.polygon(surface, color, pixel_coords, thickness)
# if rand > 50:
# pygame.draw.polygon(surface, color, pixel_coords, thickness)
# else:
# pygame.draw.polygon(surface, color, pixel_coords)
minor_hex_s = HEX_S/3
minor_hex_h = sin(radians(30.0)) * minor_hex_s # h==0.33333334
minor_relative_size = minor_hex_s + minor_hex_h
minor_hex_r = cos(radians(30.0)) * minor_hex_s # r==0.57735026
minor_hex_a = 2 * minor_hex_r # width of the entire hex; a==1.1547005

pixel_coords = get_hex_vertices_pixels(hex_center[0]-minor_hex_a, hex_center
[1], minor_hex_s)
pygame.draw.polygon(surface, color, pixel_coords, thickness)
pixel_coords = get_hex_vertices_pixels(hex_center[0]+minor_hex_a, hex_center
[1], minor_hex_s)
pygame.draw.polygon(surface, color, pixel_coords, thickness)
pixel_coords = get_hex_vertices_pixels(hex_center[0]-minor_hex_r, hex_center
[1]-minor_relative_size, minor_hex_s)
pygame.draw.polygon(surface, color, pixel_coords, thickness)
pixel_coords = get_hex_vertices_pixels(hex_center[0]+minor_hex_r, hex_center
[1]-minor_relative_size, minor_hex_s)
pygame.draw.polygon(surface, color, pixel_coords, thickness)
pixel_coords = get_hex_vertices_pixels(hex_center[0]-minor_hex_r, hex_center
[1]+minor_relative_size, minor_hex_s)
pygame.draw.polygon(surface, color, pixel_coords, thickness)
pixel_coords = get_hex_vertices_pixels(hex_center[0]+minor_hex_r, hex_center
[1]+minor_relative_size, minor_hex_s)
pygame.draw.polygon(surface, color, pixel_coords, thickness)
# -- draw_sub_hexes --
def get_neighbor_hex(x, y, direction):
# given hex(x, y), return hex in direction
if direction == HEX_EAST:
return (x+1, y)
elif direction == HEX_SOUTHEAST:
if y%2 > 0:
return (x+1, y+1)
else:
return (x, y+1)
elif direction == HEX_SOUTHWEST:
if y%2 > 0:
return (x, y+1)
else:
return (x-1, y+1)
elif direction == HEX_WEST:
return (x-1, y)
elif direction == HEX_NORTHWEST:
if y%2 > 0:
return (x, y-1)
else:
return (x-1, y-1)
else: # direction == HEX_NORTHEAST
if y%2 > 0:
return (x+1, y-1)
else:
return (x, y-1)
# -- get_neighbor_hex --
def get_neighbor_hexes(x, y):
# given hex(x, y), return list of adjacent hexes
neighboring_hexes = []
neighboring_hexes.append((x-1, y))
neighboring_hexes.append((x, y+1))
neighboring_hexes.append((x, y-1))
neighboring_hexes.append((x+1, y))
if y % 2 > 0: # odd row
neighboring_hexes.append((x+1, y-1))
neighboring_hexes.append((x+1, y+1))
else:
neighboring_hexes.append((x-1, y-1))
neighboring_hexes.append((x-1, y+1))
return neighboring_hexes
# -- get_neighbor_hexes --
def get_border_vertices(x, y):
# given hex(x, y), return list of bordering vertices
bordering_vertices = [ ]
if y % 2 > 0:
bordering_vertices.append((2*x+2, y))
bordering_vertices.append((2*x+1, y))
bordering_vertices.append((2*x+1, y+1))
bordering_vertices.append((2*x+2, y+1))
bordering_vertices.append((2*x+3, y+1))
bordering_vertices.append((2*x+3, y))
else:
bordering_vertices.append((2*x+1, y))
bordering_vertices.append((2*x, y))
bordering_vertices.append((2*x, y+1))
bordering_vertices.append((2*x+1, y+1))
bordering_vertices.append((2*x+2, y+1))
bordering_vertices.append((2*x+2, y))

return bordering_vertices
# -- get_border_vertices --
def get_border_hexes(x, y):
# given vertex(x, y), return list of bordering hexes
bordering_hexes = [ ]
x_offset = x % 2
y_offset = y % 2
if x_offset > 0:
if y_offset > 0: # x is odd, y is odd
bordering_hexes.append(((x+1)/2-1, y-1))
bordering_hexes.append(((x-1)/2-1, y))
bordering_hexes.append(((x-1)/2, y))
else: # x is odd, y is even
bordering_hexes.append((x/2-1, y-1))
bordering_hexes.append(((x-1)/2, y-1))
bordering_hexes.append((x/2, y))
else:
if y_offset > 0: # x is even, y is odd
bordering_hexes.append(((x+1)/2-1, y-1))
bordering_hexes.append(((x-1)/2+1, y-1))
bordering_hexes.append(((x-1)/2, y))
else: # x is even, y is even
bordering_hexes.append((x/2-1, y-1))
bordering_hexes.append(((x-1)/2, y))
bordering_hexes.append((x/2, y))
return bordering_hexes
# -- get_bordering_hexes --
def get_neighbor_vertices(x, y):
# given vertex(x, y), return list of adjacent vertices

#print(str(x) + ", " + str(y))
neighboring_vertices = []
neighboring_vertices.append((x-1, y))
neighboring_vertices.append((x+1, y))
odd_row = (y % 2 > 0)
odd_col = (x % 2 > 0)
if (odd_row and odd_col) or ((not odd_row) and (not odd_col)):
neighboring_vertices.append((x, y+1))
else:
neighboring_vertices.append((x, y-1))
#print("get_neighbor_vertices: " + str(neighboring_vertices))
return neighboring_vertices
# -- get_neighbor_vertices
def initialize_vertices(game_map):
# each vertex at index (x, y) is a list of the following elements:
# [0] contains a map_image pixel coord (x,y) where the vertex is displayed
# [1] contains the aggregate elevation for the vertex based on the bordering
hexes' elevation

vertices = [ ]
#initialize vertex array
for h in xrange(len(game_map)+1):
new_row = [ ]
for w in xrange(len(game_map[0])*2 + 3):
new_row.append([(-1, -1), 0])
vertices.append(new_row)
return vertices
# -- initialize_vertices --
def initialize_edges(game_map):
edges = [ ]
#initialize edge array
for h in xrange(len(game_map)+1):
new_row = [ ]
for w in xrange(len(game_map[0])*2 + 3):
new_row.append(((-1, -1),(-1, -1)))
edges.append(new_row)
return edges
# -- initialize_edges --
def get_elevation_type(elevation):
if elevation < 1:
return "WATER"
elif elevation < 3:
return "PLAINS"
elif elevation < 6:
return "FOOTHILLS"
elif elevation < 9:
return "HIGHLANDS"
elif elevation < 13:
return "MOUNTAINS"
else:
return "CRAGS"
# -- get_elevation_type
def direction_to_nearest_edge(x, y):
# given a hex, return direction to nearest map edge
if x >= len(game_map[0])/2:
if y > len(game_map)/2:
dir = HEX_SOUTHEAST
elif y < len(game_map)/2:
dir = HEX_NORTHEAST
else: # y == len(game_map)/2
dir = HEX_EAST
else: # x < len(game_map[0])/2:
if y > len(game_map)/2:
dir = HEX_SOUTHWEST
elif y < len(game_map)/2:
dir = HEX_NORTHWEST
else: # y == len(game_map)/2
dir = HEX_WEST
return dir
# -- direction_to_nearest_edge --
def check_if_ocean(x, y, depth, checked_hexes_set):
# given a water hex, determine if it an ocean hex
#print(" >> depth: " + str(depth) + " (" + str(x) + "," + str(y) + ")")
#print("check if ocean: " + str(x) + "," + str(y))
if (y == 0) or (x == 0) or (y == len(game_map)-1) or (x == len(game_map[0])-
1):
# if hex is on map-border, set to ocean and return True
game_map[y][x][2] = True
return True
elif game_map[y][x][2]:
# if hex is already known to be ocean, return True
return True
elif game_map[y][x][0] != WATER:
# if hex is not a water hex, return False
return False
elif (x, y) in checked_hexes_set:
return False
else:
# determine direction of closest map edge
dir = direction_to_nearest_edge(x, y)
#dir = HEX_NORTHWEST
#print("checking in direction " + str(dir))
path_found = False
directions_checked = 0
while not path_found and directions_checked < 6:
check_hex = get_neighbor_hex(x, y, dir)
checked_hexes_set.add((x, y))

if check_if_ocean(check_hex[0], check_hex[1], depth+1, checked_hexes
_set):
path_found = True
game_map[y][x][2] = True
else:
checked_hexes_set.add((check_hex[0], check_hex[1]))
#print(checked_hexes_set)
directions_checked += 1
dir += 1
if dir > 5:
dir = dir % 5
# end while
#print("path_found for " + str(x), "," + str(y) + ": " + str(path_found)
)
return path_found
# -- check_if_ocean --
def determine_coastal_hexes(game_map):
for y in xrange(len(game_map)):
for x in xrange(len(game_map[0])):
if game_map[y][x][0] == GRASS:
neighbors = get_neighbor_hexes(x, y)
isCoast = False
for n in xrange(len(neighbors)):
n_x = neighbors[n][0]
n_y = neighbors[n][1]
if game_map[n_y][n_x][2]: # isOcean == True
isCoast = True
if isCoast:
game_map[y][x][3] = True
# -- determine_coastal_hexes --
def determine_water_types(game_map):
checked_hexes = [ ]
checked_hexes_set = set(checked_hexes)
for y in xrange (len(game_map)):
for x in xrange (len(game_map[0])):
if game_map[y][x][0] == WATER:
check = check_if_ocean(x, y, 0, checked_hexes_set)
game_map[y][x][2] = check
# -- determine_water_types --
def create_random_map(width, height):
random_map = [ ]
#initialize map with water hexes
for h in xrange(height):
random_row = [ ]
for w in xrange(width):
# each hex has properties:
# [0] Terrain type (WATER, GRASS)
# [1] elevation
# [2] isOcean
# [3] isCoast
random_row.append([WATER, 0, False, False])
random_map.append(random_row)
#print random_row

#add random seeds
num_seeds = trunc(width * height * 0.004)
for s in xrange(num_seeds):
rand_x = randint(width / 10, width - (width / 10))
rand_y = randint(height / 10, height - (height / 10))

random_map[rand_y][rand_x][0] = GRASS
random_map[rand_y][rand_x][1] += 1

generating = True
cur_x = rand_x
cur_y = rand_y
limit = 100
while generating:
dir = randint(0, 3)
if dir == 0: #N
if cur_y > 1:
cur_y -= 1
elif dir == 1: #S
if cur_y < height - 2:
cur_y += 1
elif dir == 2: #W
if cur_x > 1:
cur_x -= 1
elif dir == 3: #E
if cur_x < width - 2:
cur_x += 1
#print("cur: " + str(cur_x) + "," + str(cur_y))
random_map[cur_y][cur_x][0] = GRASS
random_map[cur_y][cur_x][1] += 1

roll = randint(1, 100)
roll_check = randint(40, 65)
# roll check: determines how quickly each seed completes generating
land
# note these roll check note comments were made for roll check being
constant across all seeds
# the way this is currently coded it is per seed; if desired for mor
e consistency, move to outside of loop
# roll > 65 creates single large continent, ~52-58% landmass
# roll > 50 creates large continent with 1-2 medium islands, ~45-48%
landmass
# roll > 35 creates snakier continents, several medium islands, ~40%
landmass
# roll > 15 creates many small/medium islands, ~35% landmass
# roll > 5 creates many small/medium islansa, ~30-32% landmass
if roll > roll_check:
limit = limit - 1
if limit == 0:
generating = False
return random_map
# -- end create_random_map --
def post_process_map_coarse(random_map):
cur_map_height = len(random_map)
cur_map_width = len(random_map[1])
# postprocess random map
# fill in large amounts of water
for h in xrange(cur_map_height-1):
if h > 0:
row = random_map[h]
for w in xrange(cur_map_width-1):
if w > 0:
if random_map[h][w][0] == WATER:
# check if neighbors are land
neighbors = get_neighbor_hexes(w, h)
check = 0
for i in xrange(len(neighbors)):
#check if valid coords
coord = neighbors.pop()
if coord[0] >= 0 and coord[0] < cur_map_width and co
ord[1] >= 0 and coord[1] < cur_map_height:
if random_map[coord[1]][coord[0]][0] == GRASS:
check += 1
p = randint(1, 100)
if check == 6 and p <= 95:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
elif check == 5 and p <= 75:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
elif check == 4 and p <= 55:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
elif check == 3 and p <= 35:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
elif check == 2 and p <= 15:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
elif check == 1 and p <= 5:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
return random_map
# -- end post_process_map_coarse --
def post_process_map_fine(random_map):
# this just aims to fill in lakes, not radically change the coastline
cur_map_height = len(random_map)
cur_map_width = len(random_map[1])
# postprocess random map
# fill in some small lakes
for h in xrange(cur_map_height-1):
if h > 0:
row = random_map[h]
for w in xrange(cur_map_width-1):
if w > 0:
if random_map[h][w][0] == WATER:
# check if neighbors are land
neighbors = get_neighbor_hexes(w, h)
check = 0
#print str(len(neighbors))
for i in xrange(len(neighbors)):
#check if valid coords
coord = neighbors.pop()
#print "pop" + str(i)
if coord[0] >= 0 and coord[0] < cur_map_width and co
ord[1] >= 0 and coord[1] < cur_map_height:
if random_map[coord[1]][coord[0]][0] == GRASS:
check += 1
p = randint(1, 100)
if check == 6 and p <= 95:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
elif check == 5 and p <= 75:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
elif check == 4 and p <= 55:
random_map[h][w][0] = GRASS
random_map[h][w][1] += 1
return random_map
# -- end post_process_map_fine --
def generate_map():
game_map = create_random_map(MAP_WIDTH, MAP_HEIGHT)
for i in xrange(POST_PROCESS_PASSES):
game_map = post_process_map_coarse(game_map)
for i in xrange(POST_PROCESS_PASSES):
game_map = post_process_map_fine(game_map)
return game_map
# -- generate_map --
def calculate_vertex_weights(game_map):
global map_vertices
for y in xrange(len(game_map)):
if (y % 2 > 0):
is_row_odd = True
else:
is_row_odd = False
for x in xrange(len(game_map[0])):
elevation = game_map[y][x][1]
if is_row_odd:
map_vertices[y][x*2+1][1] += elevation
map_vertices[y][x*2+2][1] += elevation
map_vertices[y][x*2+3][1] += elevation
map_vertices[y+1][x*2+1][1] += elevation
map_vertices[y+1][x*2+2][1] += elevation
map_vertices[y+1][x*2+3][1] += elevation
else:
map_vertices[y][x*2][1] += elevation
map_vertices[y][x*2+1][1] += elevation
map_vertices[y+1][x*2][1] += elevation
map_vertices[y+1][x*2+1][1] += elevation
map_vertices[y+1][x*2+2][1] += elevation
map_vertices[y][x*2+2][1] += elevation

# -- calculate_vertex_weights --
def draw_map_image(game_map):
# t1 = pygame.time.get_ticks()

global map_vertices
global map_edges
global rivers
width = ceil(len(game_map[0]) * HEX_WIDTH + HEX_R) + 1
height = ceil(len(game_map) * HEX_HEIGHT + HEX_H) + 1
map_surface = pygame.Surface((width, height))
map_surface.fill(color_dodger_blue)
# calc some stats
#total_num_hexes = len(game_map) * len(game_map[0])
#num_land_hexes = 0
for y in xrange(len(game_map)):
yf = y*RELATIVE_SIZE
if (y % 2 > 0):
is_row_odd = True
else:
is_row_odd = False

for x in xrange(len(game_map[0])):
xa = x*HEX_A
if is_row_odd:
xa += HEX_R

p1 = (xa+HEX_R, yf)
p2 = (xa, yf+HEX_H)
p3 = (xa, yf+1*RELATIVE_SIZE)
p4 = (xa+HEX_R, yf+HEX_B)
p5 = (xa+HEX_A, yf+1*RELATIVE_SIZE)
p6 = (xa+HEX_A, yf+HEX_H)
if game_map[y][x][0] == WATER:
color = color_steel_blue
elif game_map[y][x][0] == GRASS:
# calc stats
#num_land_hexes += 1

#color = color_green
elevation = game_map[y][x][1]
if elevation < 3:
color = color_green
elif elevation < 6:
color = color_sawgrass
elif elevation < 9:
color = color_mesa2
elif elevation < 13:
color = color_burly_wood4
else:
color = color_sienna

else: # dirt
color = color_chocolate_brown
## if (x, y) in rivers:
#print(str(x) + "," + str(y))
## color = color_blue
pygame.draw.polygon(map_surface, color, (p1, p2, p3, p4, p5, p6))
##
if is_row_odd:
map_vertices[y][x*2+1][0] = (trunc(p2[0]), trunc(p2[1]))
map_vertices[y][x*2+2][0] = (trunc(p1[0]), trunc(p1[1]))
map_vertices[y][x*2+3][0] = (trunc(p6[0]), trunc(p6[1]))
map_vertices[y+1][x*2+1][0] = (trunc(p3[0]), trunc(p3[1]))
map_vertices[y+1][x*2+2][0] = (trunc(p4[0]), trunc(p4[1]))
map_vertices[y+1][x*2+3][0] = (trunc(p5[0]), trunc(p5[1]))
else:
map_vertices[y][x*2][0] = (trunc(p2[0]), trunc(p2[1]))
map_vertices[y][x*2+1][0] = (trunc(p1[0]), trunc(p1[1]))
map_vertices[y+1][x*2][0] = (trunc(p3[0]), trunc(p3[1]))
map_vertices[y+1][x*2+1][0] = (trunc(p4[0]), trunc(p4[1]))
map_vertices[y+1][x*2+2][0] = (trunc(p5[0]), trunc(p5[1]))
map_vertices[y][x*2+2][0] = (trunc(p6[0]), trunc(p6[1]))


# draw river
#print("rivers is: " + str(len(rivers)))
if RELATIVE_SIZE < 30:
thickness = 3
else:
thickness = 5

for k in xrange(len(rivers)):
river = rivers[k]
for r in xrange(len(river)-1):
r1 = river[r]
r2 = river[r+1]
r1_x = r1[0]
r1_y = r1[1]
r2_x = r2[0]
r2_y = r2[1]
p1 = map_vertices[r1_y][r1_x][0]
p2 = map_vertices[r2_y][r2_x][0]
#print("drawing river: " + str(p1) + " to " + str(p2))

pygame.draw.line(map_surface, color_dodger_blue, p1, p2, thickness)
## for r in xrange(len(rivers)):
## river = list(rivers[r])
## print("river == " + str(river))
## for ri in xrange(len(river) - 1):
##
## r1 = river[r]
## print("r1: " + str(r1))
## r2 = river[r+1]
## print("r2: " + str(r2))
##
## r1_x = r1[0]
## r1_y = r1[1]
## r2_x = r2[0]
## r2_y = r2[1]
##
## p1 = map_vertices[r1_y][r1_x][0]
## p2 = map_vertices[r2_y][r2_x][0]
##
## print("drawing river: " + str(p1) + " to " + str(p2))
##
## pygame.draw.line(map_surface, color_blue, p1, p2, thickness)
# t2 = pygame.time.get_ticks()
# print("draw_map: " + str(t2-t1))
# calc stats
#print("Land Coverage is {:.0%}".format(float(num_land_hexes)/float(total_nu
m_hexes)))
return map_surface
# -- end draw_map_image --
def draw_overlay_image(game_map):
# t1 = pygame.time.get_ticks()
global map_vertices
global map_edges
width = ceil(len(game_map[0]) * HEX_WIDTH + HEX_R) + 1
height = ceil(len(game_map) * HEX_HEIGHT + HEX_H) + 1
overlay_surface = pygame.Surface((width, height), flags=SRCALPHA, depth=32)
if show_hex_lines or show_hex_coords or show_elevation:
for y in xrange(len(game_map)):
yf = y*RELATIVE_SIZE
if (y % 2 > 0):
is_row_odd = True
else:
is_row_odd = False

for x in xrange(len(game_map[0])):
xa = x*HEX_A
if is_row_odd:
xa += HEX_R
p1 = (xa+HEX_R, yf)
p2 = (xa, yf+HEX_H)
p3 = (xa, yf+1*RELATIVE_SIZE)
p4 = (xa+HEX_R, yf+HEX_B)
p5 = (xa+HEX_A, yf+1*RELATIVE_SIZE)
p6 = (xa+HEX_A, yf+HEX_H)
if show_hex_lines:
pygame.draw.polygon(overlay_surface, color_gray_77, (p1, p2,
p3, p4, p5, p6), 1)
if show_hex_coords:
hex_coords_text = str(x) + "," + str(y)
hex_center = (xa+HEX_R, yf+HEX_S)
size = small_font.size(hex_coords_text)
overlay_surface.blit(small_font.render(hex_coords_text, True
, color_gray_196), (hex_center[0] - size[0]/2, hex_center[1] - size[1]/2 + 4))
if show_elevation:
# show height text
if game_map[y][x][1] > 0:
height_text = str(game_map[y][x][1])
hex_center = (xa+HEX_R, yf+HEX_S)
size = small_font.size(height_text)
overlay_surface.blit(small_font.render(height_text, True
, color_orange), (hex_center[0] - size[0]/2, hex_center[1] - size[1]/2 - 10))
if selected_hex[0] > -1 and selected_hex[1] > -1:
x = selected_hex[0]
y = selected_hex[1]

if game_map[y][x][1] > 0:
draw_sub_hexes(x, y, overlay_surface, color_gray_77, 1)

yf = y*RELATIVE_SIZE
xa = x*HEX_A
if y % 2 > 0:
xa += HEX_R
p1 = (xa+HEX_R, yf)
p2 = (xa, yf+HEX_H)
p3 = (xa, yf+1*RELATIVE_SIZE)
p4 = (xa+HEX_R, yf+HEX_B)
p5 = (xa+HEX_A, yf+1*RELATIVE_SIZE)
p6 = (xa+HEX_A, yf+HEX_H)
if RELATIVE_SIZE < 30:
thickness = 2
else:
thickness = 4
pygame.draw.polygon(overlay_surface, color_orange, (p1, p2, p3, p4, p5,
p6), thickness)

if show_help_text:
for z in xrange(len(help_text)):
s = help_text[z]
overlay_surface.blit(large_font.render(s, True, color_orange), (10,
z*large_font_height))
# t2 = pygame.time.get_ticks()
# print("draw_overlay: " + str(t2-t1))

return overlay_surface
# -- draw_overlay_image
def get_mountain_hexes(game_map):
mountain_hexes = [ ]
for y in xrange(len(game_map)):
for x in xrange(len(game_map[0])):
if get_elevation_type(game_map[y][x][1]) == "MOUNTAINS":
mountain_hexes.append((x, y))
return mountain_hexes
# -- get_mountain_hexes --
def get_lowest_neighbor_hex(cur_hex, prev_hex):
cur_hex_neighbors = get_neighbor_hexes(cur_hex[0], cur_hex[1])
#print("chn: " + str(cur_hex_neighbors))
prev_hex_neighbors = get_neighbor_hexes(prev_hex[0], prev_hex[1])
#print("phn: " + str(prev_hex_neighbors))
available_hexes = set(cur_hex_neighbors).difference(set(prev_hex_neighbors))
if prev_hex in available_hexes:
available_hexes.remove(prev_hex)
#print("ahn: " + str(available_hexes))
## lowest_elevation = 100
## lowest_neighbor = (-1, -1)
## for h in xrange(len(available_hexes)):
## check_hex = available_hexes.pop()
## if game_map[check_hex[1]][check_hex[0]][1] < lowest_elevation:
## lowest_neighbor = check_hex
lowest_elevation = 100
lowest_neighbor = (-1, -1)
lowest_set = [ ]
for h in xrange(len(available_hexes)):
check_hex = available_hexes.pop()
if game_map[check_hex[1]][check_hex[0]][1] <= lowest_elevation:
lowest_set.append(check_hex)
if len(lowest_set) == 1:
lowest_neighbor = lowest_set[0]
else:
num = randint(0, len(lowest_set) - 1)
lowest_neighbor = lowest_set[num]
return lowest_neighbor
# -- get_lowest_neighbor_hex --
def generate_rivers(game_map, map_vertices):
mountain_hexes = get_mountain_hexes(game_map)
potential_river_sources = set()
for h in xrange(len(mountain_hexes)):
neighbor_set = set(get_neighbor_hexes(mountain_hexes[h][0], mountain_hex
es[h][1]))
found = False
while not found and len(neighbor_set) > 0:
n = neighbor_set.pop()
if n in potential_river_sources:
found = True
if not found:
potential_river_sources.add(mountain_hexes[h])

river_sources = set()
for p in xrange(len(potential_river_sources)):
roll = randint(1, 100)
prs = potential_river_sources.pop()
if roll < 90:
river_sources.add(prs)

all_river_hexes = set()
for r in xrange(len(river_sources)):
river = [ ]
prev_hex = (-1, -1)
cur_hex = river_sources.pop()

done = False
while not done:
next_hex = get_lowest_neighbor_hex(cur_hex, prev_hex)
#print("prev: " + str(prev_hex) + " cur: " + str(cur_hex) + " n
ext: " + str(next_hex))
if next_hex == (-1, -1):
done = True
#print("next hex: no lowest neighbor")
else:
#if game_map[next_hex[1]][next_hex[0]][2]: # isOcean == True
if game_map[next_hex[1]][next_hex[0]][0] == WATER :
river.append(cur_hex)
#all_river_hexes.add(cur_hex)
done = True
#print("next hex: next hex is ocean")
elif next_hex in all_river_hexes:
done = True
#print("next hex: already in river hex set")
else:
river.append(cur_hex)
#all_river_hexes.add(cur_hex)
prev_hex = cur_hex
cur_hex = next_hex

if (len(river) > 4):
for j in xrange(len(river)):
all_river_hexes.add(river[j])

#print("num rivers: " + str(len(all_river_hexes)))
return all_river_hexes
# -- generate_rivers --

def get_lowest_neighbor_vert(cur_vert, prev_vert):
print("glnv: " + str(cur_vert) + " -- " + str(prev_vert))
cur_vert_neighbors = get_neighbor_vertices(cur_vert[0], cur_vert[1])
#print("chn: " + str(cur_hex_neighbors))
prev_vert_neighbors = get_neighbor_vertices(prev_vert[0], prev_vert[1])
#print("phn: " + str(prev_hex_neighbors))
available_verts = set(cur_vert_neighbors).difference(set(prev_vert_neighbors
))
if prev_vert in available_verts:
available_verts.remove(prev_vert)
#print("ahn: " + str(available_hexes))
lowest_elevation = 1000
lowest_neighbor = (-1, -1)
lowest_set = [ ]
for h in xrange(len(available_verts)):
check_vert = available_verts.pop()
print("check_vert: " + str(check_vert))
if map_vertices[check_vert[1]][check_vert[0]][1] <= lowest_elevation:
lowest_set.append(check_vert[0])
if len(lowest_set) == 1:
lowest_neighbor = lowest_set[0][0]
else:
num = randint(0, len(lowest_set) - 1)
lowest_neighbor = lowest_set[num]
return lowest_neighbor
# -- get_lowest_neighbor_vert --
def generate_rivers_2(game_map, map_vertices):
mountain_hexes = get_mountain_hexes(game_map)
potential_river_sources = set()
for h in xrange(len(mountain_hexes)):
neighbor_set = set(get_neighbor_hexes(mountain_hexes[h][0], mountain_hex
es[h][1]))
found = False
while not found and len(neighbor_set) > 0:
n = neighbor_set.pop()
if n in potential_river_sources:
found = True
if not found:
potential_river_sources.add(mountain_hexes[h])

river_sources = set()
for p in xrange(len(potential_river_sources)):
roll = randint(1, 100)
prs = potential_river_sources.pop()
if roll < 90:
river_sources.add(prs)

all_river_verts = set()
for r in xrange(len(river_sources)):
river = [ ]
prev_vert = (-1, -1)
cur_hex = river_sources.pop()
cur_hex_verts = get_border_vertices(cur_hex[0], cur_hex[1])
r = randint(0, 5)
cur_vert = cur_hex_verts[r][0]

done = False
while not done:
next_vert = get_lowest_neighbor_vert(cur_vert, prev_vert)
#print("prev: " + str(prev_hex) + " cur: " + str(cur_hex) + " n
ext: " + str(next_hex))
if next_vert == (-1, -1):
done = True
#print("next hex: no lowest neighbor")
else:
if map_vertces[next_vert[1]][next_vert[0]][1] < 4 :
river.append(cur_vert)
done = True
#print("next hex: next hex is ocean")
elif next_vert in all_river_verts:
done = True
#print("next hex: already in river hex set")
else:
river.append(cur_vert)
prev_vert = cur_vert
cur_vert = next_vert

if (len(river) > 4):
for j in xrange(len(river)):
all_river_verts.add(river[j])

#print("num rivers: " + str(len(all_river_hexes)))
return all_river_verts
# -- generate_rivers_2 --
def generate_rivers_original(game_map, map_vertices):
rivers = [ ]
new_river = [ ]
#num = randint(2, 5)
peak_height = 0
peak = (-1, -1)
for y in xrange(len(map_vertices)):
for x in xrange(len(map_vertices[y])):
if map_vertices[y][x][1] > peak_height:
peak_height = map_vertices[y][x][1]
peak = (x, y)
#print("peak: " + str(peak_height) + " @ " + str(x) + "," + str(
y))
new_river.append(peak)
new_river_set = set(new_river)
#print("river starting at: " + str(peak))
#cur_vertex = peak
prev_vert = peak
neighbors = get_neighbor_vertices(peak[0], peak[1])
roll = randint(0, 2)
cur_vert = neighbors[roll]
new_river.append(cur_vert)
new_river_set = set(new_river)
done = False
while not done:
#print("START_LOOP:")
neighbors = get_neighbor_vertices(cur_vert[0], cur_vert[1])
#print(" neighbors: " + str(neighbors))
neighbor_set = set(neighbors)
#print(" neighbor_set: " + str(neighbor_set))
#print(" attempting to remove " + str(prev_vert) + " from set")
neighbor_set.remove(prev_vert)
#print(" neighbors_set: " + str(neighbor_set))

vert_1 = neighbor_set.pop()
vert_2 = neighbor_set.pop()
if vert_1[1] < 0 or vert_1[0] < 0 or vert_2[1] < 0 or vert_2[0] < 0:
done = True
print("gen complete: neighbor verts invalid")
else:

elev_1 = map_vertices[vert_1[1]][vert_1[0]][1]
elev_2 = map_vertices[vert_2[1]][vert_2[0]][1]
if elev_1 < elev_2:
next_vert = vert_1
other_vert = vert_2
elif elev_1 > elev_2:
next_vert = vert_2
other_vert = vert_1
else: # elev_1 == elev_2
prob = randint(1,100)
if prob > 50:
next_vert = vert_1
other_vert = vert_2
else:
next_vert = vert_2
other_vert = vert_1
cur_elev = map_vertices[cur_vert[1]][cur_vert[0]][1]
next_elev = map_vertices[next_vert[1]][next_vert[0]][1]
#print(" cur_elev: " + str(cur_elev) + "; next_elev: " + str(next_e
lev))
if (next_elev > cur_elev + 16) or (next_elev < 2):
done = True
print("gen complete: next vert elev too high or too low")
elif next_vert in new_river_set:
next_vert = other_vert
if other_vert in new_river_set:
done = True
print("gen complete: both possible next verts are already in
river")
else:
#print(" next_vert: " + str(next_vert))
new_river.append(next_vert)
new_river_set = set(new_river)
prev_vert = cur_vert
#print(" prev_vert: " + str(prev_vert))
cur_vert = next_vert
## prob = randint(1, 100)
## if prob > 90:
## #print("foo")
## done = True
## print("gen complete: other vert chosen, but rolled ove
r 90")
else:
#print(" next_vert: " + str(next_vert))
new_river.append(next_vert)
new_river_set = set(new_river)
prev_vert = cur_vert
#print(" prev_vert: " + str(prev_vert))
cur_vert = next_vert

rivers.append(new_river)
return rivers
# -- generate_rivers_original --
def generate_coastal_hexes(game_map):
coastal_hexes = [ ]
for y in xrange(len(game_map)):
for x in xrange(len(game_map[0])):
if game_map[y][x][3]: # isCoast == True
coastal_hexes.append((x, y))
#print("num coastal hexes: " + str(len(coastal_hexes)))
return coastal_hexes
# -- generate_coastal_rivers --
def generate_coastal_rivers(game_map, coastal_hexes):
river_sources = [ ]
r = randint(0, len(coastal_hexes)-1)
river_sources.append(coastal_hexes[r])
river_source_verts = [ ]
for rs in xrange(len(river_sources)):
rs_hex = river_sources[rs]
print("rs_hex: " + str(rs_hex))
rs_verts = get_border_vertices(rs_hex[0], rs_hex[1])
print("rs_verts: " + str(rs_verts))
potential_source_verts = [ ]
for rsv in xrange(len(rs_verts)):
neighbors = get_border_hexes(rs_verts[rsv][0], rs_verts[rsv][1])
num_land = 0
for n in xrange(len(neighbors)):
if game_map[neighbors[n][1]][neighbors[n][0]][0] == GRASS:
num_land += 1
if num_land > 1:
potential_source_verts.append(rs_verts[rsv])
rv = randint(0, len(potential_source_verts)-1)
river_source_verts.append(potential_source_verts[rv])
rivers = [ ]
print("RSV: " + str(river_source_verts))
for rsvert in xrange(len(river_source_verts)):
river = set()
river.add(river_source_verts[rsvert])
cur_vert = river_source_verts[rsvert]
print("cur_vert: " + str(cur_vert))
done = False
while not done:
neighbor_verts = get_neighbor_vertices(cur_vert[0], cur_vert[1])
print("neighbor_verts: " + str(neighbor_verts))
highest_vert = [ ]
highest_elevation = 0
for nv in xrange(len(neighbor_verts)):
if map_vertices[neighbor_verts[nv][1]][neighbor_verts[nv][0]][1]
>= highest_elevation:
highest_elevation = map_vertices[neighbor_verts[nv][1]][neig
hbor_verts[nv][0]][1]
highest_vert.append(neighbor_verts[nv])
print("highest_vert: "+ str(neighbor_verts[nv]))
print("highest_vert: " + str(highest_vert))

if len(highest_vert) > 1:
rand = randint(0, len(highest_vert)-1)
if highest_vert[rand] in river:
done = True
river.add(highest_vert[rand])
cur_vert = highest_vert[rand]
elif len(highest_vert) == 1:
if highest_vert[0] in river:
done = True
else:
done = True
print("rivers adding river: " + str(river))
rivers.append(river)
return rivers
# -- generate_coastal_rivers --
def calculate_hex_sizes(new_size):
global RELATIVE_SIZE
global HEX_S
global HEX_H
global HEX_R
global HEX_B
# global HEX_B2
global HEX_A
global HEX_WIDTH
global HEX_HEIGHT
# global HEX_SLOPE
# global new_dim

RELATIVE_SIZE = new_size
HEX_S = 2.0 / 3.0 # length of a side of a hex; s==0.6666667
HEX_S *= RELATIVE_SIZE
HEX_H = sin(radians(30.0)) * HEX_S # h==0.33333334
HEX_R = cos(radians(30.0)) * HEX_S # r==0.57735026
HEX_B = HEX_S + 2 * HEX_H # height of the entire hex; b==1.3333334
# HEX_B2 = HEX_B / 2.0 # half the height of the entire hex
HEX_A = 2 * HEX_R # width of the entire hex; a==1.1547005
HEX_WIDTH = HEX_A
HEX_HEIGHT = HEX_S + HEX_H # This is equal to RELATIVE_SIZE
# HEX_SLOPE = HEX_H / HEX_R
# new_dim = 3*RELATIVE_SIZE/4
# -- end calculate_hex_sizes --
RELATIVE_SIZE = 10
MAP_WIDTH = 105
MAP_HEIGHT = 73
POST_PROCESS_PASSES = 4 # was 4
button_zoom_in_filename = 'hex_zoom_in_v2.png'
button_zoom_out_filename = 'hex_zoom_out_v2.png'
#button_randomize_map_filename = 'generateWorld.png'
button_randomize_map_filename = 'globe.png'
#sprite_1_image_filename = 'resources3_16.png'
hill_icon_filename = 'hill_icon.png'
scroll_x = 0
scroll_y = 0
SCROLL_AMOUNT = 5
HEX_S = 2.0 / 3.0 # length of a side of a hex; s==0.6666667
HEX_S *= RELATIVE_SIZE
HEX_H = sin(radians(30.0)) * HEX_S # h==0.33333334
HEX_R = cos(radians(30.0)) * HEX_S # r==0.57735026
HEX_B = HEX_S + 2 * HEX_H # height of the entire hex; b==1.3333334
HEX_B2 = HEX_B / 2.0 # half the height of the entire hex
HEX_A = 2 * HEX_R # width of the entire hex; a==1.1547005
HEX_WIDTH = HEX_A
HEX_HEIGHT = HEX_S + HEX_H # This is equal to RELATIVE_SIZE
HEX_SLOPE = HEX_H / HEX_R
HEX_EAST = 0
HEX_SOUTHEAST = 1
HEX_SOUTHWEST = 2
HEX_WEST = 3
HEX_NORTHWEST = 4
HEX_NORTHEAST = 5
button_area = (52, 52)
icon_size = (48, 48)
control_panel_offset = (8, 8)
button_offset = 10
#os.environ['SDL_VIDEO_CENTERED'] = '1'
os.environ['SDL_VIDEO_WINDOW_POS'] = "6,30"
pygame.init()
#pygame.display.list_modes()
pygame.display.set_caption("PyGame HexTest v0.0.23")
display_info = pygame.display.Info()
screen_width = display_info.current_w
screen_height = display_info.current_h
print(str(screen_width) + " x " + str(screen_height))
screen = pygame.display.set_mode((screen_width, screen_height), FULLSCREEN, 32)
control_panel_width = 68
info_panel_height = 100
control_panel_size = (control_panel_width, screen_height - info_panel_height)
control_panel_loc = (screen_width - control_panel_width, 0)
info_panel_size = (screen_width, info_panel_height)
info_panel_loc = (0, screen_height - info_panel_height)
map_panel_loc = (0, 0)
map_panel_size = (screen_width - control_panel_width, screen_height - info_panel
_height)
map_panel = screen.subsurface(map_panel_loc, map_panel_size)
info_panel = screen.subsurface(info_panel_loc, info_panel_size)
control_panel = screen.subsurface(control_panel_loc, control_panel_size)
button_zoom_in = pygame.image.load(button_zoom_in_filename)
button_zoom_out = pygame.image.load(button_zoom_out_filename)
button_randomize_map = pygame.image.load(button_randomize_map_filename)
button_randomize_map = pygame.transform.smoothscale(button_randomize_map, icon_s
ize)
#resource_sprite_1 = pygame.image.load(sprite_1_image_filename).convert_alpha()
#scaled_resource_sprite_1 = pygame.transform.smoothscale(resource_sprite_1,
new_dim = 3*RELATIVE_SIZE/4
color_blue = (0,0,255)
color_green = (0,127,0)
color_green2 = (47,155,0)
color_sawgrass = (70,127,0)
color_white = (245,245,245)
color_black = (0,0,0)
color_red = (255, 0, 0)
color_cyan = (0, 255, 255)
color_steel_blue = (70, 130, 180)
color_orange = (255, 127, 80)
color_dodger_blue = (16, 78, 139)
color_dim_gray = (105, 105, 105)
color_gray_77 = (77, 77, 77)
color_gray_196 = (196, 196, 196)
color_yellow_4 = (139, 139, 0)
color_dark_goldenrod = (139, 101, 8)
color_navajo_white4 = (139, 121, 94)
color_mesa2 = (100, 127, 42)
color_burly_wood4 = (139, 115, 85)
color_chocolate_brown = (139, 69, 19)
color_sienna = (139, 71, 38)
color_bisque3 = (205, 183, 158)
color_bisque4 = (139, 125, 107)
color_cornsilk4 = (139, 136, 120)
color_dark_orchid = (104, 34, 139)
color_indigo = (75, 0, 130)
font = pygame.font.SysFont("accidental presidency", 18)
font_height = font.get_linesize()
small_font = pygame.font.SysFont("accidental presidency", 12)
small_font_height = small_font.get_linesize()
large_font = pygame.font.SysFont("accidental presidency", 24)
large_font_height = large_font.get_linesize()
event_text = "MOUSE COORDS:"
hex_coord_text = "HEX COORDS "
selected_hex_text = [ ]
help_text = [ "F1: Help", "F2: Show Hex Grid", "F3: Show Hex Coords", "F4: Show
Elevations", "Esc: Exit Program", "Scroll Wheel: Zoom", "WASD: Scroll Map"]
show_help_text = False
show_hex_info = True
#show_debug_text = False
show_hex_coords = False
show_hex_lines = False
show_elevation = False
#terrain_types
GRASS = 0
WATER = 1
selected_hex = (-1, -1)
#t1 = pygame.time.get_ticks()
#print("starting map gen at " + str(t1))
game_map = generate_map()
determine_water_types(game_map)
determine_coastal_hexes(game_map)
map_vertices = initialize_vertices(game_map)
calculate_vertex_weights(game_map)
#rivers = generate_rivers_2(game_map, map_vertices)
#map_edges = initialize_edges(game_map)
coastal_hexes = generate_coastal_hexes(game_map)
rivers = generate_rivers_original(game_map, map_vertices)
#rivers = generate_coastal_rivers(game_map, coastal_hexes)
map_image = draw_map_image(game_map)
overlay_image = draw_overlay_image(game_map)
#t2 = pygame.time.get_ticks()
#print("ending map gen at " + str(t2))
#print("total time: " + str(t2-t1))
running = True
while running:
map_panel.fill(color_dodger_blue)
control_panel.fill(color_dim_gray)
info_panel.fill(color_dim_gray)

for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
if event.key == K_F1:
show_help_text = not show_help_text
overlay_image = draw_overlay_image(game_map)
if event.key == K_F2:
show_hex_lines = not show_hex_lines
overlay_image = draw_overlay_image(game_map)
if event.key == K_F3:
show_hex_coords = not show_hex_coords
overlay_image = draw_overlay_image(game_map)
if event.key == K_F4:
show_elevation = not show_elevation
overlay_image = draw_overlay_image(game_map)
# if event.type == MOUSEMOTION:
# event_text = "MOUSE COORDS:" + str(event.pos)
if event.type == MOUSEBUTTONDOWN:
#print event.button
x, y = pygame.mouse.get_pos()
if (y >= info_panel_loc[1]): # mouse clicked in info panel at botto
m of screen
print "info panel:", x, y
elif (x >= control_panel_loc[0]): # mouse clicked in control panel o
n right of screen
x -= control_panel_loc[0]
# check if mouse clicked in a button
if (x > button_offset) & (x < button_offset + button_area[0]):
# figure out which button clicked
if (y > button_offset) & (y < button_offset + button_area[1]
):
# first button clicked
# zoom button
# temporarily reusing as post-process button
for i in xrange(POST_PROCESS_PASSES):
game_map = post_process_map_coarse(game_map)

#if (RELATIVE_SIZE < 100):
# calculate_hex_sizes(RELATIVE_SIZE+5)

map_image = draw_map_image(game_map)

elif (y > control_panel_offset[1] + button_area[1] + button_
offset) & (y < control_panel_offset[1] + button_area[1]*2 + button_offset):
# second button clicked
# zoom out button
# temporarily reusing as post_process_lake button
for i in xrange(POST_PROCESS_PASSES):
game_map = post_process_map_fine(game_map)
#if (RELATIVE_SIZE > 5):
# calculate_hex_sizes(RELATIVE_SIZE-5)
map_image = draw_map_image(game_map)

elif (y > (control_panel_offset[1] + button_area[1])*2 + but
ton_offset) & (y < control_panel_offset[1] + button_area[1]*3 + button_offset*2)
:
# randomize map button pressed
game_map = generate_map()
determine_water_types(game_map)
determine_coastal_hexes(game_map)
map_vertices = initialize_vertices(game_map)
calculate_vertex_weights(game_map)
coastal_hexes = generate_coastal_hexes(game_map)
#rivers = generate_coastal_rivers(game_map, coastal_hexe
s)
rivers = generate_rivers_original(game_map, map_vertices
)
#rivers = generate_rivers_2(game_map, map_vertices)
map_image = draw_map_image(game_map)
overlay_image = draw_overlay_image(game_map)

else: # mouse clicked in main map panel
if event.button == 4: # scroll wheel up
if (RELATIVE_SIZE < 80):
calculate_hex_sizes(RELATIVE_SIZE+5)
map_image = draw_map_image(game_map)
overlay_image = draw_overlay_image(game_map)
elif event.button == 5: # scroll wheel down
if (RELATIVE_SIZE > 10):
calculate_hex_sizes(RELATIVE_SIZE-5)
map_image = draw_map_image(game_map)
overlay_image = draw_overlay_image(game_map)

elif event.button == 1: # left click
# this algorithm for converting pixel coordinates to hex coo
rdinates is based on the info at:
# http://www.gamedev.net/page/resources/_/technical/game-pro
gramming/coordinates-in-hexagon-based-tile-maps-r1800
guess_x = trunc((x + scroll_x) / HEX_WIDTH) # same as secti
on_x
guess_y = trunc((y + scroll_y) / HEX_HEIGHT) # same as secti
on_y
section_pixel_x = trunc((x + scroll_x) % HEX_WIDTH)
section_pixel_y = trunc((y + scroll_y) % HEX_HEIGHT)
real_hex_x = guess_x
real_hex_y = guess_y

if (guess_y % 2 > 0):
#section_type = "b"
if (section_pixel_x >= HEX_R): # right half of a B sect
ion
if (section_pixel_y < (2* HEX_H - section_pixel_x *
HEX_SLOPE)): # upper triangle of right half B section
real_hex_y -= 1
elif (section_pixel_x < HEX_R): # left half of a B secti
on
if (section_pixel_y < (section_pixel_x * HEX_SLOPE))
: # upper triangle portion of left half B section
real_hex_y -= 1
else: # bottom part of left half of B section
real_hex_x -= 1
else:
#section_type = "a"
if (section_pixel_y < HEX_H - (section_pixel_x * HEX_SLO
PE)): # upper left triangle of A section
real_hex_x -= 1
real_hex_y -= 1
elif (section_pixel_y < (-1 * (HEX_H - (section_pixel_x
* HEX_SLOPE)))): # upper right triangle of A section
real_hex_y -= 1

hex_coord_text = "HEX COORDS (" + str(real_hex_x) + ", " + s
tr(real_hex_y) + ")"
if selected_hex[0] == real_hex_x and selected_hex[1] == real
_hex_y:
selected_hex = (-1, -1)
selected_hex_text = [ ]
overlay_image = draw_overlay_image(game_map)
else:
if real_hex_y < len(game_map) and real_hex_x < len(game_
map[0]):
selected_hex = (real_hex_x, real_hex_y)
selected_hex_text = [ ]
selected_hex_text.append(hex_coord_text)
e = game_map[real_hex_y][real_hex_x][1]
if e == 0: # water
if game_map[real_hex_y][real_hex_x][2]: # isOcea
n
selected_hex_text.append("OCEAN (ELEV " + st
r(e) + ")")
else:
selected_hex_text.append("SEA (ELEV " + str(
e) + ")")
else:
if game_map[real_hex_y][real_hex_x][3]: # isCoas
t
selected_hex_text.append("COASTAL " + get_el
evation_type(e) + " (ELEV " + str(e) + ")")
else:
selected_hex_text.append(get_elevation_type(
e) + " (ELEV " + str(e) + ")")
overlay_image = draw_overlay_image(game_map)

key_pressed = pygame.key.get_pressed()
if key_pressed[K_w]:
scroll_y -= SCROLL_AMOUNT
if scroll_y < 0:
scroll_y = 0
if key_pressed[K_a]:
scroll_x -= SCROLL_AMOUNT
if scroll_x < 0:
scroll_x = 0
if key_pressed[K_s]:
scroll_y += SCROLL_AMOUNT
if scroll_y + map_panel_size[1] > (map_image.get_size())[1]:
scroll_y = (map_image.get_size())[1] - map_panel_size[1]
if key_pressed[K_d]:
scroll_x += SCROLL_AMOUNT
if scroll_x + map_panel_size[0] > (map_image.get_size())[0]:
scroll_x = (map_image.get_size())[0] - map_panel_size[0]
# t1 = pygame.time.get_ticks()
map_panel.blit(map_image, (0, 0), (scroll_x, scroll_y, map_panel_size[0], ma
p_panel_size[1]))
map_panel.blit(overlay_image, (0, 0), (scroll_x, scroll_y, map_panel_size[0]
, map_panel_size[1]))
# t2 = pygame.time.get_ticks()
# print("BLIT total time: " + str(t2-t1))
if show_hex_info:
info_panel.fill(color_dim_gray)
pygame.draw.line(screen, color_gray_77, (0, map_panel_size[1]), (map_pan
el_size[0], map_panel_size[1]), 3)
for z in xrange(len(selected_hex_text)):
s = selected_hex_text[z]
info_panel.blit(large_font.render(s, True, color_orange), (10, z*lar
ge_font_height))
# draw the GUI elements
pygame.draw.line(screen, color_gray_77, (map_panel_size[0], 0), (map_panel_s
ize[0], map_panel_size[1]), 3)
pygame.draw.line(screen, color_gray_77, (0, map_panel_size[1]), (map_panel_s
ize[0], map_panel_size[1]), 3)

pygame.draw.rect(control_panel, color_gray_196, Rect(control_panel_offset, b
utton_area), 1)
pygame.draw.rect(control_panel, color_gray_196, Rect((control_panel_offset[0
], control_panel_offset[1] + button_area[1] + button_offset), button_area), 1)
pygame.draw.rect(control_panel, color_gray_196, Rect((control_panel_offset[0
], control_panel_offset[1] + (button_area[1] + button_offset)*2), button_area),
1)
control_panel.blit(button_zoom_in, (button_offset, button_offset))
control_panel.blit(button_zoom_out, (button_offset, button_offset*2 + button
_area[1]))
control_panel.blit(button_randomize_map, (button_offset, button_offset*3 + b
utton_area[1]*2))
pygame.display.update()
pygame.quit()

You might also like