Pygame:物体一个接一个地移动

时间:2017-12-04 09:54:46

标签: python loops object pygame

我正在使用pygame创建塔防游戏。产生和移动敌人的功能很好。然而,下一个敌人只在第一个敌人完成移动时移动。我认为这与forwhile循环有关。谁可以纠正我的代码,以便对象同时移动。

此功能在游戏的主循环中调用:

# creates and places all of the enemys
def begin_wave(self):
    # mark next waves as started
    self.wave += 1
    print("Wave number: ", self.wave)

    ### Draw Enemy ###
    for enemy in self.enemies:
        while enemy.waypoints_reached < len(config.WAYPOINTS):

            # Move enemy
            enemy.move()

            # Paint game + new enemy location
            self.paint(self.screen)

            # Update Screen
            pygame.display.update()

    # if done -> set state to BA_CLEAR
    print("Wave Completed!")
    self.waves_comp += 1
    self.state = config.BA_CLEAR

需要的第一个功能是移动敌人的功能,这个是:

def move(self):

    speed = config.DEFAULT_SPEED

    # Update enemy's location
    if self.get_position()[0] < self.trackNextWaypoint()[1]:
        if self.trackNextWaypoint()[1] - self.get_position()[0] >= speed:
            self.set_position((self.get_position()[0] + speed, self.get_position()[1]))
        else:
            self.set_position((self.get_position()[0] + (self.trackNextWaypoint()[1] - self.get_position()[0]),
                       self.get_position()[1]))

    if self.get_position()[0] > self.trackNextWaypoint()[1]:
        if self.get_position()[0] - self.trackNextWaypoint()[1] >= speed:
            self.set_position((self.get_position()[0] - speed, self.get_position()[1]))
        else:
            self.set_position((self.get_position()[0] - (self.get_position()[0] - self.trackNextWaypoint()[1]),
                       self.get_position()[1]))

    if self.get_position()[1] < self.trackNextWaypoint()[2]:
        if self.trackNextWaypoint()[2] - self.get_position()[1] >= speed:
            self.set_position((self.get_position()[0], self.get_position()[1] + speed))
        else:
            self.set_position((self.get_position()[0],
                       self.get_position()[1] + (self.trackNextWaypoint()[2] - self.get_position()[1])))

    if self.get_position()[1] > self.trackNextWaypoint()[2]:
        if self.get_position()[1] - self.trackNextWaypoint()[2] >= speed:
            self.set_position((self.get_position()[0], self.get_position()[1] - speed))
        else:
            self.set_position((self.get_position()[0], self.get_position()[1] - (self.get_position()[1] - self.trackNextWaypoint()[2])))

    # If enemy position is on waypoint
    if self.get_position()[0] == self.trackNextWaypoint()[1] and self.get_position()[1] == self.trackNextWaypoint()[2]:
        self.setWaypointsReached(1)

最后,您可能还需要这些功能来理解:

def trackNextWaypoint(self):
    for waypoint in config.WAYPOINTS:
        if waypoint[0] == self.waypoints_reached+1:
            return waypoint
            break

def setWaypointsReached(self, number):
    self.waypoints_reached += number

def get_position(self):
    return self.position

def set_position(self, position):
    self.position = position

更新

我的可视化效果<{3}}

1 个答案:

答案 0 :(得分:1)

问题是因为您在while内使用for

for内的对象应该只做一小步,而外部while应该多次执行for

您需要一些方法来检查是否所有对象都移动到目标位置并再次重复for循环 我使用how_many_moved检查所有敌人是否在目的地。

可能是这样的

# value bigger than 0 to start `while` loop
how_many_moved = 1

while how_many_moved > 0:

    how_many_moved = 0

    ### every enemy makes only small step ###
    for enemy in self.enemies:
        if enemy.waypoints_reached < len(config.WAYPOINTS):
            # count how many was moved
            how_many_moved += 1

            # Move enemy
            enemy.move()

            # Paint game + new enemy location
            self.paint(self.screen)

    # Update Screen afer every enemy made small step
    pygame.display.update()