網站首頁 編程語言 正文
一、復習
首先將上次畫的矩形做復雜一些的小程序:
import pygame,sys, random
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for i in range(100):
width = random.randint(0, 250)
height = random.randint(0, 100)
top = random.randint(0, 400)
left = random.randint(0, 500)
pygame.draw.rect(screen, [0, 0, 0], [left, top, width, height], 1)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
在此基礎上還可以增加矩形的寬度和顏色:
import pygame,sys, random
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for i in range(100):
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
rect_width = random.randint(1, 5)
width = random.randint(0, 250)
height = random.randint(0, 100)
top = random.randint(0, 400)
left = random.randint(0, 500)
pygame.draw.rect(screen, [r, g, b], [left, top, width, height], rect_width)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
實現的效果如下:
二、畫單個像素
單個像素在pygame中就是畫一個寬高為1的矩形。
代碼示例:
import pygame, sys
import math
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for x in range(0, 640):
y = int(math.sin(x/640*math.pi*4)*200 + 240)
pygame.draw.rect(screen, [0, 0, 0], [x, y, 1, 1], 1)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
效果圖:
需要注意的是,矩形的線寬須是1,而不是平常寫的為0,這是因為矩形太小了,沒有中間部分可以填充。
三、連接多個點
二中畫的曲線,如果仔細看就會發現中間不是連續的,點與點之前存在間隙。這是因為在比較陡峭的地方,x每變動1個值,y就要變動2個或更多的值,因此出現縫隙。
我們可以用畫線的方式把各個點連接起來,這樣就不會有間隙了:
首先來看畫線函數:
發現此函數與draw.rect相比,只是參數plotPoints略有不同
1.連接程序生成的點
上關鍵代碼:
plotPoints = []
for x in range(0, 640):
y = int(math.sin(x/640*math.pi*4)*200+240)
plotPoints.append([x, y])
pygame.draw.lines(screen, [0, 0, 0], False, plotPoints, 1)
由于plotPoints是一個數組,因此我們需要先根據x值計算出所有的y值,然后將x,y成隊的加入到數組plotPoints中,最后再通過lines一次性畫出整個曲線來
效果圖如下:
2.連接外部給定的點
import pygame, sys
from data import dots
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
pygame.draw.lines(screen, [0, 0, 0], True, dots, 2)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
dots = [
[221, 432], [225, 331], [133, 342], [141, 310],
[51, 230], [74, 217], [58, 153], [114, 164],
[123, 135], [176, 190], [159, 77], [193, 93],
[230, 28], [267, 93], [301, 77], [284, 190],
[327, 135], [336, 164], [402, 153], [386, 217],
[409, 230], [319, 310], [327, 342], [233, 331],
[237, 432]
]
生成的效果圖:
四、逐點繪制
如果我們只是想改變某些像素的顏色,用draw.rect通過小矩形來做就有點浪費資源,可以用screen.set_at([x, y], [0, 0, 0])來實現相同的效果
示例代碼:
import pygame, sys, math
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for x in range(640):
y = math.sin(x/640*math.pi*4) * 200 + 240
screen.set_at([int(x), int(y)], [0, 0, 0])
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
效果圖:
原文鏈接:https://blog.csdn.net/luhouxiang/article/details/127699604
相關推薦
- 2022-08-30 詳解Oracle控制文件及日志文件的管理問題_oracle
- 2023-10-15 webrtc 測試video_loopback
- 2022-03-31 SQL?Server的觸發器詳解_MsSql
- 2022-04-10 elasticsearch + spring boot 配置
- 2022-06-16 Kotlin操作符重載實例詳解_Android
- 2022-04-04 小程序Arrow function should not return assignment
- 2022-04-17 iOS喚起記住密碼的數字鍵盤,輸入的時候發生閃爍的問題
- 2022-06-17 C#關鍵字之重載Overload介紹_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支