public
Created

Pioneers of Katan

  • Download Gist
settlers.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
import pygame, sys, random, math
from pygame.locals import *
 
# set up game
pygame.init()
mainClock = pygame.time.Clock()
 
# create window
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Pioneers of Katan')
 
# location of all possible settlements
# passed to the main board
settlementLoc = []
 
# road locations
roadLoc = []
 
""" Constants """
CLAY = (255, 0, 0)
ORE = (190, 190, 190)
WHEAT = (255, 215, 0)
SHEEP = (124, 252, 0)
WOOD = (0, 100, 0)
SAND = (139, 69, 19)
 
resources = {CLAY: 'Clay', ORE: 'Ore', WHEAT: 'Wheat',
SHEEP: 'Sheep', WOOD: 'Wood', SAND: 'Desert, Nothing'}
 
P1 = (0, 0, 255)
P2 = (255, 20, 147)
P3 = (255, 165, 0)
P4 = (70, 130, 180)
 
# grid locations of all hex tiles
hexTiles = [(0,0), (0,1), (0,-1), (-1,0.5), (-1,-0.5), (1,0.5), (1,-0.5), (2,0), (-2,0), (0,2),
(0,-2), (1,1.5), (1,-1.5), (-1,1.5), (-1, -1.5), (2,1), (2,-1), (-2,1), (-2,-1)]
hexProbabilities = [5, 2, 6, 3, 8, 10, 9, 12, 11, 4, 8, 10, 9, 4, 5, 6, 3, 11]
 
random.shuffle(hexProbabilities)
 
hexResources = [CLAY, CLAY, CLAY, ORE, ORE, ORE, WHEAT, WHEAT, WHEAT, WHEAT, SHEEP,
SHEEP, SHEEP, SHEEP, WOOD, WOOD, WOOD, WOOD]
zipHex = zip(hexResources, hexProbabilities)
 
zipHex += [(SAND, -1)]
 
random.shuffle(zipHex)
 
""" The Board Class """
class Board:
hexes = []
settlements = []
instruction = ""
 
def __init__(self, size):
self.cX = screen.get_rect().centerx
self.cY = screen.get_rect().centery
self.hexHeight = 0.866 * size
""" The Hex's """
for tile in zip(hexTiles, zipHex):
self.hexes.append(Hex(tile[0], size, tile[1][0], tile[1][1], self))
# set up the settlement locations
self.settlements = settlementLoc # may not to do anymore
# update the roads, also for each road, connect the graph of settlements
def draw(self):
# draw the HEXes
for h in self.hexes:
h.draw()
# draw the settlement locations
for item in self.settlements:
item.draw()
# message to user
basicFont = pygame.font.SysFont(None, 24)
text = basicFont.render("Select your first village", True, (255,255,255), (0,0,0))
textRect = text.get_rect()
textRect.left = 5
textRect.top = 560
screen.blit(text, textRect)
""" Create a settlement class"""
class Settlement:
def __init__(self, location, tileOwner):
self.location = location
self.locationx = location[0]
self.locationy = location[1]
self.onTiles = [tileOwner]
self.roadsTo = [] # do I need?
self.city = False
self.owner = -1
def draw(self):
pygame.draw.rect(screen, (0,0,0), (self.locationx, self.locationy, 20, 20),2)
""" The Road Class """
class Road:
def __init__(self, start, end):
self.start = start
self.end = end
self.owner = -1
 
""" Create a hex class """
class Hex:
 
def __init__(self, centre, size, resource, probability, board):
self.centre = centre
self.hexX = board.cX +(1.5*centre[0]*size)
self.hexY = board.cY+(2*centre[1]*board.hexHeight)
self.hexSize = size
self.resource = resource
self.value = probability # chance of getting this resource
self.hasRobber = False
# calculate each 6 points so we only do it once
self.points = []
self.points.append((self.hexX - self.hexSize, self.hexY))
self.points.append((self.hexX - (self.hexSize/2), self.hexY - (0.866*self.hexSize)))
self.points.append((self.hexX + (self.hexSize/2), self.hexY - (0.866*self.hexSize)))
self.points.append((self.hexX + self.hexSize, self.hexY))
self.points.append((self.hexX + (self.hexSize/2), self.hexY + (0.866*self.hexSize)))
self.points.append((self.hexX - (self.hexSize/2), self.hexY + (0.866*self.hexSize)))
# add the points of all settlements to the settlement lists
newSets = [((int(10*math.floor(X / 10))-5), int((10*math.floor(Y / 10))-5)) for (X,Y) in self.points]
 
for s in settlementLoc:
if s.location in newSets:
newSets.remove(s.location)
s.onTiles.append(self)
for ns in newSets: # these are the remainders
settlementLoc.append(Settlement(ns, self))
## going to create all the roads similarly...
newRoads = self.points
newRoads.append(newRoads[0])
print newRoads
# if road doesn't exist already
def draw(self):
# draw the hex
pygame.draw.polygon(screen, self.resource, self.points, 0)
self.points.append(self.points[0])
# draw the roads
for i in range(len(self.points) - 1):
# draw the polygon
pygame.draw.line(screen, (255,20,147), self.points[i], self.points[i+1], 5)
# draw the text
if self.value > 0:
font = pygame.font.Font(None, 36)
text = font.render(str(self.value), 1, (0,0,0))
textpos = text.get_rect()
textpos.left = self.hexX - 5
textpos.top = self.hexY - 10
screen.blit(text, textpos)
 
 
""" Set up the information for the settlements """
settlers = Board(60)
 
# run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
click = pygame.mouse.get_pos()
print click
x = int((10*math.floor(click[0] / 10))-5)
y = int((10*math.floor(click[1] / 10))-5)
test = []
for set in settlers.settlements:
if set.location == (x,y):
test.append(set.location)
for tile in set.onTiles:
print str(resources[tile.resource]) + " is " + str(tile.value)
 
## draw the board
settlers.draw()
 
# draw the window onto the screen
pygame.display.update()
mainClock.tick(30)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.