利用Pygame绘制圆环的示例代码
作者:我的天才女友 时间:2022-04-02 12:55:05
三角函数
如果我们以OP作为圆的半径r,以o点作为圆的圆心,圆上的点的x坐标就是r * cos a ,y坐标就是 r * sin a。
python中提供math.cos() 和 math.sin(),要求参数为弧度。
弧度和角度的关系
PI代表180度,PI就是圆周率:3.1415926 535 897392 23846,python提供了角度和弧度的转化
math.degress() 弧度转角度
math.radiens() 角度转弧度
a = math.cos(math.radians(90))
90度的横坐标为0,但因为PI不是浮点小数,导致运算不准确,是接近0的一个值。
基本包和事件捕捉
初始化窗口,配置圆心和半径,添加了定时器便于控制绘制的速度
import sys, random, math, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("梦幻圆")
screen.fill((0, 0, 100))
pos_x = 300
pos_y = 250
radius = 200
angle = 360
# 定时器
mainClock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
pygame.quit()
sys.exit()
主程序
角度不断的加,如果超过360度则重新重1开始,随机一个颜色,计算出这个角度上的大圆上的点,以这个点画一个半径为10的圆。
angle += 1
if angle >= 360:
angle = 0
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = r, g, b
x = math.cos(math.radians(angle)) * radius
y = math.sin(math.radians(angle)) * radius
pos = (int(pos_x + x), int(pos_y + y))
pygame.draw.circle(screen, color, pos, 10, 0)
pygame.display.update()
mainClock.tick(20)
全部代码
import sys, random, math, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("梦幻圆")
screen.fill((0, 0, 100))
pos_x = 300
pos_y = 250
radius = 200
angle = 360
# 定时器
mainClock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
pygame.quit()
sys.exit()
angle += 1
if angle >= 360:
angle = 0
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = r, g, b
x = math.cos(math.radians(angle)) * radius
y = math.sin(math.radians(angle)) * radius
pos = (int(pos_x + x), int(pos_y + y))
pygame.draw.circle(screen, color, pos, 10, 0)
pygame.display.update()
mainClock.tick(10)
来源:https://blog.csdn.net/qq_40801987/article/details/122584401
标签:Pygame,圆环
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pycharm设置虚拟环境与更换镜像教程
2023-06-10 06:58:05
![](https://img.aspxhome.com/file/2023/7/78447_0s.jpg)
10个Python面试常问的问题(小结)
2023-04-11 19:36:15
PyChon中关于Jekins的详细安装(推荐)
2021-03-17 08:07:31
![](https://img.aspxhome.com/file/2023/9/87789_0s.jpg)
python将html转成PDF的实现代码(包含中文)
2023-08-30 18:11:58
文字适度阅读的宽度或者字数
2007-10-26 07:31:00
python进程池的简单实现
2021-02-22 06:22:14
发一个自己用的JS框架(试用版^_^)
2011-09-11 18:53:24
Scrapy元素选择器Xpath用法汇总
2021-09-11 11:42:58
ZABBIX3.2使用python脚本实现监控报表的方法
2021-04-11 20:15:02
![](https://img.aspxhome.com/file/2023/0/79680_0s.jpg)
Python数据分析之使用matplotlib绘制折线图、柱状图和柱线混合图
2023-09-16 23:18:09
![](https://img.aspxhome.com/file/2023/5/93625_0s.png)
浅谈pytorch、cuda、python的版本对齐问题
2022-12-03 13:04:54
SQL查询入门(中篇)
2011-09-30 11:15:09
asp如何从数据库中调出个人档案?
2009-11-15 20:02:00
Bootstrap显示与隐藏简单实现代码
2023-08-24 17:30:54
![](https://img.aspxhome.com/file/2023/1/56221_0s.jpg)
CSS3的新特性
2009-03-23 17:46:00
![](https://img.aspxhome.com/file/UploadPic/20093/23/01-13s.jpg)
Magic Photo Frame 神奇创意相框
2009-09-15 20:45:00
端午节将至,用Python爬取粽子数据并可视化,看看网友喜欢哪种粽子吧!
2023-08-23 06:29:31
![](https://img.aspxhome.com/file/2023/2/62272_0s.png)
go和python变量赋值遇到的一个问题
2023-11-15 01:54:40
函数式编程让JS更优美
2008-06-10 12:40:00
怎样管理好css样式
2008-10-12 12:12:00