擅长:python、mysql、java
<p>你走得很好。在</p>
<p>试试这个:</p>
<ul>
<li>把游戏的每一个状态提取到一个函数中</li>
<li>有一个变量知道当前哪个状态是“活动的”。在</li>
</ul>
<p>示例代码:</p>
<pre><code>def home():
events = pygame.event.get()
for event in events:
...
if something_happened:
switch_state(txtbox)
def txtbox():
events = pygame.event.get()
for event in events:
...
if something:
switch_state(home)
Function = home # assign the function itself to a variable
def switch_state(new_state):
global Function
Function = new_state
...
while True:
Function() # call the function which is currently active
</code></pre>
<p>下一步:</p>
<ul>
<li>将状态写为对象,而不是函数(这样它们就可以保存一些关于它们自身的数据——例如,你会有一个状态“级别”和所有关于特定级别的数据)</li>
<li>与一个全局的<code>Function()</code>不同的是,保留一个状态列表,这样你就可以把一个新的状态推到最上面,然后弹出它并返回到以前的任何状态。这将使您可以轻松地管理多个游戏屏幕。在</li>
</ul>