-
Notifications
You must be signed in to change notification settings - Fork 1
/
Map.py
160 lines (118 loc) · 4.09 KB
/
Map.py
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
import random, math, pygame
class Map():
def __init__(self, xSize, ySize):
self.xSize = xSize
self.ySize = ySize
self.scale = 30
self.viewPos = [0,0]
self.map = [[0 for x in xrange(self.xSize)] for y in xrange(self.ySize)]
pygame.font.init()
self.font = pygame.font.Font(None, 30)
def getNeighbors(self, x, y):
return (self.map[x][y])
def getTileAt(self, pos):
return self.map[pos[0]][pos[1]]
def generate(self):
islandPos = [random.uniform(0,self.xSize/8), random.uniform(0,self.ySize/8)]
islandPos[0] += self.xSize/2 - self.xSize/8
islandPos[1] += self.ySize/2 - self.ySize/8
islandScale = [random.uniform(0,self.xSize/2), random.uniform(0,self.ySize/2)]
islandScale[0] += (islandScale[0]+islandScale[1])/2
islandScale[1] += (islandScale[0]+islandScale[1])/2
for x in range(len(self.map)):
for y in range(len(self.map[x])):
#self.map[x][y] += random.randint(0,1)
self.map[x][y] = min(80/((x-islandPos[0])**2 + (y-islandPos[1])**2 + 0.0001), 2)
level1 = [[0 for x in xrange(self.xSize/8)] for y in xrange(self.ySize/8)]
for x in range(len(level1)):
for y in range(len(level1[x])):
level1[x][y] = random.uniform(-1,1)
level2 = [[0 for x in xrange(self.xSize/4)] for y in xrange(self.ySize/4)]
for x in range(len(level2)):
for y in range(len(level2[x])):
level2[x][y] = random.uniform(-1,1)
level3 = [[0 for x in xrange(self.xSize/2)] for y in xrange(self.ySize/2)]
for x in range(len(level3)):
for y in range(len(level3[x])):
level3[x][y] = random.uniform(-1,1)
level4 = [[0 for x in xrange(self.xSize/1)] for y in xrange(self.ySize/1)]
for x in range(len(level4)):
for y in range(len(level4[x])):
level4[x][y] = random.uniform(-1,1)
for x in range(len(self.map)):
for y in range(len(self.map[x])):
self.map[x][y] *= level1[x/8][y/8]*0.25+1
self.map[x][y] *= 0.5*level2[x/4][y/4]*0.25+1
self.map[x][y] *= 0.25*level3[x/2][y/2]*0.25+1
self.map[x][y] *= 0.125*level4[x/1][y/1]*0.25+1
for x in range(len(self.map)):
for y in range(len(self.map[x])):
if self.map[x][y] > 1:
if random.random() > 0.95:
self.map[x][y] = 9
def draw(self, surf):
for x in range(len(self.map)):
for y in range(len(self.map[x])):
#print x, y, self.map[x][y]
color = (0,0,0)
if self.map[x][y] < 1:
color = (10,30,90)
elif self.map[x][y] < 2:
color = (230,200,100)
elif self.map[x][y] < 3:
color = (50,230,30)
elif self.map[x][y] < 10:
color = (240,50,80)
#color = (random.random()*255,random.random()*255,random.random()*255)
#pygame.draw.rect(surf, color, (x*20,y*20,20,20))
#pygame.draw.circle(surf, color, [int(self.project([x,y])[0]+2*side), int(self.project([x,y])[1]+2*side)], self.scale/2)
pygame.draw.polygon(surf, color, hexagon(self.project([x,y]), self.scale/2))
pygame.draw.lines(surf, (255,255,255), True, hexagon(self.project([x,y]), self.scale/2))
self.drawHUD(surf)
def drawHUD(self, surf):
label = self.font.render("Beta", 1, (255,255,255))
surf.blit(label, (10,10))
def project(self, worldPos):
short = self.scale/(2*math.sqrt(3))
side = short*2
x = int(worldPos[0]*(short*3)+self.viewPos[0])
if worldPos[0] % 2 == 1:
yOff = 0*(self.scale/2)
else:
yOff = -(self.scale/2)
y = int(worldPos[1]*self.scale+yOff+self.viewPos[1])
return [x, y]
def move(self, pos, dir, dist):
dir %= 6
neighbors = [
[ [+1, 0], [+1, -1], [ 0, -1],
[-1, -1], [-1, 0], [ 0, +1] ],
[ [+1, +1], [+1, 0], [ 0, -1],
[-1, 0], [-1, +1], [ 0, +1] ]
]
parity = pos[0]%2
d = neighbors[parity][dir]
return [pos[0] + d[0], pos[1] + d[1]]
def hexagon(pos, size):
x = pos[0]
y = pos[1]
sqrt3 = math.sqrt(3)
short = size/sqrt3
side = short*2
vertices = []
for i in range(6):
angle = 2 * math.pi / 6 * (i + 0.5)
vertices.append([size * math.cos(angle) + x, size * math.cos(angle) + y])
#print vertices
#return vertices
return (
(x+short, y),
(x+short+side, y),
(x+short*2+side, y+size),
(x+short+side, y+size*2),
(x+short, y+size*2),
(x, y+size)
)
if __name__ == "__main__":
import Main
Main.run()