调用类未运行方法

时间:2019-06-20 06:27:34

标签: python pygame

我正在尝试使用类在pygame中的屏幕上为对象设置动画。

我在没有该类的情况下尝试了此方法,并且效果很好,但是在该类中却无法正常工作。

class Car:
    def __init__(self):
        self.locx = 20
        self.locy = 90
        self.x = 20
        self.y = 90

    def draw_car(self):
        pygame.draw.circle(screen, RED, [self.locx, self.locy], 20, 8)

    def animator(self):
        self.locx += 5


def main_game():  # main game loop, for all code related to the simulation
    game_play = False
    while not game_play:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_play = True
                pygame.quit()

        clock.tick(60)
        screen.fill(BLACK)
        pygame.draw.line(screen, BLUE, [1, 450], [800, 450], 5)
        draw_road()
        Car()

绘制一个圆圈,并在整个班级上对其进行动画处理。

1 个答案:

答案 0 :(得分:1)

调用Car()仅创建一个Car对象。除非您调用Car.draw_carCar.animator,否则它不会被绘制或移动。您需要做的是在Car循环之前创建while对象,并将其分配给变量my_car这样。要绘制和移动汽车,您需要在my_car.animator()循环(即

)中调用my_car.draw_carwhile
def main_game():  # main game loop, for all code related to the simulation
    game_play = False
    my_car = Car()
    while not game_play:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_play = True
                pygame.quit()

        clock.tick(60)
        screen.fill(BLACK)
        pygame.draw.line(screen, BLUE, [1, 450], [800, 450], 5)
        draw_road()
        my_car.animator()
        my_car.draw_car()