如何在 pygame 中创建圆形精灵
how to create a circular sprite in pygame
我尝试在 pygame 中制作一个圆形精灵。我的精灵 class:
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)
这个returns错误:
line 23, in __init__
pygame.draw.circle(self.image,self.color,self.speed,5)
TypeError: argument 3 must be 2-item sequence, not int
所以我一直在尝试不同的来源,但我似乎永远无法弄清楚该怎么做。那么如何制作圆形精灵呢?它不需要移动或任何东西 - 我只需要一个小的(大概)精灵。
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.sprite.Sprite
object should always have a .rect
attribute (instance of 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()
请注意,pygame.sprite.Sprite
对象的 .rect
和 .image
属性被 .draw()
, method of a pygame.sprite.Group
用来绘制包含的精灵。
因此可以通过更改矩形中编码的位置(例如self.rect.x
、self.rect.y
)来移动精灵。
我尝试在 pygame 中制作一个圆形精灵。我的精灵 class:
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)
这个returns错误:
line 23, in __init__ pygame.draw.circle(self.image,self.color,self.speed,5) TypeError: argument 3 must be 2-item sequence, not int
所以我一直在尝试不同的来源,但我似乎永远无法弄清楚该怎么做。那么如何制作圆形精灵呢?它不需要移动或任何东西 - 我只需要一个小的(大概)精灵。
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.sprite.Sprite
object should always have a .rect
attribute (instance of 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()
请注意,pygame.sprite.Sprite
对象的 .rect
和 .image
属性被 .draw()
, method of a pygame.sprite.Group
用来绘制包含的精灵。
因此可以通过更改矩形中编码的位置(例如self.rect.x
、self.rect.y
)来移动精灵。