如何在Pygame中慢慢画一个圆?

问题描述 投票:0回答:4

我想在 pygame 中慢慢画一个圆圈,这样绘图的行为实际上是肉眼可见的。我在 stackoverflow 上得到了一个函数,可以通过增加终点并保持起点相同来绘制直线,但无法弄清楚如何在 pygame 屏幕中慢慢绘制圆。

python pygame geometry
4个回答
2
投票

您可以使用标准的正弦和余弦圆公式:

  • 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()
输出:

如果您更喜欢使用标准线作为轮廓而不是重叠点,请使用

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()
    

0
投票
我推荐使用turtle库,因为它包含一个圆函数。例如,circle(40) 将绘制一个半径为 40 个单位的圆。当您运行程序时,圆圈将在您面前绘制


0
投票
你的问题表明你的主要目标是画一个圆圈,所以我建议你考虑使用海龟。

您可以运行这些代码并获得输出:

import turtle t = turtle.Turtle() t.circle(50)
    

0
投票
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
 为圆的厚度。

© www.soinside.com 2019 - 2024. All rights reserved.