我用Pyqt4编写了一个Snake游戏。
处理我的物品时遇到了严重的问题。问题是我有4个对象(我没有包括剩下的两个因为我认为没有重要但我可以上传,如果有必要)并且当Screen对象调用Example对象时会丢失一个错误并且它不起作用。但是如果Example使用Screen对象创建一个var,则不会丢弃错误!
class Screen(QtGui.QWidget):#This class wrapp the game with the snake and the food
comprovamenjar = 0
def __init__(self):
super(Screen,self).__init__()
self.initUI()
self.snake = Snake()
self.menjar = Menjar()
self.example = Example() #< --------------------------------This provoke error
def initUI(self):
pass
def paintEvent(self,e): #We paint stuff..
#The code from below didn't work also
"""if self.snake.coordenadax < 0 or self.snake.coordenadax > self.height or self.snake.coordenaday < 0 or self.snake.coordenaday > self.width :
#self.example.timer.stop()
pass
else:"""
qp = QtGui.QPainter()
qp.begin( self )
self.snake.pintacap( qp,self.food)#Paint Snake's Head
self.snake.pintacos( qp,self.food)#Paint Snake's Body
self.comprovamenjar = self.snake.pintacos#Check food
self.menjar.pinta( qp,self.snake)#Paint Food
qp.end()
class Example(QtGui.QWidget): #This Wrapp again the game with all the stuff
p = None
def __init__(self):
super(Example, self).__init__()
self.initUI()
def keyPressEvent(self, e): #Keyboard functions
if e.key() == QtCore.Qt.Key_Up:
direccio = 3
elif e.key() == QtCore.Qt.Key_Down:
direccio = 2
elif e.key() == QtCore.Qt.Key_Right:
direccio = 1
elif e.key() == QtCore.Qt.Key_Left:
direccio = 0
self.p.serp.startup = 1
self.p.serp.direccio(direccio,self.p)
def initUI(self): #Screen options
#Screen
self.p = Screen()
self.p.setParent(self)
self.p.move(0,0)
self.p.resize(self.p.height(),self.p.width())
self.timer = QtCore.QTimer()
self.p.amplada = self.p.width()
self.p.alcada = self.p.height()
self.timer.timeout.connect(self.p.repaint)#colbac
self.timer.start(15)##########################################Snake's Speed Movement
self.setGeometry(100, 100, self.p.amplada, self.p.alcada)
self.setWindowTitle('Signal & slot')
self.show()
self.setFocus()
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
错误是这样的:
File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 169,
in <module> main()
File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 164,
in main ex = Example()
File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 132,
in __init__ self.initUI()
File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 146,
in initUI self.p = Screen()
File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 104,
in __init__ self.ex = Example()#<------------------------------------------------
The problem File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 132,
in __init__ self.initUI()
File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 146,
in initUI self.p = Screen()
File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 104, in __init__ self.ex = Example()#<------------------------------------------------ The problem File "C:\Users\Marc\Desktop\GarciaMarcSerp1.py", line 132, in __init__
任何帮助将不胜感激
答案 0 :(得分:0)
在构造对象时似乎有无限递归。每个Screen
实例都尝试创建Example
实例以存储在self.example
中,但每个Example
实例都会尝试创建Screen
实例以存储在self.p
中}}。最终Python达到递归限制并放弃,引发异常,就像你部分引用的一样。
您需要打破对象创建的循环。也许您的Example
类可以将Screen
对象作为__init__
的参数?然后Screen
可以将自己作为该对象传递。
这是一些显示我的意思的快速代码:
class Screen(QtGui.QWidget):
def __init__(self):
# other init stuff
self.example = Example(self) # pass self to the Example constructor
# other methods
class Example(QtGui.QWidget):
def __init__(self, screen): # take screen as a parameter
super(Example, self).__init__()
self.initUI(screen) # pass it along to initUI
# other methods, etc
def initUI(self, screen): # take screen as a parameter
self.p = screen # use the passed screen rather than creating one
# do other stuff with self.p, same as before