class CombatExerciseAsteroidsGame(BasicGame): def __addAsteroid(self, force=False): logging.info("Add Asteroid (%s)", repr(force)) # add player bauble self.world.append(Asteroid(getPositionAwayFromOtherObjects(self.world, 80, 30, force))) logging.info("Done Adding Asteroid") def __addAsteroids(self, w, num, force=False): logging.info("Adding %d Asteroids (%s)", num, repr(force)) for i in xrange(num): w.append(Asteroid(getPositionAwayFromOtherObjects(w, 100, 30, force))) logging.info("Done Adding Asteroids") """ def player_died(self, player, gone): if player.object != None and \ player.object.killedby != None and \ player.object.killedby.owner != None and \ player.object.killedby.owner.player != None: self.player_update_score(player.object.killedby.owner.player, player.object.timealive / self.cfg.getint("CombatExercise", "points_time_alive_divide_by")) super(CombatExerciseAsteroidsGame, self).player_died(player, gone) """ def world_add_remove_object(self, wobj, added): logging.debug("CE Add Object(%s): #%d (%s)", repr(added), wobj.id, friendly_type(wobj)) if not added and isinstance(wobj, Asteroid) and hasattr(wobj, "killedby") and wobj.killedby != None: obj = wobj.killedby if isinstance(obj, Torpedo): self.player_update_score(obj.owner.player, self.cfg.getint("CombatExerciseAsteroids", "points_shoot_asteroid")) logging.info("Torpedo Owner (#%d) Destroyed Asteroid", obj.owner.id) """ elif isinstance(obj, Ship) and obj.health.value > 0: self.player_update_score(obj.player, self.__shippoints) logging.info("Ship (#%d) Destroyed Asteroid", obj.id) """ elif not added and isinstance(wobj, Ship) and hasattr(wobj, "killedby") and wobj.killedby != None: obj = wobj.killedby if isinstance(obj, Torpedo): self.player_update_score(obj.owner.player, wobj.timealive / self.cfg.getint("CombatExerciseAsteroids", "points_time_alive_divide_by")) logging.info("Torpedo Owner (#%d) Destroyed Ship (#%d)", obj.owner.id, wobj.id) elif isinstance(obj, Ship) and obj.health.value > 0: self.player_update_score(obj.player, wobj.timealive / self.cfg.getint("CombatExerciseAsteroids", "points_time_alive_divide_by")) logging.info("Ship (#%d) Destroyed Ship (#%d)", obj.id, wobj.id) super(CombatExerciseAsteroidsGame, self).world_add_remove_object(wobj, added) def round_over(self): self.__btimer.cancel() # prevent more asteroids from being spawned! super(CombatExerciseAsteroidsGame, self).round_over() def round_start(self): super(CombatExerciseAsteroidsGame, self).round_start() # start Bauble Spawn Timer self.newAsteroidTimer() def gui_draw_game_world_info(self, surface, flags): for player in self.game_get_current_player_list(): if player.object != None: # draw time alive by player text = self._dfont.render("%.1f" % player.object.timealive, False, player.color) surface.blit(text, (player.object.body.position[0]+30, player.object.body.position[1]-4)) def newAsteroidTimer(self): self.__btimer = RoundTimer(self.cfg.getint("CombatExerciseAsteroids", "spawn_time"), self.spawnAsteroid) self.__btimer.start() def spawnAsteroid(self): self.__addAsteroids(self.world, self.cfg.getint("CombatExerciseAsteroids", "spawn_number")) self.newAsteroidTimer()
class BaubleHuntGame(BasicGame): def __init__(self, cfgobj): bb = cfgobj.getfloat("BaubleHunt", "bauble_percent_blue") yb = cfgobj.getfloat("BaubleHunt", "bauble_percent_yellow") rb = cfgobj.getfloat("BaubleHunt", "bauble_percent_red") self._mv = cfgobj.getint("BaubleHunt", "bauble_points_red") self.__valuetable = [(bb, cfgobj.getint("BaubleHunt", "bauble_points_blue")), (bb+yb, cfgobj.getint("BaubleHunt", "bauble_points_yellow")), (bb+yb+rb, self._mv)] self.__bases = ThreadSafeDict() self.__baubles = ThreadSafeDict() super(BaubleHuntGame, self).__init__(cfgobj) self.__maxcarry = self.cfg.getint("BaubleHunt", "ship_cargo_size") def world_create(self, pys=True): w = ConfiguredWorld(self, self.cfg, pys) # Add some initial small Baubles self.__addBaubles(w, self.cfg.getint("BaubleHunt", "bauble_initial")) return w def game_get_info(self): return {"GAMENAME": "BaubleHunt"} def _world_reset(self): self.__bases = ThreadSafeDict() self.__baubles = ThreadSafeDict() super(BaubleHuntGame, self)._world_reset() def player_added(self, player, reason): super(BaubleHuntGame, self).player_added(player, reason) player.carrying = [] if reason == BasicGame._ADD_REASON_REGISTER_: player.totalcollected = 0 elif reason == BasicGame._ADD_REASON_START_: player.totalcollected = 0 self.__addHomeBase(player) self.__addBaubles(self.world, self.cfg.getint("BaubleHunt", "bauble_per_player")) elif self.__bases.has_key(player.netid): self.__bases[player.netid].newOwner(player.object) def __addHomeBase(self, player, force=False): logging.info("Add HomeBase (%s) for Player %d", repr(force), player.netid) # add player bauble b = HomeBase(self.getHomeBasePosition(), player.object) self.__bases[player.netid] = b self.world.append(b) logging.info("Done Adding HomeBase") # Ignores Baubles def getHomeBasePosition(self): pos = (0,0) x = 0 n = 1 while n > 0 and x < 15: x += 1 pos = (random.randint(100, self.world.width - 100), random.randint(100, self.world.height - 100)) objs = self.world.getObjectsInArea(pos, 200) for i in xrange(len(objs)-1, -1, -1): if isinstance(objs[i], Bauble): del objs[i] n = len(objs) return pos def __addBaubles(self, w, num, force=False): logging.info("Adding %d Baubles (%s)", num, repr(force)) for i in xrange(num): r = random.random() v = 0 for ent in self.__valuetable: if r < ent[0]: v = ent[1] break b = Bauble(getPositionAwayFromOtherObjects(w, 80, 30, force), v) if v == self._mv: self.__baubles[b.id] = b w.append(b) logging.info("Done Adding Baubles") def world_physics_pre_collision(self, shapes): types = [] objs = [] for shape in shapes: objs.append(self.world[shape.id]) types.append(friendly_type(objs[-1])) if "Ship" in types and "HomeBase" in types: ship = None myhome = None homes = [] for obj in objs: if isinstance(obj, HomeBase): homes.append(obj) elif isinstance(obj, Ship): ship = obj if ship != None: logging.info("Ship #%d hit bases %d owner id #%d", ship.id, len(homes), homes[0].owner.id) for h in homes: if ship.id == h.owner.id: myhome = h logging.info("Ship #%d hit their base", ship.id) break #eif #next else: logging.error("Ship not found after collision with Ship?") #eif if myhome != None: return [ False, [ [self.depositBaubles, ship, myhome] ] ] else: logging.info("Ship #%d hit some other base", ship.id) return [ False, [] ] if "Ship" in types and "Bauble" in types: b = [] ship = None for obj in objs: if isinstance(obj, Bauble): b.append(obj) elif isinstance(obj, Ship): ship = obj #eif #next return [ False, [ [self.collectBaubles, ship, b] ] ] elif "HomeBase" in types or "Bauble" in types: return [ False, [] ] return super(BaubleHuntGame, self).world_physics_pre_collision(shapes) def collectBaubles(self, ship, para): logging.info("Collected Baubles Ship #%d", ship.id) sound = True for bauble in para[0]: if len(ship.player.carrying) < self.__maxcarry: ship.player.carrying.append(bauble) if sound: sound = False ship.player.sound = "BAUBLE" if self.__baubles.has_key(bauble.id): del self.__baubles[bauble.id] self.world.remove(bauble) bauble.destroyed = True self.__addBaubles(self.world, 1, True) #eif logging.info("Done Collecting Baubles #%d", ship.id) def depositBaubles(self, ship, para): home = para[0] logging.info("Player Depositing Baubles #%d", ship.id) for b in ship.player.carrying: self.player_update_score(ship.player, b.value) home.stored += b.value ship.player.totalcollected += len(ship.player.carrying) if len(ship.player.carrying) > 0: ship.player.sound = "COLLECT" ship.player.carrying = [] def player_died(self, player, gone): # if ship destroyed, put baubles stored back for b in player.carrying: b.body.position = (player.object.body.position[0] + random.randint(-10, 10), player.object.body.position[1] + random.randint(-10, 10)) if b.value == self._mv: self.__baubles[b.id] = b self.world.append(b) self.world.causeExplosion(player.object.body.position, 32, 1000) # Remove player's base if they're gone if gone and self.__bases.has_key(player.netid): self.world.remove(self.__bases[player.netid]) del self.__bases[player.netid] return super(BaubleHuntGame, self).player_died(player, gone) def game_get_extra_environment(self, player): if player.netid in self.__bases: # Check if Player still around? v = 0 for b in player.carrying: v += b.value baubles = [] for b in self.__baubles: baubles.append(intpos(b.body.position)) env = super(BaubleHuntGame, self).game_get_extra_environment(player) env.update({"POSITION": intpos(self.__bases[player.netid].body.position), "BAUBLES": baubles, "STORED": len(player.carrying), "STOREDVALUE": v, "COLLECTED": player.totalcollected}) return env else: return {} def game_get_extra_radar_info(self, obj, objdata): """ Called by the World when the obj is being radared """ super(BaubleHuntGame, self).game_get_extra_radar_info(obj, objdata) if hasattr(obj, "player"): objdata["NUMSTORED"] = len(obj.player.carrying) def player_get_stat_string(self, player): return str(int(player.score)) + " in " + str(player.totalcollected) + " : " + player.name + " c.v. " + str(sum(b.value for b in player.carrying)) + " in " + str(len(player.carrying)) def round_over(self): logging.info("Round Over") self.__btimer.cancel() # prevent more baubles from being spawned! super(BaubleHuntGame, self).round_over() def gui_draw_game_world_info(self, surface, flags): for player in self.game_get_current_player_list(): if player.object != None: # draw number of objects carried text = debugfont().render(repr(len(player.carrying)), False, player.color) surface.blit(text, (player.object.body.position[0]+30, player.object.body.position[1]-4)) # draw line between player and HomeBase if flags["DEBUG"] and self.__bases.has_key(player.netid): pygame.draw.line(surface, player.color, intpos(player.object.body.position), intpos(self.__bases[player.netid].body.position)) # draw number of baubles carried by player def round_start(self): logging.info("Game Start") super(BaubleHuntGame, self).round_start() # start Bauble Spawn Timer self.newBaubleTimer() def newBaubleTimer(self): ntime = self.cfg.getint("BaubleHunt", "bauble_timer") if ntime > 0: self.__btimer = RoundTimer(ntime, self.spawnBauble) self.__btimer.start() def spawnBauble(self): logging.info("Spawn Bauble Timer [%d]", thread.get_ident()) self.__addBaubles(self.world, self.cfg.getint("BaubleHunt", "bauble_timer_spawns")) self.newBaubleTimer()
class AsteroidMinerGame(BasicGame): def __init__(self, cfgobj): super(AsteroidMinerGame, self).__init__(cfgobj) self.__torpedopoints = self.cfg.getint("AsteroidMiner", "points_torpedo") self.__shippoints = self.cfg.getint("AsteroidMiner", "points_ship") def player_added(self, player, reason): super(AsteroidMinerGame, self).player_added(player, reason) if reason == BasicGame._ADD_REASON_START_: self.__addAsteroid() def __addAsteroid(self, force=False): logging.info("Add Asteroid (%s)", repr(force)) # add player bauble self.world.append(Asteroid(getPositionAwayFromOtherObjects(self.world, 80, 30, force))) logging.info("Done Adding Asteroid") def __addAsteroids(self, w, num, force=False): logging.info("Adding %d Asteroids (%s)", num, repr(force)) for i in xrange(num): w.append(Asteroid(getPositionAwayFromOtherObjects(w, 100, 30, force))) logging.info("Done Adding Asteroids") """ def world_physics_pre_collision(self, shapes): types = [] objs = [] for shape in shapes: objs.append(self.world[shape.id]) types.append(friendly_type(objs[-1])) if "Asteroid" in types and "Torpedo" in types: b = [] ship = None for i in xrange(len(objs)-1, -1, -1): if isinstance(objs[i], Asteroid): b.append(objs[i]) del objs[i] elif isinstance(objs[i], Torpedo): ship = objs[i] return [ True, [ [self.checkAsteroid, ship, b] ] ] def checkAsteroid(self, torpedo, para): logging.info("checkAsteroid Torpedo #%d", torpedo.id) for ast in para[0]: if ast.destroyed: self.player_update_score(torpedo.owner.player, 1) # add new asteroid self.__addAsteroid(True) #eif logging.info("Done checkAsteroid #%d", torpedo.id) """ def world_add_remove_object(self, wobj, added): logging.debug("BH Add Object(%s): #%d (%s)", repr(added), wobj.id, friendly_type(wobj)) if not added and isinstance(wobj, Asteroid) and hasattr(wobj, "killedby") and wobj.killedby != None: obj = wobj.killedby if isinstance(obj, Torpedo): self.player_update_score(obj.owner.player, self.__torpedopoints) logging.info("Torpedo Owner (#%d) Destroyed Asteroid", obj.owner.id) elif isinstance(obj, Ship) and obj.health.value > 0: self.player_update_score(obj.player, self.__shippoints) logging.info("Ship (#%d) Destroyed Asteroid", obj.id) else: super(AsteroidMinerGame, self).world_add_remove_object(wobj, added) def round_over(self): self.__btimer.cancel() # prevent more asteroids from being spawned! super(AsteroidMinerGame, self).round_over() def round_start(self): super(AsteroidMinerGame, self).round_start() # start Bauble Spawn Timer self.newAsteroidTimer() def newAsteroidTimer(self): self.__btimer = RoundTimer(self.cfg.getint("AsteroidMiner", "spawn_time"), self.spawnAsteroid) self.__btimer.start() def spawnAsteroid(self): self.__addAsteroids(self.world, self.cfg.getint("AsteroidMiner", "spawn_number")) self.newAsteroidTimer()
class HungryHungryBaublesGame(BasicGame): def __init__(self, cfgobj): self.__points_blue = cfgobj.getint("HungryHungryBaubles", "bauble_points_blue") self.__points_gold = cfgobj.getint("HungryHungryBaubles", "bauble_points_gold") self.__points_extra = cfgobj.getint("HungryHungryBaubles", "bauble_points_extra") self.__baubles = {} super(HungryHungryBaublesGame, self).__init__(cfgobj) def world_create(self, pys=True): w = ConfiguredWorld(self, self.cfg, pys) # Add some initial small Baubles self.__addBaubles(w, self.cfg.getint("HungryHungryBaubles", "bauble_initial")) return w def game_get_info(self): return {"GAMENAME": "HungryHungryBaubles"} def player_added(self, player, reason): if reason == BasicGame._ADD_REASON_START_: self.__addBauble(player) self.__addBaubles(self.world, self.cfg.getint("HungryHungryBaubles", "bauble_per_player")) super(HungryHungryBaublesGame, self).player_added(player, reason) def __addBauble(self, player, force=False): logging.info("Add Bauble (%s) for Player %d", repr(force), player.netid) # add player bauble b = Bauble(getPositionAwayFromOtherObjects(self.world, 80, 30, force), self.__points_gold) self.__baubles[player.netid] = b self.world.append(b) logging.info("Done Adding Bauble") def __addBaubles(self, w, num, force=False): logging.info("Adding %d Baubles (%s)", num, repr(force)) for i in xrange(num): w.append(Bauble(getPositionAwayFromOtherObjects(w, 100, 30, force), self.__points_blue)) logging.info("Done Adding Baubles") def world_physics_pre_collision(self, shapes): types = [] objs = [] for shape in shapes: objs.append(self.world[shape.id]) types.append(friendly_type(objs[-1])) if "Ship" in types and "Bauble" in types: b = [] ship = None for i in xrange(len(objs) - 1, -1, -1): if isinstance(objs[i], Bauble): b.append(objs[i]) del objs[i] elif isinstance(objs[i], Ship): ship = objs[i] return [False, [[self.collectBaubles, ship, b]]] def collectBaubles(self, ship, para): logging.info("Collected Baubles Ship #%d", ship.id) for bauble in para[0]: # collect own Bauble? if bauble == self.__baubles[ship.player.netid]: logging.info("Collected Own Bauble #%d", ship.id) self.player_update_score(ship.player, self.__points_extra) # add new bauble self.__addBauble(ship.player, True) elif bauble in self.__baubles.values(): logging.info("Collected Gold Bauble #%d", ship.id) # someone else's bauble for key, value in self.__baubles.iteritems(): if self._players.has_key(key) and value == bauble: self.__addBauble(self._players[key], True) elif value == bauble: # Gold Bauble no longer owned, add back a regular one self.__addBaubles(self.world, 1, True) # eif else: logging.info("Collected Regular Bauble #%d", ship.id) self.__addBaubles(self.world, 1, True) # eif self.player_update_score(ship.player, bauble.value) bauble.destroyed = True self.world.remove(bauble) logging.info("Done Collecting Baubles #%d", ship.id) def game_get_extra_environment(self, player): env = super(HungryHungryBaublesGame, self).game_get_extra_environment(player) env["POSITION"] = intpos(self.__baubles[player.netid].body.position) return env def round_over(self): self.__btimer.cancel() # prevent more baubles from being spawned! super(HungryHungryBaublesGame, self).round_over() def gui_draw_game_world_info(self, surface, flags): for player in self.game_get_current_player_list(): if player.object != None and self.__baubles.has_key(player.netid): # draw line between player and Bauble pygame.draw.line( surface, player.color, intpos(player.object.body.position), intpos(self.__baubles[player.netid].body.position), ) def round_start(self): super(HungryHungryBaublesGame, self).round_start() # start Bauble Spawn Timer self.newBaubleTimer() def newBaubleTimer(self): self.__btimer = RoundTimer(self.cfg.getint("HungryHungryBaubles", "bauble_timer"), self.spawnBauble) self.__btimer.start() def spawnBauble(self): self.__addBaubles(self.world, self.cfg.getint("HungryHungryBaubles", "bauble_timer_spawns")) self.newBaubleTimer()