[ create a new paste ] login | about

Link: http://codepad.org/rDBxiJNH    [ raw code | fork ]

Python, pasted on Oct 13:
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
import random, sys, copy, os, pygame, mobs
from pygame.locals import *
from hud import Hud
from pgu import text

pygame.init()
FPS = 60
WINW = 1100
WINH = 900
HALFW = WINW/2
HALFH = WINH/2
DRAWRANGE = 25
gameWindow = pygame.Rect((0, 0), (40, 600))


TILEW = 64
TILEH = 32
BASICFONT = pygame.font.Font('fonts\\freesansbold.ttf', 10)

RED = (200, 20, 20)
BLUE = (0, 0, 100)
BLACK = (0, 0, 0)
BGCOLOR = BLACK
moblist = []
objectlist = []
mobrectlist = []

DISPLAYSURF = pygame.display.set_mode((WINW, WINH))

IMAGESDICT = {'green': pygame.image.load('green.png').convert_alpha(),
                 'red': pygame.image.load('red.png').convert_alpha(),
                  'cobblebrown': pygame.image.load('cobblebrown.png').convert_alpha(),
                  'grass': pygame.image.load('grass.png').convert_alpha(),
              
                  'NEend':pygame.image.load('gfx\walls\NEend.png').convert_alpha(),
                  'NESEc':pygame.image.load('gfx\walls\NESEc.png').convert_alpha(),
                  'NET':pygame.image.load('gfx\walls\NET.png').convert_alpha(),
                  'NWend':pygame.image.load('gfx\walls\NWend.png').convert_alpha(),
                  'NWNEc':pygame.image.load('gfx\walls\NWNEc.png').convert_alpha(),
                  'NWSWc':pygame.image.load('gfx\walls\NWSWc.png').convert_alpha(),
                  'NWT':pygame.image.load('gfx\walls\NWT.png').convert_alpha(),
                  'SEend':pygame.image.load('gfx\walls\SEend.png').convert_alpha(),
                  'SET':pygame.image.load('gfx\walls\SET.png').convert_alpha(),
                  'SEw':pygame.image.load('gfx\walls\SEw.png').convert_alpha(),
                  'NEend':pygame.image.load('gfx\walls\NEend.png').convert_alpha(),
                  'SEw':pygame.image.load('gfx\walls\SEw.png').convert_alpha(),
                  'SWend':pygame.image.load('gfx\walls\SWend.png').convert_alpha(),
                  'SWSEc':pygame.image.load('gfx\walls\SWSEc.png').convert_alpha(),
                  'SWT':pygame.image.load('gfx\walls\SWT.png').convert_alpha(),
                  'SWw':pygame.image.load('gfx\walls\SWw.png').convert_alpha(),
                  'Wx':pygame.image.load('gfx\walls\Wx.png').convert_alpha()}

WALLMAP = {'}': IMAGESDICT['NEend'],
               '{': IMAGESDICT['NWend'],
               ']': IMAGESDICT['SEend'],
               '[': IMAGESDICT['SWend'],
               'L': IMAGESDICT['NESEc'],
               '^': IMAGESDICT['NET'],
               'k': IMAGESDICT['NWNEc'],
               'x': IMAGESDICT['NWSWc'],
               '<': IMAGESDICT['NWT'],
               '>': IMAGESDICT['SET'],
               '-': IMAGESDICT['SEw'],
               'j': IMAGESDICT['SWSEc'],
               'T': IMAGESDICT['SWT'],
               '|': IMAGESDICT['SWw'],
               '+': IMAGESDICT['Wx']}
    
MOBDICT = {'player': pygame.image.load('char3.png').convert_alpha(),
            'box': pygame.image.load('gfx\\box.png').convert_alpha(),
           'shelf': pygame.image.load('gfx\shelf.png').convert_alpha()}
# Types: quest, hair, earring, ring, head, cloak, necklace, weapon, gloves, offhand, legs, body, boots, belt
#Key: img0, Name1, Type2, Text3, Apt4, Lck5, Pstr6, pcon7, pdex8, OStr9, OCon10, ODex11, Hlth12, Stam13, Def14, Acc15, DmgDice16,
#     W.Res17, ARes18, FRes19, ERes20, SRes21, Spch22, Mech23, Heal24, Conc25, Merc26, Ter27, Long28, Short29, Blunt30, Axe31, Pole32,
#     Throw33, Hist34, Cul35, Proph36, Spawn37, power38, Stories39, OT40, Split41, Tie42, Unravel43, Sight44, Overch45, Seduction46, Intimidation47, Placeholder48, Placeholder49, Weight50, Value51

