Skip to content

Instantly share code, notes, and snippets.

@sli
Created November 24, 2011 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sli/1392416 to your computer and use it in GitHub Desktop.
Save sli/1392416 to your computer and use it in GitHub Desktop.
Hexmap Generator (PyGame)
import sys
import pygame
from pygame.locals import *
##### Rendering Fuctions #####
def drawGrid(context, width, height, cell_size):
column = 1
row = 0
at = 0
grid = pygame.Surface(context.get_size())
grid = grid.convert()
red = pygame.Color(250,0,0,255)
blue = pygame.Color(0,0,250,255)
while row < (height / 20):
while column < (width / 20):
x = column * 20
if at is 0:
y = row * 20
at = 1
c = red
else:
y = (row + 1) * 20
at = 0
c = blue
if y < height:
pygame.draw.rect(grid, c, ((x, y), (cell_size, cell_size)))
column += 2
row += 2
column = 1
at = 0
return grid
##############################
if len(sys.argv) > 2:
try:
# 50px x 50px cells
COLUMNS = int(sys.argv[1])
ROWS = int(sys.argv[2])
except:
print 'Usage: hexmap.py <columns> <rows>'
COLUMNS = 20
ROWS = 10
else:
print 'Usage: hexmap.py <columns> <rows>'
COLUMNS = 20
ROWS = 10
CELL_SIZE = 50
BOARD_WIDTH = COLUMNS*CELL_SIZE
BOARD_HEIGHT = ROWS*CELL_SIZE
print 'Board size: %dx%d' % (BOARD_WIDTH, BOARD_HEIGHT)
pygame.init()
window = pygame.display.set_mode((BOARD_WIDTH, BOARD_HEIGHT))
pygame.display.set_caption('Hexmap')
# Named 'context' for ease of porting from
# HTML5 canvas and Javascript
context = pygame.display.get_surface()
while 1:
for event in pygame.event.get():
if event.type == QUIT:
break
grid = drawGrid(context, BOARD_WIDTH, BOARD_HEIGHT, CELL_SIZE)
context.blit(grid, (0, 0))
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment