【发布时间】:2020-09-13 13:56:56
【问题描述】:
我正在尝试在 Pygame 中构建一个模拟器来模拟正在种植的草。尝试获取当前恶意(草)的位置的目的是为了在它旁边添加一个新的精灵(草)。
首先,我创建一个类,为草块提供一个位置。
class Grass(pygame.sprite.Sprite):
def __init__(self, width, height, pos_x, pos_y, color):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.center = [pos_x, pos_y]
然后我添加一片草,以便我们可以开始产卵过程,我通过创建一个组来做到这一点。
grass_group = pygame.sprite.Group()
grass = Grass(20, 20, random.randrange(50, width - 50), random.randrange(50, height - 50), green)
grass_group.add(grass)
grass_group.draw(screen)
然后我想在旧草的旁边每秒创建一个新草。
if seconds <= (one_second + 100) and seconds >= (one_second - 100):
one_second += 1000
for i in range(len(grass_group)):
for j in range(len(grass_group)):
j.x =
j.y =
i.x = random.choice(j.x - 20, j.x, j.x + 20)
i.y = random.choice(j.y - 20, j.y, j.y + 20)
i = Grass(20, 20, i.x, i.y, green)
grass_group.add(i)
grass_group.draw(screen)
pygame.display.flip()
所以我需要找出所有旧草的位置,以便在它附近创建新草。
【问题讨论】: