如何在Pygame中慢慢画一个圆?
How to slowly draw a circle in Pygame?
我想在pygame中慢慢画一个圆,这样画的动作实际上是肉眼可见的。我在 Whosebug 上得到了一个函数,通过递增终点并保持起点相同来绘制直线,但无法弄清楚如何在 pygame 屏幕上慢慢画一个圆。
我推荐使用 turtle 库,因为它包含一个 circle 函数。例如 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
作为圆的厚度。
您可以使用标准的正弦和余弦圆公式:
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()
我想在pygame中慢慢画一个圆,这样画的动作实际上是肉眼可见的。我在 Whosebug 上得到了一个函数,通过递增终点并保持起点相同来绘制直线,但无法弄清楚如何在 pygame 屏幕上慢慢画一个圆。
我推荐使用 turtle 库,因为它包含一个 circle 函数。例如 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
作为圆的厚度。
您可以使用标准的正弦和余弦圆公式:
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()