Pygame的Draw繪圖
Pygame 中提供了一個draw模組用來繪製一些簡單的圖形形狀,例如矩形、多邊形、圓形、直線、弧線等。
pygame.draw模組的常用方法如下表所示:
#說明 | |
---|---|
#pygame.draw.rect() |
#繪製矩形 |
pygame.draw.polygon () |
繪製多邊形 |
#pygame.draw.circle() | ##依圓心和半徑繪製圓形|
pygame.draw.ellipse()
| 繪製一個橢圓形|
pygame.draw.arc()
| 繪製弧線(揮橢圓的一部分)|
pygame.draw.line()
| 繪製線段(直線)|
pygame.draw.lines()
| 繪製多條連續的線段|
pygame.draw.aaline()
| 繪製一條平滑的線段(抗鋸齒)|
#pygame.draw.aalines()
| 繪製多條連續的線段
pygame.color
物件
- 三元組
-
#RGBA 四元組 下面通對上述一些方法的參數進行詳細說明:
繪製矩形繪製矩形的語法格式如下:
<pre class='brush:php;toolbar:false;'>pygame.draw.rect(surface, color, rect, width)</pre>
參數說明如下:surface
:指主遊戲窗口,無特殊情況,通常會在主畫面上繪製;
rectcolor
:此參數用於此圖形著色;
- :繪製圖形的位置和尺寸大小;
-
width :可選參數,指定邊框的寬度,預設為0,表示填滿該矩形區域。
繪製多邊形
pygame.draw.polygon(surface, color, points, width)
- 參數說明如下
-
#points : 一個清單參數,它表示組成多邊形頂點的3 或多個(x,y) 座標,透過元組或列表來表示這些多邊形頂點。 其餘參數與上述函數相同。
-
繪製圓形
pygame.circle(surface, color, pos, radius, width=0)
參數說明如下
# :此參數用來指定的圓心位置;
- radius :用來指定圓的半徑;
- 繪製橢圓形
pygame.draw.ellipse(surface, color, Rect, width=0)
繪製橢圓形的過程,其實就是在矩形區域內部(Rect)繪製一個內接橢圓形
參數說明如下
其餘參數與上述函數相同。
繪製圓弧曲線pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)
繪製橢圓形的過程,其實就是在矩形區域內部(Rect)繪製一個內接橢圓形
參數說明如下
start_angle
: 是該段圓弧的起始角度;-
:是終止角度;stop_angle
其餘參數與上述函數相同。
- 繪製直線
pygame.draw.line(surface, color, start_pos, end_pos, width=1)
參數說明如下
#start_pos
: 是該線段的開始位置;- end_pos
- : 是該線段的結束位置;;
其餘參數與上述函數相同。 -
如果是繪製一條消除鋸齒的平滑線,此時則使用blend = 1 參數,如下所示:
pygame.aaline(surface, color, startpos, endpos, blend=1)
blend 參數表示透過繪製混合背景的陰影來實現抗鋸齒功能。 - 繪製多直線參數說明如下
#pointlist
: 參數值為列表,包含了一些列點座標的列表;#########closed ###: 布林值參數,如果設定為True,表示直線的第一個端點和直線的最後一個端點要首尾相連;; ############其餘參數與上述函數相同。 ############如果繪製抗鋸齒直線,使用下列方法:###pygame.draw.aalines(surface, color, closed, pointlist, blend=1)###除了指定了 blend = 1 之外,其餘參數意義與上述函數相同。 ######下面透過一組簡單的範例對上述繪圖方法進行示範:###
import pygame from math import pi # 初始化 pygame.init() # 设置主屏幕大小 size = (500, 450) screen = pygame.display.set_mode(size) # 设置标题 pygame.display.set_caption("Python自学网") # 设置一个控制主循环的变量 done = False # 创建时钟对象 clock = pygame.time.Clock() while not done: # 设置游戏的fps clock.tick(10) for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # 若检测到关闭窗口,则将done置为True # 绘制一条宽度为 3 的红色对角线 pygame.draw.line(screen, (0, 255, 0), [0, 0], (500, 450), 3) # 绘制多条蓝色的直线(连续直线,非抗锯齿),False 表示首尾不相连 pygame.draw.lines(screen, (0, 0, 255), False, [[0, 80], [50, 90], [200, 80], [220, 30]], 1) # 绘制一个灰色的矩形区域,以灰色填充区域 pygame.draw.rect(screen, (155, 155, 155), (75, 10, 50, 20), 0) # 绘制一个线框宽度为2的矩形区域 pygame.draw.rect(screen, (0, 0, 0), [150, 10, 50, 20], 2) # 绘制一个椭圆形,其线宽为2 pygame.draw.ellipse(screen, (255, 0, 0), (225, 10, 50, 20), 2) # 绘制一个实心的红色椭圆形 pygame.draw.ellipse(screen, (255, 0, 0), (300, 10, 50, 20)) # 绘制一个绿色边框(宽度为2)三角形 pygame.draw.polygon(screen, (100, 200, 45), [[100, 100], [0, 200], [200, 200]], 2) # 绘制一个蓝色实心的圆形,其中[60,250]表示圆心的位置,40为半径,width默认为0 pygame.draw.circle(screen, (0, 0, 255), [60, 250], 40) # 绘制一个圆弧,其中0表示弧线的开始位置,pi/2表示弧线的结束位置,2表示线宽 pygame.draw.arc(screen, (255, 10, 0), (210, 75, 150, 125), 0, pi / 2, 2) # 刷新显示屏幕 pygame.display.flip() # 点击关闭,退出pygame程序 pygame.quit()
以上是Python之Pygame的Draw繪圖方法怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3漢化版
中文版,非常好用

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能