Exemple #1
0
 def __init__(self, canvas, field: Field):
     self.field = field
     self.canvas = canvas
     self.pos = Vector(0, 0)
     self.old_pos = Vector(0, 0)
     self.v = Vector(0, 0)
     self.shape = None
Exemple #2
0
 def __init__(self, canvas, team):
     self.canvas = canvas
     self.team = team
     self.pos = Vector(0, 0)
     self.old_pos = Vector(0, 0)
     self.shape = None
     self.miss_ball = False
Exemple #3
0
def inverse(A):
    if not A or len(A) != len(A[0]):
        raise Exception("Invalid input")
    length = len(A)
    res = append(A, eye(length))
    for i in range(length):
        res[i] = Vector.multiply_integer(res[i], 1 / res[i][i])
        for j in range(length):
            if i == j:
                continue
            res[j] = Vector.minus(res[j], Vector.multiply_integer(res[i], res[j][i]))
    for i in range(length):
        res[i] = res[i][length:]
    return res
Exemple #4
0
def determinant(A):
    if not A or len(A) != len(A[0]):
        raise Exception("Invalid input")
    new_A = [[0] * len(A[0]) for _ in range(len(A))]
    for i in range(len(A)):
        for j in range(len(A)):
            new_A[i][j] = A[i][j]
    for i in range(len(A)):
        for j in range(i + 1, len(A)):
            new_A[j] = Vector.minus(new_A[j], Vector.multiply_integer(new_A[i], new_A[j][i] / new_A[i][i]))
    res = 1
    for i in range(len(A)):
        res *= new_A[i][i]
    return res
Exemple #5
0
 def set_rand_speed(self, team: int):
     if team == 1:
         vx = randint(40, 60) / 10
         if randint(0, 1) == 0:
             vy = randint(1, 10) / 5
         else:
             vy = -randint(1, 10) / 5
         self.v = Vector(vx, vy)
     elif team == 2:
         vx = -randint(40, 60) / 10
         if randint(0, 1) == 0:
             vy = randint(1, 10) / 5
         else:
             vy = -randint(1, 10) / 5
         self.v = Vector(vx, vy)
def get_covariance_matrix(data):
    avg=get_average(data)
    avg=[n[0] for n in avg]
    centralized_matrix=[0]*len(data)  
    for i,item in enumerate(data):
        centralized_matrix[i]=Vector.minus(item,avg)
    res=Matrix.multiply(Matrix.transpose(centralized_matrix),centralized_matrix)
    N=len(data)-1
    return Matrix.multiply_integer(res, 1/N)
    
    
Exemple #7
0
 def shoot(self, num_goal):
     y_min = self.field.center_y - Field.BOX
     y_max = self.field.center_y + Field.BOX
     y = y_min + randint(0, y_max - y_min)
     if num_goal == 1:
         point = Vector(self.field.start_x, y)
     else:
         point = Vector(self.field.end_x, y)
     m = randint(40, 80) / 10
     self.v = Vector(m, m).mul(point.sub(self.pos).norm())
Exemple #8
0
 def set_on_center(self, field: Field):
     if self.team == 1:
         self.set(Vector(field.start_x + Field.BOX / 3, field.center_y))
     elif self.team == 2:
         self.set(Vector(field.end_x - Field.BOX / 3, field.center_y))
Exemple #9
0
 def move_to_ball_y(self, ball: Ball, field: Field):
     if field.goal_start_y <= ball.pos.y <= field.goal_end_y:
         y = field.center_y + (ball.pos.y - field.center_y) / 2
         self.set(Vector(self.pos.x, y))
Exemple #10
0
 def center(self):
     return Vector(self.center_x, self.center_y)
Exemple #11
0
 def right(self):
     return self.pos.add(Vector(Goalkeeper.RADIUS, 0))
Exemple #12
0
 def is_goal1(self) -> bool:
     return self.field.goal1_rectangle.hit(
         self.pos.sub(Vector(Ball.RADIUS, 0)))
