def generate_shop(w, h, shop_items): level = Level(w, h, is_shop=True) for x,y in range2d(w, h): t = level.tiles[x][y] if x == 0 or y == 0 or x == w-1 or y == h-1: t.img = spr.WOOD_WALL t.blocking = True t.transparent = False else: t.img = spr.WOOD_FLOOR t.blocking = False t.transparent = True for a,b in box2d(2, 2, 5, 5): level.add_item((a,b), random.choice(shop_items)) for a,b in box2d(9, 2, 5, 5): level.add_item((a,b), random.choice(shop_items)) for a,b in box2d(2, 9, 5, 5): level.add_item((a,b), random.choice(shop_items)) for a,b in box2d(9, 9, 5, 5): level.add_item((a,b), random.choice(shop_items)) level.illuminated = True return level
def field_of_view(self): for x,y in box2d(-12, -12, 25, 25): ax, ay = x+self.x, y+self.y if not self.cur_level.in_bounds(ax, ay): continue if self.can_see(ax, ay): yield (ax,ay)
def field_of_view(self, x1, y1, r=25): seeable = [] for x,y in box2d(-12, -12, 25, 25): # for each tile in map size: ax, ay = x1+x, y1+y if not self.in_bounds(ax, ay): continue if self.sight(x1, y1, ax, ay, r): yield (ax, ay)
def generate_dungeon(w, h, difficulty=1): level = Level(w, h) new_map = [[0 for _ in xrange(h)] for _ in xrange(w)] # initialize new_map to noise (0.43% filled) for x,y in range2d(w, h): if random.random() < 0.43: new_map[x][y] = 1 # apply cellular automation for i in xrange(2): temp_map = [[0 for _ in xrange(h)] for _ in xrange(w)] for x,y in range2d(w, h): wall_count = 0 for i,j in box2d(x-1, y-1, 3, 3): if 0 <= i < w and 0 <= j < h: wall_count += new_map[i][j] else: # sides = instawall wall_count += 3 if wall_count >= 5: temp_map[x][y] = 1 new_map = temp_map # apply changes to actual map for x,y in range2d(w, h): tile = level.tiles[x][y] if new_map[x][y] == 1: tile.img = spr.ROCK tile.blocking = True tile.transparent = False else: tile.img = spr.MUD_FLOOR tile.blocking = False tile.transparent = True # spawn treasures and creatures mr = level.get_main_region() treasures = random.sample(mr, difficulty) for loc in treasures: level.add_item(loc, Item(spr.GOLD_NUGGET, name="gold nugget", value=50+difficulty*25)) mobs = random.sample(mr, difficulty) for loc in mobs: c = Creature(level, *loc, hp=difficulty*10, maxhp=difficulty*10, name="malicious slime", gold=10+difficulty*5) c.min_atk = difficulty c.max_atk = difficulty*2 level.creatures.append(c) return level
def redraw(): display.clear((64,64,64)) # Draw the map display.fill_rect((0,0, 400,400), (0,0,0)) Player.cur_level.recalc_light() for x,y in box2d(-12, -12, 25, 25): # for each tile in the player's map range: ax, ay = x+Player.x, y+Player.y if Player.cur_level.in_bounds(ax, ay): cur_tile = Player.cur_level.tiles[ax][ay] seen = Player.can_see(ax, ay) if seen == 1: # Illuminated and in LoS cur_tile.memorized = True # draw the terrain display.draw_sprite(cur_tile.img, ((x+12)*16, (y+12)*16)) # draw each item, starting with the ones on the bottom of the pile for i in cur_tile.items: display.draw_sprite(i.img, ((x+12)*16, (y+12)*16)) elif seen == 2: # In night vision range cur_tile.memorized = True display.draw_sprite(cur_tile.img, ((x+12)*16, (y+12)*16), sheet=display.sprites_blue) # draw each item, starting with the ones on the bottom of the pile for i in cur_tile.items: display.draw_sprite(i.img, ((x+12)*16, (y+12)*16), sheet=display.sprites_blue) elif cur_tile.memorized: display.draw_sprite(cur_tile.img, ((x+12)*16, (y+12)*16), sheet=display.sprites_grey) for i in cur_tile.items: if not i.holdable: display.draw_sprite(i.img, ((x+12)*16, (y+12)*16), sheet=display.sprites_grey) # Draw creatures, including the player for c in Player.cur_level.creatures: sx, sy = c.x-Player.x+12, c.y-Player.y+12 if 0 <= sx < 25 and 0 <= sy < 25 and Player.can_see(c.x, c.y): display.draw_sprite(c.img, (sx*16, sy*16)) if c != Player: # health bar display.fill_rect((sx*16, sy*16, 16*(float(c.hp)/c.maxhp), 1), (255,0,0)) # Message log display.fill_rect((5,405, 790,190), (32,32,32)) display.msg_log.draw((10, 410), 160) # Player text entry if Player.entering_text: display.fill_rect((9,575, 782,16), (64,64,64)) display.draw_text(Player.entered_text, (12,576)) # Draw player's health bar display.fill_rect((400,0, 400,16), (128,0,0)) display.fill_rect((400,0, 400*(float(Player.hp)/Player.maxhp), 16), (255,0,0)) display.draw_text('HP: %d/%d' % (Player.hp, Player.maxhp), (405, 1)) # energy bar display.fill_rect((400,16, 400,16), (192,192,192)) display.fill_rect((400,16, 400*(float(Player.mp)/Player.maxmp), 16), (255,255,0)) display.draw_text('ENERGY: %d/%d' % (Player.mp, Player.maxmp), (405, 17), (0,0,0)) # Player's wealth and inventory display.draw_text('Gold: %d' % (Player.gold), (405, 40), (255,255,0)) display.draw_text('Inventory', (410, 156)) INV_X = 420 INV_Y = 190 INV_W = 10 INV_H = 10 # inventory slot boxes for i in xrange(INV_W*INV_H): x,y = i%INV_W, i//INV_W display.draw_rect((INV_X+x*21, INV_Y+y*21, 20, 20), (255,255,255)) # inventory axis labels (for EZ-dropping/selling) for i in xrange(INV_W): display.draw_text('%d' % i, (INV_X+i*21+6, INV_Y-15)) for i in xrange(INV_H): display.draw_text('%d' % i, (INV_X-9, INV_Y+i*21+3)) # actual items for i,item in enumerate(Player.inv): x,y = i%INV_W, i//INV_W display.draw_sprite(item.img, (INV_X+2+x*21, INV_Y+2+y*21)) # Equipment (garb) EQ_X = 640 EQ_Y = 200 display.draw_text('Your equipment:', (EQ_X, EQ_Y)) if Player.equipment: for i,item in enumerate(Player.equipment): EQ_Y += 16 if item.equip_slot: display.draw_text('%d: %s (%s)' % (i,item.name,item.equip_slot), (EQ_X, EQ_Y)) else: display.draw_text('%d: %s' % (i,item.name), (EQ_X, EQ_Y)) else: EQ_Y += 16 display.draw_text('None!', (EQ_X, EQ_Y)) EQ_Y += 16 # also write empty slots under garb for slot in Player.equip_slots.iterkeys(): fs = Player.equip_slots[slot] - Player.equipped[slot] if fs: EQ_Y += 16 if fs == 1: pl = '' else: pl = 's' display.draw_text('%d free slot%s (%s)' % (fs,pl,slot), (EQ_X, EQ_Y)) # Stats display.draw_text('Attack damage: %d-%d' % (Player.min_atk, Player.max_atk), (420, 60)) display.draw_text('Attack speed: %d' % (10000/Player.attack_time), (420, 76)) display.draw_text('Move speed: %d' % (10000/Player.move_time), (420, 92)) display.update()