我想在 pygame 中慢慢画一个圆圈,这样绘图的行为实际上是肉眼可见的。我在 stackoverflow 上得到了一个函数,可以通过增加终点并保持起点相同来绘制直线,但无法弄清楚如何在 pygame 屏幕中慢慢绘制圆。
x = r * cos(radians(i)) + a
y = r * sin(radians(i)) + b
其中
a
为圆心 x
坐标,b
为圆心 y
坐标
r
是圆的半径。
要减慢动画速度,请使用
Clock
对象。您可以从内置 sin
模块访问函数 cos
和 math
(请注意,您需要以弧度形式传递值,因此 radians
函数非常重要)。 实施:
import pygame
from math import sin, cos, radians
pygame.init()
wn = pygame.display.set_mode((600, 600))
r = 100
a = 300
b = 200
clock = pygame.time.Clock()
for i in range(1, 361):
clock.tick(30)
pygame.draw.circle(wn, (255, 255, 255), (int(r * cos(radians(i)) + a), int(r * sin(radians(i)) + b)), 2)
pygame.display.update()
输出:如果您更喜欢使用标准线作为轮廓而不是重叠点,请使用 函数,如下所示:
import pygame
from math import sin, cos, radians
pygame.init()
wn = pygame.display.set_mode((600, 600))
r = 100
a = 300
b = 200
def x_y(r, i, a, b):
return (int(r * cos(radians(i)) + a), int(r * sin(radians(i)) + b))
clock = pygame.time.Clock()
for i in range(0, 360, 2):
clock.tick(30)
pygame.draw.line(wn, (255, 255, 255), x_y(r, i, a, b), x_y(r, i+1, a, b), 2)
pygame.display.update()
您可以运行这些代码并获得输出:
import turtle
t = turtle.Turtle()
t.circle(50)
import pygame
pygame.init()
screen = pygame.display.set_mode((626, 416))
pygame.draw.circle(screen, (r,g,b), (x, y), R, w)
running = True
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
这是如何在 pygame 屏幕上绘制一个圆,其中 (r, g, b)
为颜色,
(x, y)
为中心,
R
为半径,
w
为圆的厚度。