<p>如果你想管理多个“便便”,那么你必须创建一个列表。每个“poop”都是一个对象(a<a href="https://docs.python.org/3/tutorial/classes.html" rel="nofollow noreferrer">class</a>的实例)</p>
<p>板条箱一个类<code>Poop</code>,它可以<code>update</code>位置和<code>draw</code>船尾:</p>
<pre><code>class Poop:
def __init__(self, x, y, w, h):
self.rect = pygame.Rect(x, y-h//2, w, h)
self.vel = -15
def update(self):
self.rect.x += self.vel
def draw(self, surf):
pygame.draw.rect(surf, (255, 200, 125), self.rect)
poops = []
</code></pre>
<p>使用计时器事件生成粪便。使用<a href="https://www.pygame.org/docs/ref/time.html#pygame.time.set_timer" rel="nofollow noreferrer">^{<cd4>}</a>重复创建<a href="https://www.pygame.org/docs/ref/event.html" rel="nofollow noreferrer">^{<cd5>}</a>。时间以毫秒为单位设置。通过<a href="https://docs.python.org/3/library/random.html" rel="nofollow noreferrer">^{<cd6>}</a>设置随机时间,例如,将时间设置在0.5到4秒之间(当然,您可以选择自己的时间间隔):</p>
<pre class="lang-py prettyprint-override"><code>min_time, max_time = 500, 4000 # 0.5 seconds to to 4 seconds
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
</code></pre>
<p>注意,在pygame中可以定义客户事件。每个事件都需要一个唯一的id。用户事件的id必须从<code>pygame.USEREVENT</code>开始。在本例中<code>pygame.USEREVENT+1</code>是计时器事件的事件id,该事件生成POOP</p>
<p>当事件在事件循环中发生时,创建一个新的poop,并设置一个新的随机时间:</p>
<pre class="lang-py prettyprint-override"><code>for event in pygame.event.get():
# [...]
if event.type == spawn_event:
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))
</code></pre>
<p>在循环中更改粪便的位置,如果它们离开左侧的窗口,则将其从列表中删除:</p>
<pre class="lang-py prettyprint-override"><code>for poop in poops[:]:
poop.update()
if poop.rect.right <= 0:
poops.remove(poop)
</code></pre>
<p>把它们画成一个圈</p>
<pre class="lang-py prettyprint-override"><code>for poop in poops:
poop.draw(screen)
</code></pre>
<p>请参见示例:</p>
<pre class="lang-py prettyprint-override"><code>import pygame
from pygame.locals import *
import random
pygame.init()
pygame.display.set_caption('STINKY BEETLE')
class Poop:
def __init__(self, x, y, w, h):
self.rect = pygame.Rect(x, y-h//2, w, h)
self.vel = -15
def update(self):
self.rect.x += self.vel
def draw(self, surf):
pygame.draw.rect(surf, (255, 200, 125), self.rect)
screen_width = 800
screen_height = 600
game_running = True
pl_x, pl_y = screen_width//10, screen_height//2
pl_width, pl_height, pl_vel = 80, 40, 30
en_width, en_height, en_yvel = 80, 40, -10
en_x, en_y, = screen_width - screen_width//10 - en_width, screen_height//2
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
min_time, max_time = 500, 4000 # 0.5 seconds up to 4 seconds
spawn_event = pygame.USEREVENT + 1
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
poops = []
while game_running:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
if event.type == MOUSEBUTTONDOWN:
if event.button == 4 and pl_y > pl_vel:
pl_y -= pl_vel
elif event.button == 5 and pl_y < screen_height - pl_width:
pl_y += pl_vel
if event.type == spawn_event:
pygame.time.set_timer(spawn_event, random.randint(min_time, max_time))
poops.append(Poop(en_x, en_y+en_yvel+en_height//2, 50, 30))
en_y += en_yvel
if en_y <= 0 or en_y >= screen_height - en_height:
en_yvel =- en_yvel
for poop in poops[:]:
poop.update()
if poop.rect.right <= 0:
poops.remove(poop)
screen.fill((0, 0, 0))
for poop in poops:
poop.draw(screen)
pygame.draw.rect(screen, (105, 255, 125), (pl_x, pl_y, pl_width, pl_height))
pygame.draw.rect(screen, (255, 125, 115), (en_x, en_y, en_width, en_height))
pygame.display.update()
pygame.quit()
</code></pre>