Exemple #13
0
class Player:
    RADIUS = 10

    COLOR1 = "#80d6ff"
    COLOR2 = "#ff867c"
    OUTLINE = "#000000"

    @property
    def right(self):
        return self.pos.sub(Vector(Player.RADIUS, 0))

    @property
    def left(self):
        return self.pos.add(Vector(Player.RADIUS, 0))

    @property
    def color(self):
        if self.team == 1:
            return Player.COLOR1
        elif self.team == 2:
            return Player.COLOR2

    def __init__(self, canvas, team):
        self.canvas = canvas
        self.team = team
        self.pos = Vector(0, 0)
        self.old_pos = Vector(0, 0)
        self.shape = None

    def set(self, v):
        self.old_pos = self.pos
        self.pos = v
        self.paint()

    def move(self, v: Vector):
        self.set(self.pos.add(v))

    def move_to_point(self, point: Vector):
        v = randint(1, 10) / 10
        self.move(point.sub(self.pos).norm().mul(Vector(v, v)))

    def get_ball(self, ball):
        if self.team == 1:
            ball.set(self.right)
        elif self.team == 2:
            ball.set(self.left)

    def paint(self):
        if self.shape is None:
            self.shape = self.canvas.create_rectangle(-Player.RADIUS,
                                                      -Player.RADIUS,
                                                      Player.RADIUS,
                                                      Player.RADIUS,
                                                      outline=Player.OUTLINE,
                                                      fill=self.color)
        delta = self.pos.sub(self.old_pos)
        self.canvas.move(self.shape, delta.x, delta.y)

    def rectangle(self) -> Rectangle:
        return self.pos.rect(Player.RADIUS)

    def ball_hit_test(self, ball: Ball) -> bool:
        return self.rectangle().hit(ball.pos)
Exemple #14
0
 def left(self):
     return self.pos.add(Vector(Player.RADIUS, 0))
Exemple #15
0
 def start_xy(self):
     return Vector(self.start_x, self.start_y)
Exemple #16
0
 def direct_to_point(self, point: Vector):
     m = randint(20, 60) / 10
     self.v = Vector(m, m).mul(point.sub(self.pos).norm())
Exemple #17
0
 def end_xy(self):
     return Vector(self.end_x, self.end_y)
Exemple #18
0
 def set(self, new_pos: Vector):
     self.old_pos = self.pos
     self.pos = new_pos
     self.v = Vector(0, 0)
     self.paint()
Exemple #19
0
 def set_in_goal2(self):
     self.set(Vector(self.field.end_x + 2 * Ball.RADIUS, self.pos.y))
Exemple #20
0
 def set_in_goal1(self):
     self.set(Vector(self.field.start_x - 2 * Ball.RADIUS, self.pos.y))
Exemple #21
0
class Goalkeeper:
    RADIUS = 10
    COLOR1 = "#0077c2"
    COLOR2 = "#b61827"

    @property
    def left(self):
        return self.pos.sub(Vector(Goalkeeper.RADIUS, 0))

    @property
    def right(self):
        return self.pos.add(Vector(Goalkeeper.RADIUS, 0))

    @property
    def color(self):
        if self.team == 1:
            return Goalkeeper.COLOR1
        elif self.team == 2:
            return Goalkeeper.COLOR2

    def __init__(self, canvas, team):
        self.canvas = canvas
        self.team = team
        self.pos = Vector(0, 0)
        self.old_pos = Vector(0, 0)
        self.shape = None
        self.miss_ball = False

    def set(self, new_pos):
        self.old_pos = self.pos
        self.pos = new_pos
        self.paint()

    def get_ball(self, ball, team):
        if team == 1:
            ball.set(self.right)
        elif team == 2:
            ball.set(self.left)

    def move_to_ball_y(self, ball: Ball, field: Field):
        if field.goal_start_y <= ball.pos.y <= field.goal_end_y:
            y = field.center_y + (ball.pos.y - field.center_y) / 2
            self.set(Vector(self.pos.x, y))

    def set_on_center(self, field: Field):
        if self.team == 1:
            self.set(Vector(field.start_x + Field.BOX / 3, field.center_y))
        elif self.team == 2:
            self.set(Vector(field.end_x - Field.BOX / 3, field.center_y))

    def ball_hit_test(self, ball: Ball) -> bool:
        if not self.miss_ball and self.rectangle().hit(ball.pos):
            if randint(0, 5) == 3:
                self.miss_ball = True
                return False
            else:
                ball.rebound_x()
                return True
        else:
            return False

    def paint(self):
        if self.shape is None:
            self.shape = self.canvas.create_rectangle(-Goalkeeper.RADIUS,
                                                      -Goalkeeper.RADIUS,
                                                      Goalkeeper.RADIUS,
                                                      Goalkeeper.RADIUS,
                                                      outline='black',
                                                      fill=self.color)
        delta = self.pos.sub(self.old_pos)
        self.canvas.move(self.shape, delta.x, delta.y)

    def rectangle(self) -> Rectangle:
        return self.pos.rect(Goalkeeper.RADIUS)
Exemple #22
0
 def right(self):
     return self.pos.sub(Vector(Player.RADIUS, 0))
