Python kivy 游戏:如何使矩形/椭圆与四边形碰撞

问题描述 投票:0回答:1

我正在创建一个游戏,但我无法使矩形与四边形碰撞。当矩形位于四边形的中间和四边形的右侧时,它会发生碰撞,但如果我将矩形移动到四边形的左侧,它就不会再发生碰撞。只要矩形的边缘超出或什至到达四边形的左侧,它就会停止碰撞。

rect = None
rect_coordinates = [(0, 0), (0, 0)]
nb_b = 100
brick = []
brick_coordinates = []

def __init__(self, **kwargs):
    super(MainWidget, self).__init__(**kwargs)
    self.init_vertical_line()
    self.init_horizontal_line()
    self.init_brick()
    self.init_ship()
    self.init_rect()
    self.generate_brick_coordinates()
    Clock.schedule_interval(self.update, 1/60)

def init_rect(self):
    with self.canvas:
        Color(0, 1, 0)
        self.rect = Rectangle()

def update_rect(self):
    pos_x = self.movement
    pos_y = self.height/1.923
    size_x = self.width/15
    size_y = self.height/25
    self.rect_coordinates[0] = (pos_x, pos_y)
    self.rect_coordinates[1] = (size_x, size_y)
    self.rect.pos = (self.rect_coordinates[0])
    self.rect.size = (self.rect_coordinates[1])

def collided(self):
    for i in range(0, len(self.brick_coordinates)):
        bi_x, bi_y = self.brick_coordinates[i]
        if bi_y < self.current_y_loop:
            return False
        if self.ball_collide_brick(bi_x, bi_y):
            return True
    return False

def ball_collide_brick(self, bi_x, bi_y):
    x_min, y_min = self.get_brick_coordinate(bi_x, bi_y)
    x_max, y_max = self.get_brick_coordinate(bi_x+1, bi_y+1)
    for i in range(0, 1):
        px, py = self.rect_coordinates[i]
        if x_min <= px <= x_max and y_min <= py <= y_max:
            return True
    return False

def init_brick(self):
    with self.canvas:
        Color(1, 1, 1)
        for i in range(0, self.nb_b):
            self.brick.append(Quad())

def get_line_x_from_index(self, index):
    central_x = self.width / 2
    spacing = self.v_line_space * self.width
    offset = index - 0.5
    line_x = central_x + offset * spacing
    return line_x

def get_line_y_from_index(self, index):
    spacing_y = self.h_line_space * self.height
    line_y = index * spacing_y - self.current_y_offset
    return line_y

def get_brick_coordinate(self, bi_x, bi_y):
    bi_y = bi_y - self.current_y_loop
    x = self.get_line_x_from_index(bi_x)
    y = self.get_line_y_from_index(bi_y)
    return x, y

def update_brick(self):
    for i in range(0, self.nb_b):
        bricks = self.brick[i]
        brick_c = self.brick_coordinates[i]
        x_min, y_min = self.get_brick_coordinate(brick_c[0], brick_c[1])
        x_max, y_max = self.get_brick_coordinate(brick_c[0] + 1, brick_c[1] + 1)
        x1, y1 = self.transform(x_min, y_min)
        x2, y2 = self.transform(x_min, y_max)
        x3, y3 = self.transform(x_max, y_max)
        x4, y4 = self.transform(x_max, y_min)
        bricks.points = [x1, y1, x2, y2, x3, y3, x4, y4]
python kivy game-physics game-development
1个回答
0
投票

当我开始编程时,我想制作游戏(就像我想的每个人一样),我从 python/kivy 开始,但随着时间的推移,我发现这不是最好的选择。这就是为什么我推荐你使用 unity 的 c#(或 c++,但它更难),特别是对于物理游戏开发(简单易学,有很多教程和比 kivy 更大的社区)。 抱歉,我无法给您任何更相关的答案,但我告诉您这一点是因为这是我开始时想知道的。

PS:我知道我不应该“根据观点发表言论;用参考资料或个人经验来支持它们”。但我认为这是最好的解决方案,因为你还没有任何答案。

© www.soinside.com 2019 - 2024. All rights reserved.