-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
285 lines (246 loc) · 9.44 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
ransack-python
Ransack - a Python based roguelike
"""
import gzip
import os
import pickle
import random
import pyved_engine as pyv
pyv.bootstrap_e()
def brokendef():
raise NotImplementedError
pyv.pygame.display.flip = brokendef
from internal.DISPLAY import interface, effects, menu, display, text
from internal.HERO import creator
from internal.IMG import images
from internal.SND import sfx
from internal.UTIL import colors, load_image, button
from internal.UTIL import inputHandler
from internal.classes import game
pygame = pyv.pygame
android = False
# const.setScaleFactor(1)
pyv.init(1)
# pygame.init()
pygame.mixer.init()
mixer = pygame.mixer
# Set the height and width of the screen
#cfac = 1.0
# screenSize = [1280, 720] # [720, 700]
# screen = pygame.display.set_mode(screenSize)
screen = pyv.get_surface()
screenSize = screen.get_size()
if not pygame.font:
print('>>>>>>>>> Warning, fonts disabled')
pygame.display.set_caption("Ransack")
# pygame.init()
pygame.key.set_repeat(100, 100)
images.preload_all()
clock = pygame.time.Clock()
random.seed(os.urandom(1))
FX = effects.effects(clock, screen)
SFX = sfx.sfx(mixer)
C = creator.Creator()
iH = inputHandler.inputHandler(FX)
iFace = interface.Interface(screen, iH)
D = display.Display(screen, images)
# this is the static game world i.e. non-generated world
# loaded once at startup and used thereafter
myWorldBall = None
selection = 0
options = ['Begin New Game', 'Load Saved Game', 'Credits', 'Exit']
buttons = []
if pygame.font:
font = pygame.font.Font("./FONTS/chancery.ttf", 60)
y = 350
for o in options:
line = font.render(o, 1, colors.white, colors.black)
buttons.append(
button.Button(((screen.get_width() / 2) - (line.get_width() / 2), y), o)
)
y += 75
ifaceImg, r = load_image.load_image(os.path.join('MENU', 'interface_m.png'), None)
logo, r = load_image.load_image('logo.png', None)
def endScreen(game, msg):
spi = os.getcwd() + "/FONTS/SpinalTfanboy.ttf"
dev = os.getcwd() + "/FONTS/devinne.ttf"
got = os.getcwd() + "/FONTS/gothic.ttf"
dScreen = pygame.Surface((screen.get_width(), screen.get_width()))
dScreen.fill(colors.black)
msgText = text.Text(msg, os.getcwd() + "/FONTS/Squealer.ttf", 18,
colors.white, colors.gold)
dScreen.blit(text.Text("Game Over", spi, 72, colors.red, colors.black),
(50, 0))
font = pygame.font.Font("./FONTS/devinne.ttf", 18)
if game.myHero.level < 4:
dScreen.blit(text.Text("Nice Try, loser!", dev, 18,
colors.white, colors.black), (50, 200))
elif game.myHero.level >= 4 and game.myHero.level < 10:
dScreen.blit(text.Text("Not bad... for a beginner!", dev, 18,
colors.white, colors.black), (50, 350))
dScreen.blit(text.Text("Level reached: {}".format(game.myHero.level),
got, 18, colors.white, colors.black), (50, 500))
dScreen.blit(text.Text("{} days, {}:{}.{}".format(
game.Ticker.getDays(),
game.Ticker.getHours() % 24,
game.Ticker.getMins() % 60,
game.Ticker.getSecs()),
got, 14, colors.white,
colors.black), (50, 250))
'''
screen.blit(pygame.transform.scale(dScreen, (int(ceil(300 * 2.4)),
int(ceil(300 * 2.4)))),
(0, 0) )
'''
screen.blit(dScreen, (0, 0))
# pygame.display.flip()
pyv.flip()
while pygame.event.wait().type != pygame.MOUSEBUTTONDOWN:
pass
def launchNewGame(titleScreen):
print('launchNewGame')
newGame = game.game(
images, screen, clock, iFace, FX, iH, titleScreen, SFX, myWorldBall, loadHero=C.mainLoop(screen)
)
FX.fadeOut(0)
iFace.state = 'game'
if newGame.mainLoop():
endScreen(newGame, "You Win!")
else:
endScreen(newGame, "Game Over.")
FX.fadeOut(0)
def loadWorld():
try:
if android:
pass
loadedWorld = gzip.GzipFile(os.getcwd() + '/MAP/WORLDS/MainWorld', 'rb', 1)
myWorldBall = pickle.load(loadedWorld)
loadedWorld.close()
# self.installWorldBall(ball, context)
except IOError as err:
print('Cannot load world: {}'.format(err))
return
def loadSavedGame(titleScreen):
raise NotImplementedError
# if android:
# android.hide_keyboard()
# try:
# FX.displayLoadingMessage(titleScreen, 'Loading game file...')
# savFile = gzip.GzipFile('ransack0.sav', 'rb', 1)
# FX.displayLoadingMessage(titleScreen, 'Loading saved game...')
# ball = pickle.load(savFile)
# savFile.close()
# Game = game.game(images, screen, clock, iFace,
# FX, iH, titleScreen, SFX, myWorldBall,
# ball[0], ball[1], ball[2], ball[3])
# FX.fadeOut(0)
# iFace.state = 'game'
# if Game.mainLoop():
# endScreen(Game, "You Win!")
# else:
# endScreen(Game, "Game Over.")
# FX.fadeOut(0)
# except IOError as err:
# print('loadSavedGame error: {}'.format(err))
def mouseHandler(m):
(mx, my) = pygame.mouse.get_pos()
if (230 <= mx < 480) and (375 <= my < 430):
launchNewGame()
elif (230 <= mx < 480) and (430 <= my < 485):
loadSavedGame()
elif (230 <= mx < 300) and (485 <= my < 540):
os.sys.exit()
def updateDisplay():
titleScreen = pygame.Surface((screenSize[0], screenSize[1]))
menuBox = pygame.Surface((300, 300))
menuBox.fill(colors.black)
menuBox.set_colorkey(colors.black)
clock.tick(20)
screen.fill(colors.black)
screen.blit(titleScreen, (0, 0))
screen.blit(logo, ((screen.get_width() // 2) - (logo.get_width() // 2), 100))
if pygame.font:
font = pygame.font.Font("./FONTS/SpinalTfanboy.ttf", 48)
for b in buttons:
screen.blit(b.img, (b.locX, b.locY))
# line = font.render(options[i], 1, colors.white, colors.black)
# menuBox.blit(line, (30, (i * line.get_height())))
# menuBox = pygame.Surface( (450,450) )
# menuBox.fill( colors.black )
# menuBox.set_colorkey(colors.black)
def showCredits():
creditsMenu = menu.menu(screen, iH, D, iFace, FX, SFX)
creditsMenu.displayStory(
"Ransack - An RPG Roguelike. All game code, story (if you can call it"
" that!) and artwork, with the exclusion of fonts, created by"
" D. Allen. [email protected] Powered by Python - www.python.org"
" Game engine built with Pygame - pygame.org and ported to Android"
" using PGS4a - pygame.renpy.org/")
creditsMenu.displayStory(
"Fonts used in game: Steelfish, Sqeualer by Ray"" Larabie,"
" Typodermic Fonts - http://www.dafont.com/ typodermic.d1705 Gothic"
" and Chancery by URW Software. urwpp.de/")
creditsMenu.displayStory(
"I'm a big fan of all the classic roguelikes, and"" cult favorites"
" like Castle of the Winds and Moraff's World, as well as"
" Japanese-style RPGs, from Final Fantasy to Suikoden to Pokemon."
" So, you could call this a melting pot of all my gaming influences.")
creditsMenu.displayStory(
"This is a work in progress. I thank you for"" playing and if you"
" notice any bugs or errors, have any ideas for improvments or"
" enhancenments please drop me a line!")
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
selection = None
(mX, mY) = pygame.mouse.get_pos()
for b in buttons:
if b.hit(mX, mY):
selection = b.msg
if selection == 'Begin New Game':
print('Begin New Game {}x{}'.format(screenSize[0],
screenSize[1]))
launchNewGame(screen) # pygame.Surface(screenSize[0], screenSize[1])
elif selection == 'Load Saved Game':
loadSavedGame(pygame.Surface((screenSize[0],
screenSize[1])))
elif selection == 'Credits':
showCredits()
elif selection == 'Exit':
FX.fadeOut(0)
os.sys.exit()
elif selection is None:
pass
font = pygame.font.Font(os.getcwd() + "/FONTS/courier.ttf", 28)
if android:
screen.blit(font.render(str(android.get_dpi()), 1, colors.white,
colors.black), (0, 0))
updateDisplay()
# pygame.display.flip()
pyv.flip()
if __name__ == '__main__':
from internal import MAP as legacyMAP
# fix imports for piclk ok
import sys
sys.modules['MAP'] = legacyMAP
try:
# '''
# if android:
# MW = android.assets.open('WORLDS/MainWorld')
# else:
# MW = open('assets/WORLDS/MainWorld', 'r')
# '''
MW = open('assets/WORLDS/MainWorld', 'rb')
loadedWorld = MW
myWorldBall = pickle.load(loadedWorld)
# print myWorldBall
loadedWorld.close()
except pickle.UnpicklingError as err:
print('Cannot load MainWorld: {}'.format(err))
os.sys.exit()
except IOError as err:
print('Cannot load MainWorld: {}'.format(err))
os.sys.exit()
main()