forked from raymond1998/Pokemon
/
main.py
262 lines (215 loc) · 6.34 KB
/
main.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
255
256
257
258
259
260
261
262
import pygame
import rgine
import base as base
import menu as menu
import battle as battle
import backpack as backpack
import Combinedv2 as libpkmon
import map as maps
textureSize = 32
ScreenSize = list(map(int, (16*textureSize*1.5, 9*textureSize*1.5)))
screen = pygame.display.set_mode(ScreenSize)
pgbar = rgine.Progressbar((ScreenSize[0]*9//10, ScreenSize[1]*2//10),(0,255,0),250,-50,1)
bsc = base.renderInitScene(ScreenSize, pgbar)
import ctypes
ctypes.windll.user32.MessageBoxW(0, "Close this window to run. ", "Run", 0)
intro_text = rgine.windows.render_text\
(
ScreenSize,
"CLOSE THIS WINDOW TO RUN! \n\n"
"Hit SPACE to show menu. \n"
"W, A, S, D: Directions. \n"
"Tab: Switch Option (msgbox). \n"
"Enter: Confirm the topmost button. \n",
pygame.font.SysFont('Times New Romen', 16),
True, (255, 255, 255)
)
while True:
try:
screen.blit(bsc.__next__(), (0, 0))
except:
# exit(0)
break
screen.blit(intro_text, (0, 0))
pygame.display.flip()
def LoadMap(uPos):
global g_cm, world, terrain
g_cm = mapManager.getMap(uPos)
world = g_cm.world
terrain = g_cm.terrain
evt = rgine.Event()
wm = rgine.windows.WindowsManager(ScreenSize)
maps.init(ScreenSize, textureSize)
mapManager = maps.MapManager()
for i in base.maps:
mapManager.add(maps.Map(*i))
terrain = base.init_terrain("1399339488.terrain", "myTexture.texture", True, textureSize)
terrain.readTextureFromSurface(pygame.image.load("1399339488.jpeg"))
terrain.readTextureProperty("1399648062.textureProperty")
terrain.setProperty(0, 1, 255)
terrain.setProperty(1, 0, 255)
mapManager.add(maps.Map(terrain, (4, 4)))
pManager = base.PlayerManager(libpkmon.player, terrain, base.playerEvent, base.npcs)
pManager.getPlayer().setPos(5+4, 10+4)
LoadMap(pManager.getPlayer().getPos())
npcManager = base.NPCManager()
for i in base.npcs:
npcManager.new(i[0], i[1], base.npcs[i])
pEventList = []
runningNpcEvent = base.NPC(None, None)
uMenu = menu.init_menu(menu.buttons, menu.inst)
uBackpack = backpack.Backpack(wm, pManager.getPlayer())
uBattle = battle.Battle(uBackpack, wm)
menu.inst[2].setBackpack(uBackpack)
import time
fps = 0
t = time.clock()
direction = []
while True:
evt.update()
if evt.type == pygame.QUIT: break
screen.fill((0, 0, 0))
# render terrain to gaming world
rgx, rgy = world.getTerrainRange()
rgx, rgy = list(map(int, rgx)), list(map(int, rgy))
(x0, x1), (y0, y1) = rgx, rgy
terrain.render_s(world.getSurface(), world.Terrain2World(x0-2, y0-2), range(x0-2, x1+2), range(y0-2, y1+2))
# check user event
pManager.setTerrain(terrain)
pManager.updateEvent(evt, wm)
x = y = 0
if not runningNpcEvent.isRunning() and \
not uMenu.isRunning() and \
not uBattle.isRunning() and \
not uBackpack.isRunning():
if evt.isKeyHit(pygame.K_UP):
direction.append(pygame.K_UP)
elif evt.isKeyHit(pygame.K_DOWN):
direction.append(pygame.K_DOWN)
elif evt.isKeyHit(pygame.K_LEFT):
direction.append(pygame.K_LEFT)
elif evt.isKeyHit(pygame.K_RIGHT):
direction.append(pygame.K_RIGHT)
if evt.isKeyUp(pygame.K_UP):
direction.remove(pygame.K_UP)
elif evt.isKeyUp(pygame.K_DOWN):
direction.remove(pygame.K_DOWN)
elif evt.isKeyUp(pygame.K_LEFT):
direction.remove(pygame.K_LEFT)
elif evt.isKeyUp(pygame.K_RIGHT):
direction.remove(pygame.K_RIGHT)
if direction:
if direction[-1] == pygame.K_UP:
y -= 1
elif direction[-1] == pygame.K_DOWN:
y += 1
elif direction[-1] == pygame.K_LEFT:
x -= 1
elif direction[-1] == pygame.K_RIGHT:
x += 1
else:
raise ValueError(direction[-1])
else:
direction = []
# check npc event
player = pManager.getPlayer()
player.normalize_pos()
px, py = player.getPos()
px+=x; py+=y
#npc direction calc.
di = base.DOWN
if x > 0:
di = base.LEFT
elif x < 0:
di = base.RIGHT
elif y > 0:
di = base.UP
for npc, bNpcEvent in npcManager.update(pygame.Rect(rgx[0], rgy[0], rgx[1]-rgx[0], rgy[1]-rgy[0]), (px, py)):
if bNpcEvent:
if not runningNpcEvent.isRunning() and npc.init(evt, wm):
runningNpcEvent = npc
npc.chgDir(di)
else:
runningNpcEvent = base.NPC(None, None)
surf, pos = npc.render(evt, wm)
if surf is None:
npcManager.delete(npc)
else:
world.blit(surf, world.Terrain2World(*pos))
# check player event
(surf, pos), pEvt, bMoving = pManager.update(x, y)
world.blit(surf, world.Terrain2World(*pos))
if pEvt != -1:
pEventList.append(pEvt[0](*pEvt[1]))
if not pEventList[-1].init(evt, wm): pEventList.pop()
# shift world
x, y = ScreenSize
px, py = pos
px -= x/textureSize/2-0.5
py -= y/textureSize/2-0.5
world.setShift(px*textureSize, py*textureSize)
# render gaming world
screen.blit(world.render_s(), (0, 0))
# Player events
for i in pEventList:
surf, pos = i.render(evt, wm)
if surf is None:
i.release(wm)
pEventList.remove(i)
else:
if isinstance(pos, (tuple, list)):
screen.blit(surf, pos)
# User Menu
surf, pos = uMenu.update(evt, wm)
if surf is not None and pos is None: break
# Battle
if evt.isKeyHit(pygame.K_0) and not uBattle.isRunning():
t1 = libpkmon.Pokemon()
t1.load(4, 0000+400)
t2 = libpkmon.Pokemon()
t2.load(1, 500*9+300)
t2.load(1, 500)
p = libpkmon.Player([t2, t1], [], "name", 0, 0)
uBattle.setFightingObjects(player, p)
uBattle.init(evt, wm)
elif evt.isKeyHit(pygame.K_0):
uBattle.release(wm)
result = uBattle.render(evt, wm)[1]
if result is not None and result != 0:
if result == battle.ATK:
print("Winner: ATK, %s"%uBattle._atk.getCurrentPokemon().getName())
elif result == battle.DEF:
print("Winner: DEF, %s"%uBattle._def.getCurrentPokemon().getName())
else:
raise ValueError(result)
uBattle.release(wm)
# Backpack
if evt.isKeyHit(pygame.K_8) and not uBackpack.isRunning():
uBackpack.setPlayer(pManager.getPlayer())
uBackpack.init(evt, wm)
elif evt.isKeyHit(pygame.K_8):
uBackpack.release(wm)
result = uBackpack.render(evt, wm)[1]
if result is not None and result != 0:
print(result)
uBackpack.release(wm)
# WindowsManager should always stay above the world
for hWnd, msg, surface, pos in wm.DispatchMessage(evt):
screen.blit(surface, pos)
# Present
pygame.display.flip()
fps += 1
if time.clock() - t >= 1:
print(fps/(time.clock()-t))
fps = 0
t = time.clock()
pManager.release()
for i in pEventList:
i.release(wm)
pEventList = []
if runningNpcEvent.isRunning():
runningNpcEvent.release(wm)
uMenu.release(wm)
uBattle.release(wm)
wm.Release()
pygame.quit()