我正在尝试使用类在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()
画一个圆并用一个类在屏幕上设置动画
调用
Car()
只会创建一个Car
对象。在调用Car.draw_car
或Car.animator
之前,它不会被绘制或移动。您需要做的是在while
循环之前创建Car
对象,并将其赋给变量my_car
。要绘制和移动汽车,需要在while
-循环中调用my_car.animator()
和my_car.draw_car
,即相关问题 更多 >
编程相关推荐