-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmo copy 2.py
671 lines (564 loc) · 21 KB
/
cosmo copy 2.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#!/usr/bin/python
# -*- coding=UTF-8 -*-
'''
==========================================
////// ////// ////// // // //////
// // // // /\ /// // //
// // // ////// //\/// // //
// // // // // // // //
////// ////// ////// // // //////.py
==========================================
Merlin Katz
Zoë Bentley
Started 4/20/16
'''
import os
import time
from time import sleep
from math import ceil, floor
####################################
import curses, locale #
from curses import wrapper # getch
#
locale.setlocale(locale.LC_ALL,'') #
####################################
class Frame:
def __init__(self, player, loaded):
self.player = player
self.loaded = loaded
self.ratioAdjust = 1
self.lockEdges = True
self.mapWidth = 41
self.mapHeight = 21
self.update()
def update(self, alert=''):
string = ''
string += self.boxStr(self.player.area.name, '=', '', ' |', '| ')
string += '\n'
mapString = self.mapString()
boxMap = self.boxStr(mapString, '=', '=', '|', '|')
string += boxMap
string += '\n'
string += self.bar('', '', ' |', '| ', ' | ', [13, -1, 13], ['HEALTH: ' + unichr(9829)*self.player.health, '<space>', 'MANA: ' + unichr(11821)*self.player.mana], len(boxMap.split('\n')[0]))
string += '\n'
string += self.boxStr((u'{:' + str(realLen(mapString.split('\n')[0]) - 2) + u'}').format(alert), '=', '=', ' |', '| ')
stdscr.clear()
stdscr.addstr(string.encode('utf-8')) #main program draw fn
stdscr.refresh()
def bar(self, top, bottom, right, left, middle, spacing, info, fit=0):
if len(info) != len(spacing):
raise Exception('info and spacing must be same length')
boxes = []
for i in range(len(info)):
if spacing[i] == -1:
if info[i] == '<space>':
tempSpacing = spacing
tempSpacing[i] = 0
spacing[i] = fit - len(self.bar(top, bottom, right, left, middle, tempSpacing, info).split('\n')[0])
boxRight = right
boxLeft = left
if i != 0:
if info[i-1] != '<space>':
boxLeft = middle
if i != len(info) - 1:
if info[i+1] != '<space>':
boxRight = ''
if info[i] == '<space>':
boxes += [self.boxStr(' ' *spacing[i], ' '*realLen(top), ' '*realLen(bottom), '', '')]
else:
boxes += [self.boxStr((u'{:' + str(spacing[i]) + u'}').format(info[i]), top, bottom, boxRight, boxLeft)]
boxes = [x.split('\n') for x in boxes]
bar = boxes[0]
del boxes[0]
for box in boxes:
for i in range(len(box)):
bar[i] += box[i]
string = ''
for line in bar:
string += line + '\n'
string = string[:-1]
return string
def start(self):
alert = ''
alertTime = time.time()
inspect = False
interact = False
up = 'UP'
down = 'DOWN'
right = 'RIGHT'
left = 'LEFT'
while True:
update = False
if len(alert) > 0 and time.time()-alertTime > 5:
alert = ''
update = True
ch = getch()
if ch in [up, down, left, right]:
direction = {up:'up', down:'down', right:'right', left:'left'}[ch]
if inspect:
alert = self.player.inspect(direction)
alertTime = time.time()
inspect = False
elif interact:
val = self.player.interact(direction)
if val != None:
alert = val
alertTime = time.time()
interact = False
else:
a = self.player.move(direction)
if a != None:
alert = a
alertTime = time.time()
update = True
if ch == ord(unicode('i')):
interact = False
if inspect:
alert = self.player.inspect()
alertTime = time.time()
update = True
inspect = not inspect
if ch == ord(unicode(' ')):
inspect = False
if interact:
val = self.player.interact()
if val != None:
alert = val
alertTime = time.time()
update = True
interact = not interact
if ch == ord(unicode('q')):
break
if update:
self.update(alert)
def mapString(self):
nullChar = ' '
area = self.player.area
x = self.player.x
y = self.player.y
positionx, positiony = x - int(floor(self.mapWidth/2.0)), y - int(floor(self.mapHeight/2.0))
if self.lockEdges:
if positionx < 0:
positionx = 0
if positiony < 0:
positiony = 0
if positionx > 0-(self.mapWidth - area.width):
positionx = 0-(self.mapWidth - area.width)
if positiony > 0-(self.mapHeight - area.height):
positiony = 0-(self.mapHeight - area.height)
if area.width <= self.mapWidth:
positionx = int(floor((self.mapWidth - area.width)/-2.0))
if area.height <= self.mapHeight:
positiony = int(floor((self.mapHeight - area.height)/-2.0))
grid = map(list,zip(*area.grid))
string = ''
for i in range(self.mapHeight):
for j in range(self.mapWidth):
if j > 0:
string += ' '*(self.ratioAdjust - 1)
drawi, drawj = i + positiony, j + positionx
if x == drawj and y == drawi:
string += self.player.playerChar
elif drawi < 0 or drawj < 0 or drawi > area.height-1 or drawj > area.width-1:
string += nullChar
else:
string += grid[drawi][drawj]
string += '\n'
string = string[:-1]
return string
# self.player.area.drawSelf(self.player, message=alert)
def inv(self):
items = self.player.items
selected = 0
while True:
pass
def boxStr(self, content, top, bottom, right, left, alert=''):
content = content.split('\n')
framed = []
for char in top:
if char == ' ':
framed += [left + char*len(content[0]) + right]
else:
framed += [char*(len(content[0]) + len(right + left))]
for i in range(len(content)):
framed += [left + content[i] + right]
for char in bottom:
if char == ' ':
framed += [left + char*len(content[0]) + right]
else:
framed += [char*(len(content[0]) + len(right + left))]
string = ''
for line in framed:
string += line + '\n'
if alert != '':
string += '\n' + alert + '\n'
string = string[:-1]
return string
class Player:
def __init__(self, loaded):
self.playerChar = unichr(186)#9879 #186
self.loaded = loaded
self.items = []
self.health = 5
self.mana = 5
def placeInArea(self, areaName, enterFrom=None, x=None, y=None):
if not self.loaded.has_key(areaName):
if not os.path.exists(areaName + '.txt'):
return False
self.loaded[areaName] = Area(areaName)
self.area = self.loaded[areaName]
if enterFrom != None:
self.x, self.y = self.area.findEntrance(enterFrom)
elif x > 0 and y > 0 and x < self.area.width and y < self.area.height:
self.x, self.y = x, y
else:
raise Exception('Player not placed in a proper location (Off map or coordinates not given)')
return True
def move(self, direction):
val = None
x = self.x
y = self.y
exec({'up':'y -= 1', 'down':'y += 1', 'right':'x += 1', 'left':'x -= 1'}[direction])
collide = self.area.collide(x, y, self.x, self.y)
if collide[0] == 'warp':
self.placeInArea(collide[1], collide[2])
elif collide[0] == 'item':
self.items += collide[1]
self.x = x
self.y = y
# self.area.drawSelf(self, message=(u'Obtained -{}-'.format(collide[1]['name'])))
val = u'Obtained -{}-'.format(collide[1]['name'])
elif collide == 'free':
self.x = x
self.y = y
return val
def inspect(self, direction=None):
x = self.x
y = self.y
exec({'up':'y -= 1', 'down':'y += 1', 'right':'x += 1', 'left':'x -= 1', None:'pass'}[direction])
name, text = self.area.inspect(x,y)
# self.area.drawSelf(self, message=(u'-{}- {}'.format(name, text)))
return u'-{}- {}'.format(name, text)
def interact(self, direction=None):
x = self.x
y = self.y
exec({'up':'y -= 1', 'down':'y += 1', 'right':'x += 1', 'left':'x -= 1', None:'pass'}[direction])
val = self.area.interact(self, x, y)
if type(val) == type(''):
return val
if val == None:
return None
if val[0] == 'npc':
npc = val[1]
if not npc.has_key('interactions'):
npc['interactions'] = 0
name = npc['name']
imageName = npc['imageName']
textName = npc['textNames'][npc['interactions']]
conversation(name, imageName, textName)
if len(npc['textNames']) - 1 > npc['interactions']:
npc['interactions'] += 1
class Area:
def __init__(self, areaName):
self.areaName = areaName
area = open(areaName + '.txt','r')
area = area.read()
splitter = area.split('\n<splitter>\n')
self.name = splitter[0]
self.area = splitter[1].split('\n')
self.blocking = splitter[2].split('\n')
self.attributes = splitter[3]
self.chars = splitter[4]
self.area = map(list, zip(*self.area))
self.blocking = map(list, zip(*self.blocking))
exec('self.attributes = ' + self.attributes)
exec('self.chars = ' + self.chars)
self.grid = []
for i in self.area:
self.grid += [list(i)]
self.blockingGrid = []
for i in self.blocking:
self.blockingGrid += [list(i)]
self.width = len(self.grid)
self.height = len(self.grid[0])
self.initChars()
def initChars(self):
for key in self.chars:
char = self.chars[key]
for i in range(self.width):
for j in range(self.height):
if self.grid[i][j] == key:
self.grid[i][j] = unichr(char)
def findEntrance(self, entranceName):
for key in self.attributes:
att = self.attributes[key]
if att['type'] == 'ENTER' or att['type'] == 'EDGE':
if att['warpfrom'] == entranceName:
for i in range(self.width):
for j in range(self.height):
if self.blockingGrid[i][j] == key:
return i,j
def drawSelf(self, player, message=None):
grid = zip(*self.grid)
grid = map(list,grid)
grid[player.y][player.x] = player.playerChar
string = ''
for i in grid:
line = ''
for j in i:
line += j
string += line + '\n'
stdscr.clear()
if message != None:
string += '\n' + str(message)
stdscr.addstr(string.encode('utf-8'))
stdscr.refresh()
def collide(self, x, y, oldx, oldy):
if x < 0 or y < 0 or x > self.width-1 or y > self.height-1:
block = self.attributes[self.blockingGrid[oldx][oldy]]
if block['type'] == 'EDGE':
return ['warp', block['warpto'], block['warpname']]
else:
return 'blocked'
block = self.attributes[self.blockingGrid[x][y]]
blockType = block['type']
if blockType == 'BLOCK':
return 'blocked'
if blockType == 'OBJECT':
return 'blocked'
elif blockType == 'EXIT':
return ['warp', block['warpto'], block['warpname']]
elif blockType == 'ITEM':
self.grid[x][y] = ' '
self.blockingGrid[x][y] = ' '
return ['item', block]
elif blockType == 'NPC':
return 'blocked'
else:
return 'free'
def inspect(self, x, y):
if x < 0 or y < 0 or x > self.width-1 or y > self.height-1:
return 'EDGE', 'The edge of this area. Do you continue?'
blockName = self.attributes[self.blockingGrid[x][y]]['name']
blockText = self.attributes[self.blockingGrid[x][y]]['text']
return blockName, blockText
def interact(self, player, x, y):
if x < 0 or y < 0 or x > self.width-1 or y > self.height-1:
return None
block = self.attributes[self.blockingGrid[x][y]]
blockType = block['type']
if blockType == 'NPC':
return ['npc', block]
else:
return None
def getch():
curses.flushinp()
ch = stdscr.getch()
if ch == -1:
ch = None
if ch == 27:
ch = stdscr.getch()
ch = stdscr.getch()
if ch == 65:
ch = 'UP'
if ch == 66:
ch = 'DOWN'
if ch == 67:
ch = 'RIGHT'
if ch == 68:
ch = 'LEFT'
if ch == 10:
ch = 'RETURN'
return ch
def realLen(string):
n = 0
for s in string:
if s == '\xc3':
n += 1
if s == '\xe2':
n += 2
return len(string) - n
def branchList(lines, layer=0):
lineList = []
while len(lines) > 0:
line = lines[0]
spacenum = len(line) - len(line.lstrip(' '))
line = line.lstrip(' ')
if spacenum == layer:
del lines[0]
lineList += [line]
if spacenum < layer:
return lineList
if spacenum > layer:
lineList += [branchDict(lines, layer+1)]
return lineList
def branchDict(lines, layer):
lineDict = {}
while len(lines) > 0:
line = lines[0]
spacenum = len(line) - len(line.lstrip(' '))
line = line.lstrip(' ')
if spacenum == layer:
del lines[0]
lineDict[line] = branchList(lines, layer+1)
if spacenum < layer:
return lineDict
if spacenum > layer:
raise Exception('Improper conversation indentation')
return lineDict
def say(text, attributes, conditions=None):
image = open(attributes['imageName'] + '.txt','r')
image = image.read()
if attributes['flip']:
flipimage = ''
image = image.split('\n')
for i in range(len(image)-1):
flipimage += (image[i] + ' '*(attributes['bubbleWidth']-len(image[i])))[::-1].replace('/','<forward slash>').replace('\\','/').replace('<forward slash>','\\') + '\n'
image = flipimage
string = ''
string += image
string += bubble(text, attributes, 1)
if conditions != None:
topstring = string
selected = 0
while True:
string = topstring
string += '\n'
for i in range(len(conditions)):
pointer = i == selected
string += bubble(conditions[i], attributes, 0, pointer, False)
stdscr.clear()
stdscr.addstr(string)
stdscr.refresh()
ch = getch()
if ch == 'UP':
if selected > 0:
selected -= 1
else:
selected = len(conditions)-1
if ch == 'DOWN':
if selected < len(conditions)-1:
selected += 1
else:
selected = 0
if ch == ord(unicode(' ')) or ch == 'RETURN':
return conditions[selected]
stdscr.clear()
stdscr.addstr(string)
stdscr.refresh()
ch = getch()
while ch != ord(unicode(' ')) and ch != 'RETURN':
ch = getch()
def branchRead(lines, attributes):
while len(lines) > 0:
if type(lines[0]) == type({}):
raise Exception('Used dictionary before prompt')
if len(lines) >= 2:
if type(lines[1]) == type({}):
response = say(lines[0], attributes, lines[1].keys())
branchRead(lines[1][response], attributes)
del lines[0:2]
else:
say(lines[0], attributes)
del lines[0]
else:
say(lines[0], attributes)
del lines[0]
def makeFit(text, space):
text = text.split(' ')
newtext = []
n = 0
for word in text:
if realLen(word) + n > space:
newtext += ['<new line>', word]
n = realLen(word) + 1
else:
newtext += [word]
n += realLen(word) + 1
text = ''
for word in newtext:
text += word + ' '
return text[:-1]
def bubble(text, attributes, bubbleSpace, pointer=False, sayName=True):
text = makeFit(text.strip(' '), attributes['bubbleWidth']-6)
string = ''
if sayName: #Put attributes['name'] at top of text bubble.
string += '/' + '='*((attributes['bubbleWidth']-4)/2 - int(ceil(float(realLen(attributes['name']))/2))) + ' ' + attributes['name'] + ' ' + '='*((attributes['bubbleWidth']-4)/2 - int(floor(float(realLen(attributes['name']))/2))) + '\\\n'
else:
string += '/' + '='*(attributes['bubbleWidth']-2) + '\\\n'
for i in range(bubbleSpace):
string += '|' + ' '*(attributes['bubbleWidth']-2) + '|\n'
firstline = True
for line in text.split(' <new line> '):
if pointer and firstline:
firstline = False
string += '| > ' + line + ' '*(attributes['bubbleWidth']-(realLen(line)+5)) + '|\n'
else:
string += '| ' + line + ' '*(attributes['bubbleWidth']-(realLen(line)+3)) + '|\n'
for i in range(bubbleSpace):
string += '|' + ' '*(attributes['bubbleWidth']-2) + '|\n'
string += '\\' + '='*(attributes['bubbleWidth']-2) + '/\n'
return string
def conversation(name, imageName, textName, flip=False):
imagetext = open(textName + '.txt','r')
imagetext = imagetext.read()
bubbleWidth = 50
attributes = {'imageName':imageName, 'flip':flip, 'name':name, 'bubbleWidth':bubbleWidth}
lines = branchList(imagetext.split('\n'))
branchRead(lines, attributes)
# first = True
# while len(lines) > 0:
#
#
# if first:
# sleep(0.5)
# first = False
# curses.flushinp()
# while not stdscr.getch() == ord(' '):
# pass
def main(stdscr):
curses.noecho()
curses.curs_set(0)
curses.halfdelay(1)
loaded = {'area1':Area('area1')}
player = Player(loaded)
player.placeInArea('area1', enterFrom='house1Exit1')
frame = Frame(player, loaded)
frame.start()
stdscr.clear()
stdscr.addstr('Thanks for playing! Screw yourself sideways!')
stdscr.refresh()
while stdscr.getch() == -1:
pass
# while True:
# stdscr.addstr(str(getch()))
# conversation('Mom', 'mom', 'zoemomtext', True)
# conversation('Mom', 'mom', 'momtext1')
# stdscr.clear()
# stdscr.refresh()
# sleep(1)
# conversation('Mommy', 'mom', 'momtext2',flip=True)
# stdscr.clear()
# stdscr.refresh()
# sleep(1)
# conversation('Mama', 'mom', 'momtext3')
# stdscr.clear()
# stdscr.refresh()
# sleep(1)
# conversation('Mother', 'mom', 'momtext4',flip=True)
if __name__ == '__main__':
try:
stdscr = curses.initscr()
main(stdscr)
curses.endwin()
finally:
if 'stdscr' in locals():
stdscr.keypad(0)
curses.flushinp()
curses.echo()
curses.nocbreak()
curses.endwin()
#184 = poop
#186 = cosmo
#8770 = bacon0
#8771 = bacon1