我認爲最好給出一個例子說明爲什麼我想要這樣做。 說我有一個遊戲。 還有一些物品是enemeys 這些enemeys有一個正在運行的類(健康,武器等) 如果玩家殺死敵人,我想從遊戲中卸載enemeys數據(移除該類的敵人實例)我會如何去做這件事?如何在python中卸載一個類的實例
編輯:
確定使用DEL劑量沒有幫助。這裏是一個基本的例子想像obj1是一個敵人,並在我= 10的想象力,敵人死亡。 我的代碼刪除但是obj的功能更新()是在OBJ1仍然被稱爲殼仍然打印「AAAAAAAAAAAA」即使外殼打印ð爲真
import threading
import time
import sched
#import Queue
#setting up some OneInstance Var's
starttime=time.time()
condition = threading.Condition()
lock = threading.Lock()
#This is the tick method for calling the update threads in the child classes (set at 20 Ticks/s)
#The tick is run in its own thread so it can not be interrupted
#The tick method is part of the Base module but not part of the base class As there needs to be ONLEY ONE INSTANCE of it
def Tick(cond):
while True:
with cond:
cond.notifyAll() #This clears the block on the threads evey 20th of a second
time.sleep(0.05 - ((time.time() - starttime) % 0.05)) #This is the 20th of a second part
tickThread=threading.Thread(name = 'ThreadTick', target=Tick, args=(condition,)) #This setsup the Tick in its own thread
tickThread.start()#This runs said thread
#BaseClass is the class All other classes inhearit from
class BaseClass(object):
def __init__(self, name):
global condition
global lock
self.name = name
t=threading.Thread(name = 'Thread'+self.name, target=self.UpdateHandler, args=(condition,lock,))
t.start()
def BaseClassType(self):
"""Returns a string of the childs type"""
pass
def UpdateHandler(self, cond, lock):
#this part handles when to call the update.
#it is allso needed so that it can be run in a thread.
while True:
self.lock = lock #this is just passed down evey tick so that threads can lock them selfs when writing to the shell
self.Update() #Calls the update on all child classes
with cond:
cond.wait()#This makes all the threads waite(besides the tick thread) when they are done for the next tick.
def Update(self):
#This is a place holder.
#It stops classes that don't have a Update function crashing when called
pass
def Unload(self):
#this method will terminate the thread.... Don't know if this is even possible in python yet
#and then remove any active instances of itself befor removing the class instance
pass
#This class is made so that I did not have to rewrite self.lock.blablabla eatch time i wanted to print to the shell
def Print (self, message):
self.lock.acquire()
print(message)
self.lock.release()
#---------------------------------------------------------------------------------
class ChildClassA(BaseClass):
def __init__(self, name):
super(ChildClassA, self).__init__(name)
def BaseClassType(self):
return 'ChildClassA'
def Update(self):
self.Print('AAAAAAAAAAAAAAAAAAAAAAA')
#----------------------------------------------------------------------------------
class ChildClassB(BaseClass):
def __init__(self, name):
super(ChildClassB, self).__init__(name)
def Update(self):
#print(self.name, "This is a completley different action")
self.Print('BBBBBBBBBBBBBBBBBBB')
self.Hi()
def Hi(self):
self.Print("Hi")
#----------------------------------------------------------------------------------
#works now just like in unity
class ChildClassC(BaseClass): #the new class
def __init__(self, name): #this is the equivalent of start()
super(ChildClassC, self).__init__(name) #this is the onley extra bit but its just to pass some name data through the classes
def Update(self): #this is litaley the same as update() except it has self in the parameters
self.Print("CCCCCCCCCCCCCCCCCCCCCCCCCC")
#--------------------------------------------------------------------------------
obj1 = ChildClassA('Obj1')
obj2 = ChildClassB('Obj2')
obj3 = ChildClassC('Obj3')
i=0
d=False
while True:
if i >=10:
if d==False:
del obj1
d = True
i=i+1
print("D: ", d)
print("I: ", i)
您可以使用'del obj'。 – selfboot
你的意思是刪除對象嗎?查看[del](https://docs.python.org/3.5/reference/simple_stmts.html#the-del-statement)。 [這是一個關於它的SO問題。](http://stackoverflow.com/questions/6146963/when-is-del-useful-in-python) – SethMMorton
你應該安裝某種英文自動正確的插件 – Julius