Beispiel #1
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())
Beispiel #2
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)
Beispiel #3
0
 def move_to_point(self, point: Vector):
     v = randint(1, 10) / 10
     self.move(point.sub(self.pos).norm().mul(Vector(v, v)))
Beispiel #4
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)
Beispiel #5
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))
Beispiel #6
0
 def direct_to_point(self, point: Vector):
     m = randint(20, 60) / 10
     self.v = Vector(m, m).mul(point.sub(self.pos).norm())