Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.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 Pygame删除列表中的对象_Python_List_Oop_Object_Pygame - Fatal编程技术网

Python Pygame删除列表中的对象

Python Pygame删除列表中的对象,python,list,oop,object,pygame,Python,List,Oop,Object,Pygame,上周我刚开始用OOP风格编程,当时我决定做一个小游戏。但现在我似乎被卡住了。我将解释到目前为止我所拥有的: 当玩家点击按钮时,将在玩家旁边创建一个bullet对象,并将bullet对象添加到bullets[]列表中。子弹然后以指定的方向水平穿过屏幕。如果子弹与玩家或墙壁碰撞,则会从子弹[]列表中删除。到目前为止,一切顺利 现在我似乎不知道当项目符号离开屏幕时如何从项目符号[]列表中删除它(屏幕定义在0和xmax之间)。另外,在我从列表中删除项目符号后,我是否也应该删除对象本身,还是自动删除 迄今

上周我刚开始用OOP风格编程,当时我决定做一个小游戏。但现在我似乎被卡住了。我将解释到目前为止我所拥有的:

当玩家点击按钮时,将在玩家旁边创建一个bullet对象,并将bullet对象添加到bullets[]列表中。子弹然后以指定的方向水平穿过屏幕。如果子弹与玩家或墙壁碰撞,则会从子弹[]列表中删除。到目前为止,一切顺利

现在我似乎不知道当项目符号离开屏幕时如何从项目符号[]列表中删除它(屏幕定义在0和xmax之间)。另外,在我从列表中删除项目符号后,我是否也应该删除对象本身,还是自动删除

迄今为止的代码:

    class BULLET(object):

#Constructor for the bullet, bullets are stored into array 'bullets'
#   The direction is stored to keep track of which player fired the bullet
def __init__(self,location,direction,color):
    self.rect = pg.Rect(location[0],location[1],xmax/160,xmax/160)
    self.bullet_type="normal"
    self.direction=direction
    self.color=color
    bullets.append(self)

#Moves the bullet horizontally across the screen, in the specified direction
#   The move function also checks for collision with any walls or players
#   The move function removes the bullet object from the list and destroys it
#   when it leaves the left or right side of the screen
def move(self,bullet_speed):
    self.rect.x += bullet_speed

    for wall in walls:
        if self.rect.colliderect(wall.rect):
            index=wall.rect.collidelist(bullets)
            del bullets[index]
            #Do I need to delete the object too? or just the list item?

    for player in players:
        if self.rect.colliderect(player.rect):
            index=player.rect.collidelist(bullets)
            if player.immune_timer <= 0:
                del bullets[index]
                player.immunity(500)
                player.life -= 1

    if self.rect.centerx > xmax or self.rect.centerx <0:
        #This is where I would like this instance of the bullet object to be deleted
        #   and to have the object removed from the bullets[] list 
类项目符号(对象):
#对于项目符号的构造函数,项目符号存储在数组“子弹”中
#该方向被存储以跟踪哪个玩家发射了子弹
定义初始(自身、位置、方向、颜色):
self.rect=pg.rect(位置[0],位置[1],xmax/160,xmax/160)
self.bullet\u type=“正常”
方向
self.color=color
项目符号。附加(自身)
#沿指定方向在屏幕上水平移动项目符号
#移动功能还检查是否与墙壁或玩家发生碰撞
#move函数从列表中删除项目符号对象并将其销毁
#当它离开屏幕的左侧或右侧时
def移动(自身、子弹头速度):
自校正x+=子弹头速度
对于墙中墙:
如果self.rect.collide rect(wall.rect):
索引=wall.rect.collidelist(项目符号)
删除项目符号[索引]
#我是否也需要删除该对象?还是仅仅是列表项?
对于玩家中的玩家:
如果self.rect.collide rect(player.rect):
index=player.rect.collidelist(项目符号)

如果player.immune\u timer xmax或self.rect.centerx我建议您在主循环中执行以下操作:

bullets = [bullet for bullet in bullets if 0 < bullet.rect.centerx < xmax]
bullets=[如果0

这将只保留应该在列表中的项目。

哇,感谢您的快速回复,这很有魅力。我还有一个问题。作为OOP新手,我是否也需要删除该对象?或者这仅仅存在于列表中?为了避免在游戏运行较长时间时出现延迟,我最近制作了一个有很多延迟的游戏,所以我不得不做一些事情来修复它。这与从列表中删除它相同,因为列表不再包含它。当然,它仍然在屏幕上绘制,但这不会导致延迟,因为它已经绘制,目前还没有绘制。