在 pygame 中创建精灵的问题
Questions with creating sprites in pygame
所以基本上我知道该怎么做是添加一个玩家精灵(制作一个继承自 pygame.sprite 的玩家 class,等等...)这对我有用。
我想知道如何迭代创建精灵并将它们添加到精灵组中。
这是因为我有一个二维数组,我有一个函数可以读取它并将 "tiles" 相应地放置在二维 space 中,这是为了更容易创建关卡。
所以我想要这个函数做的是创建这些精灵(我猜是用一个读取数组的 for 循环?)并将它们添加到组中,但这不起作用所以我首先有一些问题:
1)能否在 class 中的 init 函数之外创建精灵?
2)什么是精灵,它是一个表面耦合到一个矩形吗?
3)最后你是否知道如何简单地完成这项工作:如果我给你一个二维数组,你将如何编写读取该数组并计算位置的函数(这没关系,我我想我已经弄清楚了),最重要的是,从给定的位置制作精灵,然后可以将其添加到精灵组中。
提前致谢
Can you create sprites outside of the init function in a class?
当然可以。
What really are sprites, is it a surface coupled to a rect ?
如果我们谈论 pygame 的 Sprite
class:是的。
这样的精灵基本上是一个Surface
(image
属性)和一个Rect
(rect
属性)。它们与 pygame 的 Group
classes.
一起使用效果最佳
And finally do you have an idea of simply how to get this done ....
只需创建一个嵌套循环来遍历数组。
这是一个简单的例子:
import pygame
pygame.init()
TILESIZE = 64
class Actor(pygame.sprite.Sprite):
def __init__(self, color, pos):
super().__init__()
self.image = pygame.Surface((TILESIZE, TILESIZE))
self.image.fill(pygame.Color(color))
self.rect = self.image.get_rect(topleft = pos)
def main():
data = [
' YUB ',
' G ',
' B ',
' ',
' Y ',
' U ',
]
screen = pygame.display.set_mode((len(data[0]) * TILESIZE, len(data) * TILESIZE))
sprites = pygame.sprite.Group()
colors = {
'G': 'green',
'B': 'black',
'Y': 'yellow',
'U': 'dodgerblue'
}
x, y = 0, 0
for line in data:
for char in line:
if char in colors:
sprites.add(Actor(colors[char], (x * TILESIZE, y * TILESIZE)))
x += 1
x = 0
y += 1
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
return
sprites.update()
screen.fill((100, 100, 100))
sprites.draw(screen)
pygame.display.flip()
main()
你可以找到另一个例子here。
所以基本上我知道该怎么做是添加一个玩家精灵(制作一个继承自 pygame.sprite 的玩家 class,等等...)这对我有用。
我想知道如何迭代创建精灵并将它们添加到精灵组中。 这是因为我有一个二维数组,我有一个函数可以读取它并将 "tiles" 相应地放置在二维 space 中,这是为了更容易创建关卡。 所以我想要这个函数做的是创建这些精灵(我猜是用一个读取数组的 for 循环?)并将它们添加到组中,但这不起作用所以我首先有一些问题:
1)能否在 class 中的 init 函数之外创建精灵?
2)什么是精灵,它是一个表面耦合到一个矩形吗?
3)最后你是否知道如何简单地完成这项工作:如果我给你一个二维数组,你将如何编写读取该数组并计算位置的函数(这没关系,我我想我已经弄清楚了),最重要的是,从给定的位置制作精灵,然后可以将其添加到精灵组中。
提前致谢
Can you create sprites outside of the init function in a class?
当然可以。
What really are sprites, is it a surface coupled to a rect ?
如果我们谈论 pygame 的 Sprite
class:是的。
这样的精灵基本上是一个Surface
(image
属性)和一个Rect
(rect
属性)。它们与 pygame 的 Group
classes.
And finally do you have an idea of simply how to get this done ....
只需创建一个嵌套循环来遍历数组。
这是一个简单的例子:
import pygame
pygame.init()
TILESIZE = 64
class Actor(pygame.sprite.Sprite):
def __init__(self, color, pos):
super().__init__()
self.image = pygame.Surface((TILESIZE, TILESIZE))
self.image.fill(pygame.Color(color))
self.rect = self.image.get_rect(topleft = pos)
def main():
data = [
' YUB ',
' G ',
' B ',
' ',
' Y ',
' U ',
]
screen = pygame.display.set_mode((len(data[0]) * TILESIZE, len(data) * TILESIZE))
sprites = pygame.sprite.Group()
colors = {
'G': 'green',
'B': 'black',
'Y': 'yellow',
'U': 'dodgerblue'
}
x, y = 0, 0
for line in data:
for char in line:
if char in colors:
sprites.add(Actor(colors[char], (x * TILESIZE, y * TILESIZE)))
x += 1
x = 0
y += 1
while True:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
return
sprites.update()
screen.fill((100, 100, 100))
sprites.draw(screen)
pygame.display.flip()
main()
你可以找到另一个例子here。