Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我试图删除游戏中的一个对象,但它出现在屏幕上,但是;del ObjecName“;不';行不通_Python_Class_Pygame - Fatal编程技术网

Python 我试图删除游戏中的一个对象,但它出现在屏幕上,但是;del ObjecName“;不';行不通

Python 我试图删除游戏中的一个对象,但它出现在屏幕上,但是;del ObjecName“;不';行不通,python,class,pygame,Python,Class,Pygame,我是个新手,所以如果我的问题是一些非常基本的问题,我道歉。我想在敌人离开屏幕时将其删除。我在网上搜索了这个页面,发现了一个类似的Stackoverflow页面,我知道我需要做一些类似于del敌军的事情,但它似乎不起作用。我有 def __del__(self): print("object deleted") 方法,因此它应该输出它已被删除 以下是我尝试删除对象的函数: def enemy_actions(enemies, enemy_clock): if enemy_

我是个新手,所以如果我的问题是一些非常基本的问题,我道歉。我想在敌人离开屏幕时将其删除。我在网上搜索了这个页面,发现了一个类似的Stackoverflow页面,我知道我需要做一些类似于
del敌军
的事情,但它似乎不起作用。我有

def __del__(self):
    print("object deleted") 

方法,因此它应该输出它已被删除

以下是我尝试删除对象的函数:

def enemy_actions(enemies, enemy_clock):


    if enemy_clock == 1:

        free_lanes = 0
        free_lane_positions = []
        new_enemies_lanes = []
        print("creating enemies")




        #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])




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


            #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, 60, enemy_path, random.randint(8,13))
                enemies.append(Enemy)



    #chekcing if the nemy if of the screen
    for Enemy in enemies:
        if Enemy.y > 650:
            del Enemy

这是一张所有敌人的名单。 如果需要更多代码,请告诉我。
谢谢

只需重建敌人列表,而不包含您不想要的敌人:

#checking if the enemy is off the screen
enemies = [enemy for enemy in enemies if enemy.y <= 650]
#检查敌人是否离开屏幕
敌人=[敌人对敌人,如果敌人.y
#checking if the enemy is off the screen
enemies = [enemy for enemy in enemies if enemy.y <= 650]