所以我一直在使用pygame进行我的第一场比赛,我创建了一个创建敌人的函数。关于它们的创建方式,有一些逻辑-必须始终留有一条自由车道,因此敌人的总数不得超过3。有几种随机选择,当随机生成某些值组合时,它会崩溃。
我的功能:
#cretating and manipulating enemies
def enemy_actions(enemies):
free_lanes = 0
free_lane_positions = []
new_enemies_lanes = []
#going through all lanes
for i in lanes:
lane_taken = i[1]
if not lane_taken:
#counting how many free lanes there are
free_lanes = free_lanes + 1
#adding free lane position to a list
free_lane_positions.append(i[0])
print("Number of free lanes " + str(free_lanes))
#if atleast 2 lanes are free then we randomly select how many new enemies we will add
if free_lanes > 1:
#randomly selecting how many enemies will be added
number_of_enemies = random.randint(1,len(free_lane_positions) - 1)
print("Number of enemies " + str(number_of_enemies))
#repeating action for the number of enemies required
for i in range(number_of_enemies):
#randomly selecting lanes for enemies
lane_x = random.choice(free_lane_positions)
#adding it to the list of taken lanes
new_enemies_lanes.append(lane_x)
#removing taken up lane from list of free lanes
free_lane_positions.remove(lane_x)
#marking lane_x as taken in lanes
for i in lanes:
if i[0] == lane_x:
i.remove(False)
i.append(True)
#(self,place,x,y,length,width,path,speed):
#building enemy
for i in new_enemies_lanes:
Enemy = enemy(screen,i,enemy_y_start,60,enemy_path,random.randint(3,8))
enemies.append(Enemy)
当值
Number of free lanes 4
Number of enemies 1
Number of free lanes 3
Number of enemies 1
Number of free lanes 2
Number of enemies 1
游戏可能会在多个周期内生成,并因错误而崩溃:
Traceback (most recent call last):
File "Pygame.py",line 225,in <module>
enemy_actions(enemies)
File "Pygame.py",line 136,in enemy_actions
Enemy = enemy(screen,8))
TypeError: 'enemy' object is not callable
我不知道这种情况与任何其他组合有何不同。请告诉我是否需要额外的代码。
谢谢!