網(wǎng)站首頁 編程語言 正文
三角函數(shù)
如果我們以O(shè)P作為圓的半徑r,以o點(diǎn)作為圓的圓心,圓上的點(diǎn)的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,但因?yàn)镻I不是浮點(diǎn)小數(shù),導(dǎo)致運(yùn)算不準(zhǔn)確,是接近0的一個(gè)值。
基本包和事件捕捉
初始化窗口,配置圓心和半徑,添加了定時(shí)器便于控制繪制的速度
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 # 定時(shí)器 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ī)一個(gè)顏色,計(jì)算出這個(gè)角度上的大圓上的點(diǎn),以這個(gè)點(diǎn)畫一個(gè)半徑為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 # 定時(shí)器 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)推薦
- 2022-09-21 jquery實(shí)現(xiàn)網(wǎng)頁左側(cè)導(dǎo)航菜單欄_jquery
- 2023-01-17 CMakeList中自動編譯protobuf文件過程_C 語言
- 2022-03-19 .NET6使WebApi獲取訪問者IP地址_基礎(chǔ)應(yīng)用
- 2023-07-16 callBack: function(res){} 與 callBack: res =>{}
- 2022-10-18 docker-desktop啟動k8s的詳細(xì)方法_docker
- 2022-08-03 Go?GORM版本2.0新特性介紹_Golang
- 2022-12-29 基于R語言時(shí)間序列的平穩(wěn)時(shí)間序列模型預(yù)測圖文詳解_R語言
- 2022-10-13 Python實(shí)現(xiàn)RLE格式與PNG格式互轉(zhuǎn)_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錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(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)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支