Anda di halaman 1dari 6

# HEX METRICS:

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

# / hex \ | / \

# / dir \ _ | / \
# | 4 5 | | | / \
# | \ / | |s | b \ /
# | 3-----0 | | | \ /
# | / \ | _ | \ /
# \ 2 1 / | | +-------+
# \ / |h |
# \ / _ |
# -
# |------|
# r
# |-----------|
# a
button_zoom_in_filename = 'hex_zoom_in.png'
button_zoom_out_filename = 'hex_zoom_out.png'
button_area = (52, 52)
icon_size = (48, 48)
control_panel_offset = (8, 8)
button_offset = 10
map_panel_size = (1212, 835)
map_panel_loc = (0, 0)
info_panel_size = (1280, 125)
info_panel_loc = (0,835)
control_panel_size = (68, 835)
control_panel_loc = (1212, 0)
scroll_x = 0
scroll_y = 0
SCROLL_SIZE = 60
import pygame
from pygame.locals import *
from sys import exit
from math import *
RELATIVE_SIZE = 100
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
#print "Relative Size:", RELATIVE_SIZE
#print "Hex Side:", HEX_S
#print "Hex Full Height:", HEX_B
#print "Hex Working Height:", HEX_HEIGHT
#print "Hex Width:", HEX_WIDTH
TILE_THICKNESS = 0.25
TILE_THICKNESS_HALF = 0.125
HEX_EAST = 0
HEX_SOUTHEAST = 1
HEX_SOUTHWEST = 2
HEX_WEST = 3
HEX_NORTHWEST = 4
HEX_NORTHEAST = 5
pygame.init()
screen = pygame.display.set_mode((1280, 960), RESIZABLE, 32)
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)
map_surface = pygame.Surface((2000, 2000))
button_zoom_in = pygame.image.load(button_zoom_in_filename)
button_zoom_out = pygame.image.load(button_zoom_out_filename)
new_dim = 3*RELATIVE_SIZE/4
color_blue = (0,0,255)
color_green = (0,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_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", 24)
#font.set_bold(True)
font_height = font.get_linesize()
event_text = "MOUSE COORDS:"
#hex_info_text = "HEX GUESS:"
hex_coord_text = "HEX COORDS:"
show_debug_text = False
show_hex_coords = False
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
global scaled_sprite1
global scaled_sprite2
global scaled_sprite3
global scaled_sprite4
global scaled_sprite5

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
#print "new hex size is", str(RELATIVE_SIZE)
new_dim = 3*RELATIVE_SIZE/4
running = True
while running:
map_panel.fill(color_white)
control_panel.fill(color_dim_gray)
info_panel.fill(color_dim_gray)
map_surface.fill(color_blue)
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_debug_text = not show_debug_text
if event.key == K_F2:
show_hex_coords = not show_hex_coords
if event.key == K_w:
# move map north
scroll_y -= SCROLL_SIZE
if scroll_y < 0:
scroll_y = 0
if event.key == K_a:
# move map west
scroll_x -= SCROLL_SIZE
if scroll_x < 0:
scroll_x = 0
if event.key == K_s:
# move map south
scroll_y += SCROLL_SIZE
if event.key == K_d:
# move map east
scroll_x += SCROLL_SIZE

if event.type == MOUSEMOTION:
event_text = "MOUSE COORDS:" + str(event.pos)
if event.type == MOUSEBUTTONDOWN:
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
#print "control panel:", x, y
x -= control_panel_loc[0]
#print " > info panel:", x, y
# check if mouse clicked in a button
if (x > button_offset) & (x < button_offset + button_area[0]):
#print "button clicked"
# now figure out which button
if (y > button_offset) & (y < button_offset + button_area[1]
):
# first button clicked
#print "ZOOM IN"
if (RELATIVE_SIZE < 1000):
calculate_hex_sizes(RELATIVE_SIZE+25)
elif (y > control_panel_offset[1] + button_area[1] + button_
offset) & (y < control_panel_offset[1] + button_area[1]*2 + button_offset):
#print "ZOOM OUT"
if (RELATIVE_SIZE > 25):
calculate_hex_sizes(RELATIVE_SIZE-25)
else: # mouse clicked in main map panel
#hex_info_text = "HEX COORDS: X: " + str(x) + " Y: " + str(y)
guess_x = trunc(x / HEX_WIDTH) # same as section_x
guess_y = trunc(y / HEX_HEIGHT) # same as section_y
#print "HEX GUESS: ", guess_x, guess_y
#hex_info_text = "HEX GUESS: (" + str(guess_x) + ", " + str(gues
s_y) +")"
section_pixel_x = trunc(x % HEX_WIDTH)
section_pixel_y = trunc(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 section
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 section
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_SLOPE))
: # upper left triangle of A section
real_hex_x -= 1
real_hex_y -= 1
elif (section_pixel_y < (-1 * (HEX_H - (section_pixel_x * HE
X_SLOPE)))): # upper right triangle of A section
real_hex_y -= 1
hex_coord_text = "HEX COORDS: (" + str(real_hex_x) + ", " + str(
real_hex_y) + ")"
for y in xrange(100):
yf = y*RELATIVE_SIZE
if (y % 2 > 0):
is_row_odd = True
else:
is_row_odd = False

for x in xrange(100):
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)
screen.lock()
#pygame.draw.polygon(map_panel, color_green, (p1, p2, p3, p4, p5, p6
))
#pygame.draw.polygon(map_panel, color_gray_196, (p1, p2, p3, p4, p5,
p6), 3)
pygame.draw.polygon(map_surface, color_steel_blue, (p1, p2, p3, p4,
p5, p6))
pygame.draw.polygon(map_surface, color_gray_196, (p1, p2, p3, p4, p5
, p6), 2)
screen.unlock()
hex_coords_text = "(" + str(x) + "," + str(y) + ")"
hex_center = (xa+HEX_R, yf+HEX_S)
size = font.size(hex_coords_text)
if show_hex_coords:
#map_panel.blit(font.render(hex_coords_text, True, color_white),
(hex_center[0] - size[0]/2, hex_center[1] - size[1]/2))
map_surface.blit(font.render(hex_coords_text, True, color_white)
, (hex_center[0] - size[0]/2, hex_center[1] - size[1]/2))
map_panel.blit(map_surface, (0, 0), (0 + scroll_x, 0 + scroll_y, map_panel_s
ize[0], map_panel_size[1]))
if show_debug_text:
screen.blit(font.render(event_text, True, color_orange), (0, 0))
#screen.blit(font.render(hex_info_text, True, color_orange), (0, font_he
ight +2))
screen.blit(font.render(hex_coord_text, True, color_orange), (0, font_he
ight +2))

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)
control_panel.blit(button_zoom_in, (button_offset, button_offset))
control_panel.blit(button_zoom_out, (button_offset, button_offset*2 + button
_area[1]))

#pygame.display.flip()
pygame.display.update()
pygame.quit()

Anda mungkin juga menyukai