-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamestate.py
254 lines (195 loc) · 8.37 KB
/
gamestate.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import numpy as np
import pygame as pg
from scipy.stats import powerlaw
from random import randrange
from palette import RED, BLUE, CYAN, WHITE
from planet import PLANET_NAMES, P_IDX, PLANET_IMG
from drawutils import draw_dashed_line, glow
def dist(a, b):
return np.linalg.norm(np.array(a) - np.array(b))
class Score:
def __init__(self):
self._guns = 0
self._fun = 0
class Planet:
def __init__(self, idx, pop, coords):
self._idx = idx
self._pop = pop
self._owner = None
self._visible = False
self._explorable = False
self._pos = coords
self._name = PLANET_NAMES[(P_IDX + idx) % len(PLANET_NAMES)]
self._img = pg.image.load(PLANET_IMG[(P_IDX + idx) % len(PLANET_IMG)])
self._img = pg.transform.scale(self._img, (100, 100))
self._gimg = pg.image.load(PLANET_IMG[(P_IDX + idx) % len(PLANET_IMG)]
.split('.')[0] + 'gray.png')
self._gimg = pg.transform.scale(self._gimg, (100, 100))
self._popguns = 0
self._popfun = self._pop
self._explorecost = 2
self._conquercost = int(randrange(pop / 2, pop * 2))
assert (self._popfun + self._popguns) == self._pop
def upFun(self):
if (self._popfun < self._pop):
self._popfun += 1
self._popguns -= 1
assert (self._popfun + self._popguns) == self._pop
def upGuns(self):
if (self._popguns < self._pop):
self._popguns += 1
self._popfun -= 1
assert (self._popfun + self._popguns) == self._pop
def nextTurn(self):
self._pop += 1
self._popfun += 1
def exploredStr(self):
return "%s: Population: %d Guns: %d" % (self._name, self._pop,
self._conquercost)
def unknownStr(self):
return "Unknown planet: Population: ? Guns: ? Explore cost: %d" % \
self._explorecost
def funStr(self):
return "Fun: %d" % self._popfun
def gunStr(self):
return "Gun: %d" % self._popguns
def __str__(self):
if self._visible:
return self.exploredStr()
else:
return self.unknownStr()
class GameState:
def __init__(self, lvlmap):
self._lvlmap = lvlmap
pops = [max(1, int(x * 10))
for x in powerlaw.rvs(1.2, size=len(lvlmap.coords()))]
start_planet = None
planets = []
for i in range(0, len(lvlmap.coords())):
planets.append(Planet(i, pops[i], lvlmap.coords()[i]))
if (start_planet is None or
dist((0, 0), planets[-1]._pos) <
dist((0, 0), start_planet._pos)):
start_planet = planets[-1]
assert start_planet is not None
start_planet._owner = 'P1'
start_planet._visible = True
start_planet._explorable = True
start_planet._pop = 4
start_planet._popfun = 4
for p in planets:
if p._idx in lvlmap.getNeighbors(start_planet._idx):
p._explorable = True
self._planets = planets
self._info = ''
self._turn = 1
self._selected = start_planet
self._exploreList = set()
self._conquerList = set()
self._score = Score()
def getPlanets(self):
return self._planets
def nextTurn(self):
if self._turn == 20:
return
self._turn += 1
for p in self._exploreList:
p._visible = True
for n in self._lvlmap.getNeighbors(p._idx):
self.getPlanets()[n]._explorable = True
for p in self._conquerList:
p._owner = 'P1'
p._explorecost = 0
self._exploreList.clear()
self._conquerList.clear()
for p in self.getPlanets():
if p._owner == 'P1':
self._score._guns += p._popguns
self._score._fun += p._popfun
p.nextTurn()
def upKey(self):
self._selected.upFun()
def downKey(self):
self._selected.upGuns()
def click(self, pos):
for p in self.getPlanets():
if (dist(pos, p._pos) < 40):
if p._owner == 'P1':
self._selected = p
elif p._visible and p._owner != 'P1':
# conquer this planet
if p in self._conquerList:
self._conquerList.remove(p)
self._score._guns += p._conquercost
# return guns
elif self._score._guns >= p._conquercost:
self._conquerList.add(p)
self._score._guns -= p._conquercost
elif p._explorable and not p._visible:
if p in self._exploreList:
self._exploreList.remove(p)
self._score._guns += p._explorecost
elif self._score._guns >= p._explorecost:
self._exploreList.add(p)
self._score._guns -= p._explorecost
def hover(self, pos):
self._info = ''
for p in self.getPlanets():
if not p._explorable:
continue
if (dist(pos, p._pos) < 40):
self._info = str(p)
def endGame(self, screen):
endfont = pg.font.Font(None, 64)
txt = endfont.render("Game over! Score: %d!" % self._score._fun, True,
WHITE)
screen.blit(txt, (screen.get_width() / 2 - txt.get_width() / 2,
screen.get_height() / 2 - txt.get_height() / 2))
restartfont = pg.font.Font(None, 48)
txt = restartfont.render("Press enter to begin again", True, WHITE)
screen.blit(txt, (screen.get_width() / 2 - txt.get_width() / 2,
(screen.get_height() / 2 - txt.get_height() / 2) + 32))
def draw(self, screen):
font = pg.font.Font(None, 36)
text = font.render(self._info, True, WHITE)
turn = font.render("Turn %d Guns: %d Fun: %d " % (self._turn,
self._score._guns,
self._score._fun),
True, WHITE)
screen.blit(text, (10, 10))
screen.blit(turn, (900, 10))
for p in self._planets:
if not p._visible and not p._explorable:
continue
if p._visible:
screen.blit(p._img, (p._pos[0] - p._img.get_width() / 2,
p._pos[1] - p._img.get_height() / 2))
if p._owner == 'P1':
oursglow = glow((p._img.get_width(), p._img.get_height()),
CYAN)
screen.blit(oursglow, (p._pos[0] - p._img.get_width() / 2,
p._pos[1] - p._img.get_height() / 2))
infofont = pg.font.Font(None, 24)
funtxt = infofont.render(p.funStr(), True, WHITE)
guntxt = infofont.render(p.gunStr(), True, WHITE)
screen.blit(funtxt, (p._pos[0] + 35, p._pos[1] - 12))
screen.blit(guntxt, (p._pos[0] + 35, p._pos[1] + 12))
for n in self._lvlmap.getNeighbors(p._idx):
neighborp = self.getPlanets()[n]
draw_dashed_line(screen, RED, p._pos, neighborp._pos,
width=1, dash_length=5)
elif p._explorable:
screen.blit(p._gimg, (p._pos[0] - p._gimg.get_width() / 2,
p._pos[1] - p._gimg.get_height() / 2))
if p in self._exploreList:
explore_glow = glow((p._img.get_width(), p._img.get_height()),
BLUE)
screen.blit(explore_glow, (p._pos[0] - p._img.get_width() / 2,
p._pos[1] - p._img.get_height() / 2))
if p in self._conquerList:
conquer_glow = glow((p._img.get_width(), p._img.get_height()),
RED)
screen.blit(conquer_glow, (p._pos[0] - p._img.get_width() / 2,
p._pos[1] - p._img.get_height() / 2))
if self._turn == 20:
self.endGame(screen)