Exemple #23
0
 def goal2(self):
     return Vector(self.end_x, self.center_y)
Exemple #24
0
 def move_to_point(self, point: Vector):
     v = randint(1, 10) / 10
     self.move(point.sub(self.pos).norm().mul(Vector(v, v)))
Exemple #25
0
 def stop(self):
     self.v = Vector(0, 0)
Exemple #26
0
 def left(self):
     return self.pos.sub(Vector(Goalkeeper.RADIUS, 0))
Exemple #27
0
 def is_goal2(self) -> bool:
     return self.field.goal2_rectangle.hit(
         self.pos.add(Vector(Ball.RADIUS, 0)))
Exemple #28
0
class Ball:
    RADIUS = 5
    COLOR = "#ffff72"
    OUTLINE = "#c41c00"

    def __init__(self, canvas, field: Field):
        self.field = field
        self.canvas = canvas
        self.pos = Vector(0, 0)
        self.old_pos = Vector(0, 0)
        self.v = Vector(0, 0)
        self.shape = None

    def move(self):
        self.old_pos = self.pos
        self.pos = self.pos.add(self.v)
        self.paint()

    def set(self, new_pos: Vector):
        self.old_pos = self.pos
        self.pos = new_pos
        self.v = Vector(0, 0)
        self.paint()

    def set_on_center(self):
        self.set(self.field.center)

    def set_rand_speed(self, team: int):
        if team == 1:
            vx = randint(40, 60) / 10
            if randint(0, 1) == 0:
                vy = randint(1, 10) / 5
            else:
                vy = -randint(1, 10) / 5
            self.v = Vector(vx, vy)
        elif team == 2:
            vx = -randint(40, 60) / 10
            if randint(0, 1) == 0:
                vy = randint(1, 10) / 5
            else:
                vy = -randint(1, 10) / 5
            self.v = Vector(vx, vy)

    def direct_to_point(self, point: Vector):
        m = randint(20, 60) / 10
        self.v = Vector(m, m).mul(point.sub(self.pos).norm())

    def shoot(self, num_goal):
        y_min = self.field.center_y - Field.BOX
        y_max = self.field.center_y + Field.BOX
        y = y_min + randint(0, y_max - y_min)
        if num_goal == 1:
            point = Vector(self.field.start_x, y)
        else:
            point = Vector(self.field.end_x, y)
        m = randint(40, 80) / 10
        self.v = Vector(m, m).mul(point.sub(self.pos).norm())

    def stop(self):
        self.v = Vector(0, 0)

    def rebound_x(self):
        self.v.x = -self.v.x

    def rebound_y(self):
        self.v.y = -self.v.y

    def paint(self):
        if self.shape is None:
            self.shape = self.canvas.create_oval(-Ball.RADIUS,
                                                 -Ball.RADIUS,
                                                 Ball.RADIUS,
                                                 Ball.RADIUS,
                                                 outline=Ball.OUTLINE,
                                                 fill=Ball.COLOR)
        delta = self.pos.sub(self.old_pos)
        self.canvas.move(self.shape, delta.x, delta.y)

    def field_hit_test(self):
        if self.pos.y <= (self.field.start_y + Ball.RADIUS):
            self.rebound_y()
        if self.pos.y >= (self.field.end_y - Ball.RADIUS):
            self.rebound_y()
        # if self.pos.x <= (self.field.start_x + Ball.RADIUS):
        #    self.rebound_x()
        # if self.pos.x >= (self.field.end_x - Ball.RADIUS):
        #    self.rebound_x()

    def is_out1(self) -> bool:
        return self.pos.x <= (self.field.start_x + Ball.RADIUS)

    def is_out2(self) -> bool:
        return self.pos.x >= (self.field.end_x - Ball.RADIUS)

    def is_goal1(self) -> bool:
        return self.field.goal1_rectangle.hit(
            self.pos.sub(Vector(Ball.RADIUS, 0)))

    def is_goal2(self) -> bool:
        return self.field.goal2_rectangle.hit(
            self.pos.add(Vector(Ball.RADIUS, 0)))

    def dist2_to_goal(self, goal_num: int):
        if goal_num == 1:
            return self.pos.dist2(self.field.goal1)
        else:
            return self.pos.dist2(self.field.goal2)

    def set_in_goal1(self):
        self.set(Vector(self.field.start_x - 2 * Ball.RADIUS, self.pos.y))

    def set_in_goal2(self):
        self.set(Vector(self.field.end_x + 2 * Ball.RADIUS, self.pos.y))
Exemple #29
0
 def goal1(self):
     return Vector(self.start_x, self.center_y)