網(wǎng)站首頁 編程語言 正文
三角函數(shù)
如果我們以O(shè)P作為圓的半徑r,以o點作為圓的圓心,圓上的點的x坐標(biāo)就是r * cos a ,y坐標(biāo)就是 r * sin a。
python中提供math.cos() 和 math.sin(),要求參數(shù)為弧度。
弧度和角度的關(guān)系
PI代表180度,PI就是圓周率:3.1415926 535 897392 23846,python提供了角度和弧度的轉(zhuǎn)化
math.degress() 弧度轉(zhuǎn)角度
math.radiens() 角度轉(zhuǎn)弧度
a = math.cos(math.radians(90))
90度的橫坐標(biāo)為0,但因為PI不是浮點小數(shù),導(dǎo)致運(yùn)算不準(zhǔn)確,是接近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開始,隨機(jī)一個顏色,計算出這個角度上的大圓上的點,以這個點畫一個半徑為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
相關(guān)推薦
- 2023-05-13 Python?readline()和readlines()函數(shù)實現(xiàn)按行讀取文件_python
- 2022-09-15 一文搞懂C++中繼承的概念與使用_C 語言
- 2022-04-12 C++制作簡單的計算器功能_C 語言
- 2022-08-10 c#實現(xiàn)哈夫曼樹算法_C#教程
- 2023-01-05 Kotlin?掛起函數(shù)CPS轉(zhuǎn)換原理解析_Android
- 2022-06-21 git基礎(chǔ)之各版本控制系統(tǒng)介紹_其它綜合
- 2022-04-26 jQuery實現(xiàn)鎖定頁面元素(表格列)_jquery
- 2022-05-11 python?DataFrame數(shù)據(jù)分組統(tǒng)計groupby()函數(shù)的使用_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支