我想在 Pygame 中缓慢绘制圆形,以便裸眼可以看到绘制的过程。我从 StackOverflow 上找到了一个函数,用于通过递增终点并保持起点不变来绘制直线,但我无法弄清如何在 Pygame 屏幕上缓慢地绘制圆形。
x = r * cos(radians(i)) + a
y = r * sin(radians(i)) + b
a
是圆心的 x
坐标,b
是圆心的 y
坐标,r
是圆的半径。Clock
对象。您可以从内置的 math
模块中访问 sin
和 cos
函数(请注意,您需要将值作为弧度传递,因此需要导入 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()
输出:
如果您更喜欢使用标准线作为轮廓而不是重叠的点,请使用pygame.draw.line
函数,如下所示:
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()
我建议使用turtle库,因为它包含一个圆形函数。例如,circle(40)将绘制一个半径为40个单位的圆。当您运行程序时,圆将在您面前绘制。
你的问题说明你的主要目标只是画一个圆,所以我建议你考虑使用turtle。
你可以运行这些代码并获得输出:
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
作为圆的厚度。