我试图在 pygame 中制作一个圆形精灵。我的精灵课:
import pygame
WHITE = (255, 255, 255)
class player(pygame.sprite.Sprite):
def __init__(self, color, width, height, speed):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the player, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
#Initialise attributes of the car.
self.width = width
self.height = height
self.color = color
self.speed = speed
# Draw the player
pygame.draw.circle(self.image,self.color,self.speed,5)
这将返回错误:
line 23, in __init__ pygame.draw.circle(self.image,self.color,self.speed,5) TypeError: argument 3 must be 2-item sequence, not int
所以我一直在尝试不同的来源,但我似乎永远无法弄清楚如何去做。那么如何制作圆形精灵呢?它不需要移动或任何东西 - 我只需要一个小的(ish)精灵。
的第三个参数pygame.draw.circle()
必须是具有 2 个分量的元组,圆的 x 和 y 中心协调:
pygame.draw.circle(self.image,self.color,self.speed,5)
pygame.draw.circle(self.image, self.color, (self.width//2, self.height//2), 5)
在上面的例子中(self.width//2, self.height//2)
是圆的中心和5
半径(以像素为单位)。
另见
Pygame 不会让我画圆错误参数 3 必须是长度为 2 的序列,而不是 4
此外,一个pygame.sprite.Sprite
对象应该总是有一个.rect
属性(实例pygame.Rect
):
class player(pygame.sprite.Sprite):
def __init__(self, color, width, height, speed):
# Call the parent class (Sprite) constructor
super().__init__()
# [...]
pygame.draw.circle(self.image, self.color, (self.width//2, self.height//2), 5)
self.rect = self.image.get_rect()
注意,对象的.rect
and.image
属性pygame.sprite.Sprite
被 a 的.draw()
, 方法pygame.sprite.Group
用来绘制包含的精灵。
因此,一个子画面可以通过改变位置(例如被移动self.rect.x
,self.rect.y
在该矩形编码)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句