def next(self, ball, action=None): """ Calcul le prochain etat en fonction de l'action et de la position de la balle :param ball: :param action: :return: Action shoot effectue """ if not (hasattr(action, "acceleration") and hasattr(action, "shoot")): action = SoccerAction() #print("Warning : mauvais SoccerAction") self._action = action.copy() self._state.vitesse *= (1 - settings.playerBrackConstant) self._state.vitesse = (self._state.vitesse + self.acceleration).norm_max( settings.maxPlayerSpeed) self._state.position += self.vitesse if self._state.position.x < 0 or self.position.x > settings.GAME_WIDTH \ or self.position.y < 0 or self.position.y > settings.GAME_HEIGHT: self._state.position.x = max( 0, min(settings.GAME_WIDTH, self.position.x)) self._state.position.y = max( 0, min(settings.GAME_HEIGHT, self.position.y)) self._state.vitesse.norm = 0 if self._action.shoot.norm == 0 or not self.can_shoot(): self._dec_shoot() return Vector2D() self._reset_shoot() if self._state.position.distance(ball.position) > ( settings.PLAYER_RADIUS + settings.BALL_RADIUS): return Vector2D() return self._rd_angle( self.shoot, (self.vitesse.angle - self.shoot.angle), self.position.distance(ball.position) / (settings.PLAYER_RADIUS + settings.BALL_RADIUS))
def _rd_angle(shoot,dangle,dist): return shoot eliss = lambda x, alpha: (math.exp(alpha*x)-1)/(math.exp(alpha)-1) dangle = abs((dangle+math.pi*2) %(math.pi*2) -math.pi) dangle_factor = eliss(1.-max(dangle-math.pi/2,0)/(math.pi/2.),10) norm_factor = 1-eliss(shoot.norm/settings.maxPlayerShoot,3) dist_factor = 1-eliss(dist,2) angle_prc = dangle_factor*norm_factor*dist_factor return Vector2D(norm =shoot.norm,angle = shoot.angle +2*(random.random()-0.5)*angle_prc*settings.shootRandomAngle*math.pi/2.) norm_prc = (1-dangle_factor)*(1-dist_factor)*0.3 return Vector2D(norm=shoot.norm*(1-norm_prc), angle=shoot.angle+2*(random.random()-0.5) *angle_prc*settings.shootRandomAngle*math.pi/2.)
def __init__(self): self.sprites = dict() self.sprites["team1"] = TextSprite(color=HUD_TEAM1_COLOR, scale=0.07, position=Vector2D(0, settings.GAME_HEIGHT + 6)) self.sprites["team2"] = TextSprite(color=HUD_TEAM2_COLOR, scale=0.07, position=Vector2D(0, settings.GAME_HEIGHT + 2)) self.sprites["ongoing"] = TextSprite(position=Vector2D(settings.GAME_WIDTH - 50, settings.GAME_HEIGHT + 7), color=HUD_TEXT_COLOR, scale=0.05) self.sprites["ibattle"] = TextSprite(position=Vector2D(settings.GAME_WIDTH - 50, settings.GAME_HEIGHT + 4), color=HUD_TEXT_COLOR, scale=0.05) self.sprites["itour"] = TextSprite(position=Vector2D(settings.GAME_WIDTH - 50, settings.GAME_HEIGHT + 1), color=HUD_TEXT_COLOR, scale=0.05)
def next(self,sum_of_shoots): self.vitesse.norm = self.vitesse.norm - settings.ballBrakeSquare * self.vitesse.norm ** 2 - settings.ballBrakeConstant * self.vitesse.norm ## decomposition selon le vecteur unitaire de ball.speed snorm = sum_of_shoots.norm if snorm > 0: u_s = sum_of_shoots.copy() u_s.normalize() u_t = Vector2D(-u_s.y, u_s.x) speed_abs = abs(self.vitesse.dot(u_s)) speed_ortho = self.vitesse.dot(u_t) speed = Vector2D(speed_abs * u_s.x - speed_ortho * u_s.y, speed_abs * u_s.y + speed_ortho * u_s.x) speed += sum_of_shoots self.vitesse = speed self.vitesse = self.vitesse.norm_max(settings.maxBallAcceleration) self.position += self.vitesse
def from_str(cls, strg): l_vect = Vector2D.from_list_str(strg) if len(l_vect) != 4: raise DecodeException("Wrong format for %s : %s" % (cls, strg)) return cls(state=MobileMixin(position=l_vect[0], vitesse=l_vect[1]), action=SoccerAction(acceleration=l_vect[2], shoot=l_vect[3]))
def get_welcome(self): gw = settings.GAME_WIDTH * 0.35 gh = settings.GAME_HEIGHT * 0.8 res = [] for x in self.list_msg: res.append(TextSprite(x, color=MSG_TEXT_COLOR, scale=0.08, position=Vector2D(gw, gh))) gh -= 5 return res
def __init__(self, text="", position=None, color=None, scale=0.1): if not position: position = Vector2D() if not color: color = [255, 255, 255, 255] try: self._label = pyglet.text.Label(text, color=color, font_name="Arial", font_size=40) except Exception, e: print(e, traceback.print_exc())
def from_list(self, l): self._is_ready = False delta = -PANEL_DELTA self.sprites = [] for i, s in enumerate(l): t1 = TextSprite("%d - %s" % (i + 1, s[1]), color=PANEL_TXT_COLOR, scale=self.scale, position=Vector2D( settings.GAME_WIDTH, settings.GAME_HEIGHT + HUD_HEIGHT + delta)) t2 = TextSprite(s[2], color=PANEL_SCORE_COLOR, scale=self.scale, position=Vector2D( settings.GAME_WIDTH + PANEL_WIDTH * 1 / 4., settings.GAME_HEIGHT + HUD_HEIGHT + delta - PANEL_DELTA / 2.)) self.sprites.append([t1, t2]) delta -= PANEL_DELTA self._is_ready = True
def _rd_angle(shoot, dangle, dist): eliss = lambda x, alpha: (math.exp(alpha * x) - 1) / (math.exp(alpha) - 1) dangle = abs((dangle + math.pi * 2) % (math.pi * 2) - math.pi) dangle_factor = eliss( 1. - max(dangle - math.pi / 2, 0) / (math.pi / 2.), 5) norm_factor = eliss(shoot.norm / settings.maxPlayerShoot, 4) dist_factor = eliss(dist, 10) angle_prc = (1 - (1. - dangle_factor) * (1. - norm_factor) * (1. - 0.5 * dist_factor) ) * settings.shootRandomAngle * math.pi / 2. norm_prc = 1 - 0.3 * dist_factor * dangle_factor return Vector2D(norm=shoot.norm * norm_prc, angle=shoot.angle + 2 * (random.random() - 0.5) * angle_prc)
def apply_actions(self, actions=None): sum_of_shoots = Vector2D() if actions: for k, c in self._configs.items(): if k in actions: sum_of_shoots += c.next(self.ball, actions[k]) self.ball.next(sum_of_shoots) self.step += 1 if self._is_ball_inside_goal(): self._do_win(2 if self.ball.position.x <= 0 else 1) return if self.ball.position.x < 0: self.ball.position.x = -self.ball.position.x self.ball.vitesse.x = -self.ball.vitesse.x if self.ball.position.y < 0: self.ball.position.y = -self.ball.position.y self.ball.vitesse.y = -self.ball.vitesse.y if self.ball.position.x > settings.GAME_WIDTH: self.ball.position.x = 2 * settings.GAME_WIDTH - self.ball.position.x self.ball.vitesse.x = -self.ball.vitesse.x if self.ball.position.y > settings.GAME_HEIGHT: self.ball.position.y = 2 * settings.GAME_HEIGHT - self.ball.position.y self.ball.vitesse.y = -self.ball.vitesse.y
def from_str(cls, strg): tmp = Vector2D.from_list_str(strg) return cls(tmp[0], tmp[1])
def start(self): return self._start @property def end(self): return self._end vents: list[Vent] = [] for line in input_list: coordinates = line.split(' -> ') p1 = list(map(int, coordinates[0].split(','))) p2 = list(map(int, coordinates[1].split(','))) start = Vector2D(p1[0], p1[1]) end = Vector2D(p2[0], p2[1]) vents.append(Vent(start, end)) def get_overlaps(vents: list[Vent]): mapping = defaultdict(int) for v in vents: heading = v.start.unit_vector(v.end) pt = v.start while pt != v.end: mapping[pt] += 1 pt += heading # Include end point
def __sub__(self, other): return Vector2D(self.acceleration - other.acceleration, self.shoot - other.shoot)
def from_position(cls, x, y): return cls(state=MobileMixin(position=Vector2D(x, y)))
def __init__(self, acceleration=Vector2D(), shoot=Vector2D()): self.acceleration = acceleration self.shoot = shoot