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()