我正在尝试为pygame游戏中的每个对象绘制一个Hitbox,但该Hitbox不显示。

这是我在其中定义打击盒和敌人其他方面的课程。

class Enemy:
    def __init__(self, y, width, height):
        self.width = width
        self.height = height
        self.vel = 1.5
        self.y = y
        self.x = random.randrange(screen_width - 64 * 2)
        self.index = random.choice(number)
        self.hitboxes = [(self.x + 68, self.y + 68, self.width - 10, self.height - 14),
                         (self.x + 38, self.y + 47, self.width + 20, self.height - 5),
                         (self.x + 18, self.y + 12, self.width + 32, self.height + 30),
                         (self.x + 20, self.y + 32, self.width + 16, self.height + 5),
                         (self.x + 4, self.y + 7, self.width - 24, self.height - 31)]  # hitbox list
        self.hitbox = self.hitboxes[self.index]  # selecting hitbox from list

    def draw(self, win):
        win.blit(asteroids[self.index], (self.x, self.y))
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)  # trying to draw the hitbox


这是re_draw函数

def re_draw():
    win.fill((0, 0, 0))
    [...]
    for a in asteroids_on_screen:
        a.draw(win)  # this is where I draw the object
    [...]
    pygame.display.update()


这是主循环以及与要在其上显示击中框的敌人/对象相关联的变量。

asteroids = [pygame.image.load('rock0.png'), pygame.image.load('rock1.png'), pygame.image.load('rock2.png'),
             pygame.image.load('rock3.png'), pygame.image.load('rock4.png')]

number = [0, 1, 2, 3, 4]

asteroids_on_screen = []

rock = Enemy(-140, 64, 64)

[...]

run = True
while run:
    last = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == my_event_id:
            x = random.randrange(screen_width - 64 * 2)
            index = random.choice(number)
            asteroids_on_screen.append(Enemy(rock.y, rock.width, rock.height))

    for a in asteroids_on_screen:
        if -141 < a.y < 500:
            a.y += a.vel
        else:
            asteroids_on_screen.pop(asteroids_on_screen.index(a))

        [...]

最佳答案

Hitbox不会随小行星“移动”。如果要使这种方法可行,则必须在更改小行星的位置时更改命中框的y坐标。例如。:

for a in asteroids_on_screen:
    if -141 < a.y < 500:
        a.y += a.vel
        a.hitbox = (a.hitbox[0], a.hitbox[1]+a.vel, a.hitbox[2], a.hitbox[3])
    else:
        asteroids_on_screen.pop(asteroids_on_screen.index(a))




但是我建议计算draw中的点击框。例如创建一个property currenthitbox,它返回当前的hitbox:

class Enemy:
    def __init__(self, y, width, height):
        self.width = width
        self.height = height
        self.vel = 1.5
        self.y = y
        self.x = random.randrange(screen_width - 64 * 2)
        self.index = random.choice(number)
        self.hitboxes = [(68, 68, - 10, - 14),
                         (38, 47, + 20, - 5),
                         (18, 12, + 32, + 30),
                         (20, 32, + 16, + 5),
                         (4,  7,  - 24, - 31)]
        self.hitbox = self.hitboxes[self.index]

    @property
    def currenthitbox(self):
        return (self.hitbox[0]+self.x, self.hitbox[1]+self.y, self.hitbox[2]+self.width, self.hitbox[3]+self.height)

    def draw(self, win):
        win.blit(asteroids[self.index], (self.x, self.y))
        pygame.draw.rect(win, (255, 0, 0), self.currenthitbox, 2)

关于python - 为什么我的程序没有显示我告诉它的点击框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57857774/

10-12 18:14