def spawn_player(self, point): logger.info("Spawning player...") self.player = Mage( self, window=self.window, x=point[0], y=point[1] ) # Player body for physics inertia = pymunk.moment_for_circle(10, 0, 12, (0, 0)) body = pymunk.Body(10, inertia) body.position = (self.player.x, self.player.y) shape = pymunk.Circle(body, 12, (0, 0)) shape.elasticity = 0.2 # shape.collision_type = 1 shape.group = 0 self.space.add(body, shape) self.player.body = body self.window.set_offset( self.player.windowpos[0] - self.player.x, self.player.windowpos[1] - self.player.y, ) self.window.update_display_dungeon() logger.info("Done.")
class Game: def __init__(self, window): self.window = window self.cfg = load_cfg("Game") def newgame(self, level): logger.info("Starting new game...") self.window.flush() # for i in range(100): # print(str(i) + ": " + self.generate_name("surname")) dungeon_cfg = load_cfg("Dungeon2") self.dungeon = DungeonGenerator( logger, dungeon_cfg ) self.game_size = ( dungeon_cfg["dungeon_size"][0] * 32, dungeon_cfg["dungeon_size"][1] * 32 ) self.space = pymunk.Space() self.dungeon.generate() self.spatial_hash = spatial.SpatialHash() for w in self.dungeon.walls: self.spatial_hash.insert_object_for_rect( w.p1, w.p2, w ) for c in self.dungeon.collidable: self.spatial_hash.insert_object_for_rect( c.p1, c.p2, c ) logger.info("Generating dungeon graphics...") img_grid = self.dungeon.get_tilemap() grid_to_image( img_grid, "dungeon", self.game_size ) dungeon_texture = self.window.load_single_texture( "dungeon" ) dungeon_overlay_texture = self.window.load_single_texture( "dungeon_overlay" ) self.dungeon_sprite = pyglet.sprite.Sprite( dungeon_texture, x=0, y=0, batch=self.window.batches["dungeon"] ) self.dungeon_overlay_sprite = pyglet.sprite.Sprite( dungeon_overlay_texture, x=0, y=0, batch=self.window.batches["dungeon_overlay"] ) logger.info("Done.") start = self.window.grid_to_window(*self.dungeon.startroom.center) # self.tiles = tiles.TiledRenderer(self.window, level) self.spawn_player(start) # Add a static square for every dungeon wall for w in self.dungeon.walls: body = pymunk.Body() body.position = w.center box_points = [(-16, -16), (-16, 16), (16, 16), (16, -16)] shape = pymunk.Poly(body, box_points, (0, 0)) shape.group = 1 self.space.add(shape) w.body = body for c in self.dungeon.collidable: body = pymunk.Body() body.position = c.center box_points = [(-16, -16), (-16, 16), (16, 16), (16, -16)] shape = pymunk.Poly(body, box_points, (0, 0)) shape.group = 1 self.space.add(shape) c.body = body # self.space.add_collision_handler(1, 1, post_solve=smack) self.space.damping = 0.001 itemlist = items.L1_Sword(), items.L1_Armor(), items.L1_Ring() for i in itemlist: self.player.equip_item(i) self.player.update_stats() self.player.restore_stats() logger.info("Spawning enemies...") self.enemies = [] for r in self.dungeon.enemy_rooms: for spawn in r.spawn_locations: x, y = self.window.grid_to_window(*spawn) self.add_enemy(x, y) i = 0 for e in self.enemies: e.modifiers["str"] += i e.update_stats() e.reset() i += 3 logger.info("Done.") if self.window.ui: self.window.ui.add_stats( self.player, 0, self.window.height, 120, 240 ) logger.info("Game successfully loaded.") def add_enemy(self, x, y): inertia = pymunk.moment_for_circle(20, 0, 12, (0, 0)) body = pymunk.Body(20, inertia) body.position = (x, y) shape = pymunk.Circle(body, 12, (0, 0)) shape.group = 0 shape.elasticity = 0.2 self.space.add(body, shape) e = Enemy(self, window=self.window, x=x, y=y) e.name = self.generate_name("surname") e.body = body self.enemies.append(e) def spawn_player(self, point): logger.info("Spawning player...") self.player = Mage( self, window=self.window, x=point[0], y=point[1] ) # Player body for physics inertia = pymunk.moment_for_circle(10, 0, 12, (0, 0)) body = pymunk.Body(10, inertia) body.position = (self.player.x, self.player.y) shape = pymunk.Circle(body, 12, (0, 0)) shape.elasticity = 0.2 # shape.collision_type = 1 shape.group = 0 self.space.add(body, shape) self.player.body = body self.window.set_offset( self.player.windowpos[0] - self.player.x, self.player.windowpos[1] - self.player.y, ) self.window.update_display_dungeon() logger.info("Done.") def generate_name(self, type): syllables = load_cfg("Syllables")[type] syllable_count = random.randrange(2, 4) surname, title_pre, title_post, i = "", "", "", 0 apos_used = False while i < syllable_count: syl = random.choice(syllables) if i and not random.randint(0, 10) and not apos_used: surname += "'" apos_used = True # if i and not random.randint(0, 40): # i -= 1 if syl not in surname: surname += syl i += 1 if not random.randint(0, 4): tp = load_cfg("Syllables")["title_pre"] title_pre = random.choice(tp) tp = load_cfg("Syllables")["title_post"] title_post = random.choice(tp) if random.randint(0, 3): fullname = "{0} the {1}{2}".format( surname.title(), title_pre.title(), title_post, ) else: fullname = "{0} {1}{2}".format( surname.title(), title_pre.title(), title_post, ) elif not random.randint(0, 6): if random.randint(0, 1): tp = load_cfg("Syllables")["title_pre"] else: tp = load_cfg("Syllables")["title_post"] title_pre = random.choice(tp) if title_pre[0] == "-": title_pre = title_pre[1:] fullname = "{0} the {1}".format( surname.title(), title_pre.title(), ) else: fullname = surname.title() if check_consecutive_letters(fullname, 3): # self.generate_name("surname") return "ERRORNAME LOL" else: return fullname def check_enemy_collision(self, x, y): for e in self.enemies: if check_point_rectangle(x, y, e.rectangle): return True else: return False def update_z_index(self): pass # for e in self.enemies: # if e.y > self.player.y: # e.sprite.group = self.window.bg_group # else: # e.sprite.group = self.window.fg_group def update(self, dt): for x in range(10): self.space.step(dt / 10) if self.player: self.player.update(dt) if len(self.enemies) > 0: self.update_z_index() for e in self.enemies: e.update(dt)