ITEMDICT = {'test': [pygame.image.load('gfx\hud\\test.png').convert_alpha(), 'Test Object', 'belt', #img0, Name1, Type2
                     'This is a test object! IT IS A BELT. YOU PUT IT ON YOUR WAIST', #Text3
                     1,  2,  3,  4,  5,  6,  7,  8, #Apt4, Lck5, Pstr6, pcon7, pdex8, OStr9, OCon10, ODex11
                     0,  0,  9, 10,  0,  11, 12, 13, #Hlth12, Stam13, Def14, Acc15, DmgDice16, W.Res17, ARes18, FRes19
                     14, 15, 0,  0,  0,  0,  0,  0, #ERes20, SRes21, Spch22, Mech23, Heal24, Conc25, Merc26, Ter27
                     0,  0,  0,  0,  0,  0,  0,  0, #Long28, Short29, Blunt30, Axe31, Pole32, Throw33, Hist34, Cul35
                     0,  0,  0,  0,  0,  0,  0,  0, #Proph36, Spawn37, power38, Stories39, OT40, Split41, Tie42, Unravel43
                     0,  0,  0,  0,  0,  0,  0,  0], #Sight44, Overch45, Sedc46, Intm47, Placeholder48, Placeholder49, Weight50, Value51
            'testring': [pygame.image.load('gfx\items\\testring.png').convert_alpha(), 'Test Ring Object', 'ring', 'This is a test ring object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testring2': [pygame.image.load('gfx\items\\testring2.png').convert_alpha(), 'Test Ring Object TWO', 'ring', 'This is a test ring object!',
                     0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testring3': [pygame.image.load('gfx\items\\testring3.png').convert_alpha(), 'Test Ring Object THREE', 'ring', 'This is a test ring object!',
                     0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testear1': [pygame.image.load('gfx\items\\testear1.png').convert_alpha(), 'Test Ear Object', 'earring', 'This is a test earring object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testear2': [pygame.image.load('gfx\items\\testear2.png').convert_alpha(), 'Test Ear Object TWO', 'earring', 'This is a test earring object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testbelt': [pygame.image.load('gfx\items\\testbelt.png').convert_alpha(), 'Test Belt Object', 'belt', 'This is a test belt object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testboots': [pygame.image.load('gfx\items\\testboots.png').convert_alpha(), 'Test Boots Object', 'boots', 'This is a test boots object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testcloak': [pygame.image.load('gfx\items\\testcloak.png').convert_alpha(), 'Test Cloak Object', 'cloak', 'This is a test cloak object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testgloves': [pygame.image.load('gfx\items\\testgloves.png').convert_alpha(), 'Test Gloves Object', 'gloves', 'This is a test gloves object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testhair1': [pygame.image.load('gfx\items\\testhair1.png').convert_alpha(), 'Test Hair Object ONE', 'hair', 'This is a test hair object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testhair2': [pygame.image.load('gfx\items\\testhair2.png').convert_alpha(), 'Test Hair Object TWO', 'hair', 'This is a test hair object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testhead': [pygame.image.load('gfx\items\\testhead.png').convert_alpha(), 'Test Head Object', 'head', 'This is a test head object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testlegs': [pygame.image.load('gfx\items\\testlegs.png').convert_alpha(), 'Test Legs Object', 'legs', 'This is a test legs object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testneck': [pygame.image.load('gfx\items\\testneck.png').convert_alpha(), 'Test Neck Object', 'necklace', 'This is a test neck object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testoffhand': [pygame.image.load('gfx\items\\testoffhand.png').convert_alpha(), 'Test Offhand Object', 'offhand', 'This is a test offhand object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testweapon': [pygame.image.load('gfx\items\\testweapon.png').convert_alpha(), 'Test Weapon Object', 'weapon', 'This is a test weapon object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            'testbody': [pygame.image.load('gfx\items\\testbody.png').convert_alpha(), 'Test Body Object', 'body', 'This is a test body object!',
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]}


TILEMAP = {'r': IMAGESDICT['red'],
               'g': IMAGESDICT['green'],
               'c': IMAGESDICT['cobblebrown'],
               'f': IMAGESDICT['grass']}

            
player = mobs.Player(objectlist, moblist, mobrectlist, MOBDICT)
container1 = mobs.Container(objectlist, 'container1', MOBDICT, ITEMDICT)
container2 = mobs.Container(objectlist, 'container2', MOBDICT, ITEMDICT)
container3 = mobs.Container(objectlist, 'container3', MOBDICT, ITEMDICT)



def main():
    global FPSCLOCK, DISPLAYSURF, TILEDICT, TILEMAP, IMAGESDICT, moblist, MOBDICT, WALLMAP
    icon = pygame.image.load('gfx\\red.png')
    pygame.display.set_icon(icon)
    cameraX = HALFW - (player.x * 32) + (player.y * 32) - 65
    cameraY = HALFH - 50 - (16 * player.x) - (16 * player.y) - 15
    CAMMOVE = 10
    FPSCLOCK = pygame.time.Clock()



              #x y z [MAPDATA]
    LEVELDAT = [ [ [ ['isoprom.txt', 'isowall.txt', 'exitmap.txt', '', '', [container2, container1, container3], ['moblist']  ] ] ] ]
    

    
    for container in LEVELDAT[0][0][0][5]:
        moblist.append(container)
        mobrectlist.append(container.rect)

            
    DISPLAYSURF.fill(BLACK)
    mapGrid = readMap('isoprom.txt')
    mapWall = readMap('isowall.txt')

    mapSurf = drawMapGrid(mapGrid, player.x, player.y)
    mapSurf = drawWalls(mapWall, mapGrid, mapSurf, moblist)
    HUD = Hud(objectlist)
    redrawMap = False
    cameraMoved = True
            
    PMOVE = {'UP': False,
             'DOWN': False,
             'LEFT': False,
             'RIGHT': False,
             'UPLEFT': False,
             'UPRIGHT': False,
             'DOWNLEFT': False,
             'DOWNRIGHT': False}

    CMOVE = {'UP': False,
             'DOWN': False,
             'LEFT': False,
             'RIGHT': False}
    
    HIGHLIGHT = False
    
    while True:
        
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
                
            elif event.type == MOUSEMOTION:
                for mob in moblist: #Iterate over every clickable game object on the current map (Can this be optimized?)
                        if mob.rect.collidepoint(event.pos[0]+32*player.x + (-32 * (player.y + 1)) + len(mapGrid[0]) * 32 + 32 - 500 + 16,
                                                 event.pos[1] + 16*player.x + 16*player.y + 24 - 400 - 10) and mob.kind != 'player': #Check for mouseover
                            HIGHLIGHT = True
                            SELECTMOB = mob
                            redrawMap = True
                            cameraMoved = True
                            break
                        else:
                            if HIGHLIGHT == True:
                                HIGHLIGHT = False
                                redrawMap = True
                                cameraMoved = True
                
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    if event.pos[1] > (WINH - 180) or event.pos[0] > (WINW - 180): #Check if the player has clicked within the interface frame
                        HUD.clickCheck(DISPLAYSURF, player, event, ITEMDICT, FPSCLOCK)
                        redrawMap = True
                        cameraMoved = True
                    else:
                        for mob in moblist: #Iterate over every clickable game object on the current map (Can this be optimized?)
                            if mob.rect.collidepoint(event.pos[0]+32*player.x + (-32 * (player.y + 1)) + len(mapGrid[0]) * 32 + 32 - 500 + 16,
                                                     event.pos[1] + 16*player.x + 16*player.y + 24 - 400 - 10): #Check for click collision
                                if mob.kind == 'container': #Check if the type is a container. NOTE: Remember to convert dead NPCs to this!
                                    if abs(player.x - mob.x) < 2 and abs(player.y - mob.y) < 2:
                                        HUD.openContainer(DISPLAYSURF, player, mob, ITEMDICT, FPSCLOCK)
                                        HIGHLIGHT = False
                                        redrawMap = True
                                        cameraMoved = True
                                        break
                                    else:
                                        HIGHLIGHT = False
                                elif mob.kind == 'SOMETHING ELSE':
                                    pass
                                
                elif event.button == 3:
                    pass
        
            elif event.type == KEYDOWN:
                if event.key == K_w:
                    PMOVE['UP'] = True
                if event.key == K_s:
                    PMOVE['DOWN'] = True
                if event.key == K_a:
                    PMOVE['LEFT'] = True
                if event.key == K_d:
                    PMOVE['RIGHT'] = True
                if event.key == K_e:
                    PMOVE['UPRIGHT'] = True
                if event.key == K_q:
                    PMOVE['UPLEFT'] = True
                if event.key == K_c:
                    PMOVE['DOWNRIGHT'] = True
                if event.key == K_z:
                    PMOVE['DOWNLEFT'] = True
                    
                if event.key == K_LEFT:  ###Debug camera! Remove this block later!
                    CMOVE['LEFT'] = True
                elif event.key == K_RIGHT:
                    CMOVE['RIGHT'] = True
                elif event.key == K_UP:
                    CMOVE['UP'] = True
                elif event.key == K_DOWN:
                    CMOVE['DOWN'] = True                  
            elif event.type == KEYUP:
                if event.key == K_LEFT:  ###Debug camera! Remove this block later!
                    CMOVE['LEFT'] = False
                elif event.key == K_RIGHT:
                    CMOVE['RIGHT'] = False
                elif event.key == K_UP:
                    CMOVE['UP'] = False
                elif event.key == K_DOWN:
                    CMOVE['DOWN'] = False 
                    
        if PMOVE['UP'] == True and PMOVE['LEFT'] == True:
            PMOVE['UPLEFT'] = True
            PMOVE['UP'] = False
            PMOVE['LEFT'] = False
        if PMOVE['UP'] == True and PMOVE['RIGHT'] == True:
            PMOVE['UPRIGHT'] = True
            PMOVE['UP'] = False
            PMOVE['RIGHT'] = False
        if PMOVE['DOWN'] == True and PMOVE['LEFT'] == True:
            PMOVE['DOWNLEFT'] = True
            PMOVE['DOWN'] = False
            PMOVE['LEFT'] = False
        if PMOVE['DOWN'] == True and PMOVE['RIGHT'] == True:
            PMOVE['DOWNRIGHT'] = True
            PMOVE['DOWN'] = False
            PMOVE['RIGHT'] = False
        
        if PMOVE['UP'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'up', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'up')
            cameraY += 32
            redrawMap = True
            cameraMoved = True
            PMOVE['UP'] = False
        elif PMOVE['DOWN'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'down', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'down')
            cameraY -= 32
            redrawMap = True
            cameraMoved = True
            PMOVE['DOWN'] = False
        elif PMOVE['LEFT'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'left', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'left')
            cameraX += 64
            redrawMap = True
            cameraMoved = True
            PMOVE['LEFT'] = False
        elif PMOVE['RIGHT'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'right', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'right')
            cameraX -= 64
            redrawMap = True
            cameraMoved = True
            PMOVE['RIGHT'] = False
        elif PMOVE['UPRIGHT'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'upright', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'upright')
            cameraX -= 32
            cameraY += 16
            redrawMap = True
            cameraMoved = True
            PMOVE['UPRIGHT'] = False
        elif PMOVE['UPLEFT'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'upleft', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'upleft')
            cameraX += 32
            cameraY += 16
            redrawMap = True
            cameraMoved = True
            PMOVE['UPLEFT'] = False
        elif PMOVE['DOWNRIGHT'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'downright', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'downright')
            cameraX -= 32
            cameraY -= 16
            redrawMap = True
            cameraMoved = True
            PMOVE['DOWNRIGHT'] = False
        elif PMOVE['DOWNLEFT'] == True and mobs.checkMove(mapGrid, mapWall, WALLMAP, player.x, player.y, 'downleft', moblist):
            player.x, player.y = mobs.makeMove(player.x, player.y, 'downleft')
            cameraX += 32
            cameraY -= 16
            redrawMap = True
            cameraMoved = True
            PMOVE['DOWNLEFT'] = False
                    
        if CMOVE['UP']:
            cameraY += CAMMOVE
            cameraMoved = True
        elif CMOVE['DOWN']:
            cameraY -= CAMMOVE
            cameraMoved = True
        if CMOVE['LEFT']:
            cameraX += CAMMOVE
            cameraMoved = True
        elif CMOVE['RIGHT']:
            cameraX -= CAMMOVE
            cameraMoved = True
            

        if redrawMap == True:
            mapSurf = drawMapGrid(mapGrid, player.x, player.y)
            mapSurf = drawWalls(mapWall, mapGrid, mapSurf, moblist)
            redrawMap = False
            
        if cameraMoved:
            DISPLAYSURF.fill(BLACK)
            mapSurfRect = mapSurf.get_rect()
            displayRect = DISPLAYSURF.get_rect()
            mapSurfRect.top = displayRect.top + cameraY
            mapSurfRect.left = displayRect.left + cameraX - len(mapGrid[0])*32
            if HIGHLIGHT == True:
                highlightrect = mob.rect
                pygame.draw.rect(mapSurf, (0,0,100), highlightrect, 2)
            DISPLAYSURF.blit(mapSurf, mapSurfRect)
            HUD.drawHud(DISPLAYSURF, player)
            cameraMoved = False

            
        for direction in PMOVE: PMOVE[direction] = False
        
        pygame.display.update()
        FPSCLOCK.tick(FPS)

        
        fps = FPSCLOCK.get_fps()
        string = str(fps)+ '....'+ str(player.x)+'....'+str(player.y)
        pygame.display.set_caption(string)




def readMap(filename):
    assert os.path.exists(filename), 'Cannot find maps.'
    mapFile = open(filename, 'r')
    contents = mapFile.readlines() + ['\r\n']  #Each map ends in a blank line
    mapFile.close()
    mapLines = []
    mapGrid = []

    for lineNum in range(len(contents)):       
        line = contents[lineNum].rstrip('\r\n')
        if ';' in line:
            line = line[:line.find(';')]
        if line != '':
            mapLines.append(line)
        elif line == '' and len(mapLines) > 0:  #Finds the first blank line after a given map.
            maxXCoord = -1
            for i in range(len(mapLines)):
                if len(mapLines[i]) > maxXCoord:  
                    maxXCoord = len(mapLines[i]) #Finds the longest row
            for i in range(len(mapLines)):
                    mapLines[i] += ' ' * (maxXCoord - len(mapLines[i])) #Ensures each mapgrid is rectangular                   
            for i in range(len(mapLines[0])):                         #range(len(mapLines[0])):
                    mapGrid.append([]) #Append a sublist representing the y axis for every x coordinate possible
            for y in range(len(mapLines)):   
                for x in range(maxXCoord):
                    mapGrid[x].append(mapLines[y][x])
            return mapGrid

def drawMapGrid(mapGrid, playerx, playery):
    mapSurfW = (len(mapGrid) + len(mapGrid[0])) * (TILEW/2)
    mapSurfH = (len(mapGrid) + len(mapGrid[0])) * (TILEH/2)
    mapSurf = pygame.Surface((mapSurfW, mapSurfH))
    mapSurf.fill(BGCOLOR)
    xfrom = getRearRange(player.x)
    yfrom = getRearRange(player.y)
    
    for x in range(playerx - xfrom, playerx + DRAWRANGE):
        for y in range(playery - yfrom, playery + DRAWRANGE):
            left = 32*x + (-32 * (y + 1)) + len(mapGrid[0]) * 32
            top = 16*x + 16*y
            gridRect = pygame.Rect((left, top), (TILEW, TILEH))
            try:
                mapGrid[x][y] in TILEMAP
                tile = TILEMAP[mapGrid[x][y]]
                mapSurf.blit(tile, gridRect)
            except: pass
    return mapSurf

def getRearRange(playercoord):
        for i in range(DRAWRANGE, 0, -1):
            if (playercoord - i) >= 0: return i
        return 0


def drawMoblist(moblist, mapSurf, mapGrid, x, y, mob):
    left = 0
    top = 0
    mob.rect.midbottom = (32*mob.x + (-32 * (mob.y + 1)) + len(mapGrid[0]) * 32 + 32, 16*mob.x + 16*mob.y + 24)
    '''
    if mob.kind == 'container':
        mob.rect.bottom += 7
        '''
    mapSurf.blit(mob.image, mob.rect)
    return

def drawWalls(mapWall, mapGrid, mapSurf, moblist):

    xfrom = getRearRange(player.x)
    yfrom = getRearRange(player.y)
    for x in range(player.x - xfrom, player.x + DRAWRANGE):
        for y in range(player.y - yfrom, player.y + DRAWRANGE):
            left = 32*x + (-32 * (y + 1)) + len(mapGrid[0]) * 32
            top = 16*x + 16*y
            wallRect = pygame.Rect((left, top), (32, 163))
            wallRect.midbottom = (32*x + (-32 * (y + 1)) + len(mapGrid[0]) * 32 + 32, 16*x + 16*y + 40)
            try:
                mapWall[x][y] in WALLMAP
                wall = WALLMAP[mapWall[x][y]]
                mapSurf.blit(wall, wallRect)
            except: pass
            for mob in moblist:
                if x == mob.x and y == mob.y:
                    drawMoblist(moblist, mapSurf, mapGrid, x, y, mob)            
    return mapSurf

def terminate():
    pygame.quit()
    sys.exit()


def drawMessage(DISPLAYSURF, player, message):
    text.write(DISPLAYSURF, font, (0,0), (0,0,0), msg)
    
def title():
    pass

if __name__ == '__main__':
    main()


Create a new paste based on this one


Comments: