调用类没有运行方法

2024-03-29 07:15:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用类在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()

画一个圆并用一个类在屏幕上设置动画


Tags: selfeventgameplay屏幕maindef情况
1条回答
网友
1楼 · 发布于 2024-03-29 07:15:22

调用Car()只会创建一个Car对象。在调用Car.draw_carCar.animator之前,它不会被绘制或移动。您需要做的是在while循环之前创建Car对象,并将其赋给变量my_car。要绘制和移动汽车,需要在while-循环中调用my_car.animator()my_car.draw_car,即

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()

相关问题 更多 >