def gen_players(game): for ind, pl in enumerate(game.players): while True: place(pl) p = game.planets if any(dist(pl, game.players[i]) < pl.rad + game.players[i].rad for i in xrange(ind)): continue if all(dist(pl, p[i]) > pl.rad + p[i].rad + Const.PLANETS_GAP for i in xrange(len(p))): break
def is_a_planet_between(planets, obj1, obj2): x1, y1 = obj1.x, obj1.y x2, y2 = obj2.x, obj2.y c = dist(obj1, obj2) A, B = y2-y1, x1-x2 C = -x1 * A - y1 * B T = math.sqrt(A*A + B*B) good = False for p in planets: a = dist(obj1, p) b = dist(obj2, p) if a*a + c*c < b*b or b*b + c*c < a*a: continue d = math.fabs(A * p.x + B * p.y + C) / T if d < Const.CLOSE_DISTANCE + p.rad: good = True break return good
def gen_bonus(game): gen = True game.bonus = Bonus() while gen: gen = False place(game.bonus) for pl in game.players + game.planets: if dist(pl, game.bonus) < pl.rad + game.bonus.rad + 2: gen = True break
def gen_planets(game): planets_count = random.randint(4, 8) while True: game.planets = [Planet() for _ in xrange(planets_count)] p = game.planets if all(dist(p[i], p[j]) > p[i].rad + p[j].rad + Const.PLANETS_GAP for i in xrange(planets_count) for j in xrange(i)): break
def move_objects(self): """ Moves bonuses and bullets. Checks whether bullet has collided with some other object """ if self.bonus is not None: b = self.bonus b.dwh += b.delta if b.dwh == b.DELTA_MAX: b.delta = -b.DELTA if b.dwh == -b.DELTA_MAX: b.delta = b.DELTA if self.bullet is not None: self.bullet.ttl -= 1 if self.bullet.ttl < 0 or not self.bullet.is_visible_far(): self.bullet = None self.active_player ^= 1 return ndx, ndy = 0, 0 for planet in self.planets: d = dist(planet, self.bullet) angle_to_planet = math.atan2(planet.y - self.bullet.y, planet.x - self.bullet.x) d = d ** 1.8 ndx += math.cos(angle_to_planet) * planet.force / d ndy += math.sin(angle_to_planet) * planet.force / d self.bullet.turn(ndx/320, ndy/320) self.bullet.move() for planet in self.planets: d = dist(planet, self.bullet) # check if the bullet is near the planet if d < planet.rad: # if player has used BonusType.ENLARGE if self.bullet.bonustype == Bonus.BonusType.ENLARGE: planet.rad += planet.rad / 3 for player in self.players: if dist(planet, player) < planet.rad + player.rad: self.end_round(loser=player) return # score point # if player has used BonusType.SHRINK if self.bullet.bonustype == Bonus.BonusType.SHRINK: planet.rad -= planet.rad / 3 # recalc planet force just in case planet.force = planet.rad ** 2 * planet.density # kill bullet and change player self.bullet = None self.active_player ^= 1 return for player in self.players: gap = 0 if self.bullet.bonustype == Bonus.BonusType.ELECTRO and\ player != self.bullet.owner: gap = 30 if dist(player, self.bullet) < Player.PLAYER_RAD + gap: self.end_round(loser=player) return if self.bonus is not None: d = dist(self.bonus, self.bullet) if d <= self.bonus.rad + 1: self.players[self.active_player].bonustype = self.bonus.type self.bonus = None self.bullet = None self.active_player ^= 1 return