搜尋
首頁後端開發Python教學Python之Pygame的Draw繪圖方法怎麼使用

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() 
表格中的函數使用方法大同小異,它們都可以在 Surface 對像上繪製一些簡單的形狀,而傳回值是一個Rect 對象,表示實際繪製圖形的矩形區域。上述繪圖函數都提供了一個color 參數,我們可以透過以下三種方式來傳遞color 參數值:

  • pygame.color 物件

##RGB
     三元組
  • #RGBA
  •  四元組
  • 下面通對上述一些方法的參數進行詳細說明:

  • 繪製矩形
  • 繪製矩形的語法格式如下:<pre class='brush:php;toolbar:false;'>pygame.draw.rect(surface, color, rect, width)</pre>參數說明如下:

  • surface :指主遊戲窗口,無特殊情況,通常會在主畫面上繪製;

color

 :此參數用於此圖形著色;

rect
     :繪製圖形的位置和尺寸大小;
  • width
  •  :可選參數,指定邊框的寬度,預設為0,表示填滿該矩形區域。
注意,當 width > 0 時,表示線框的寬度;而 width

繪製多邊形

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中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
可以在Python數組中存儲哪些數據類型?可以在Python數組中存儲哪些數據類型?Apr 27, 2025 am 12:11 AM

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

如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?如果您嘗試將錯誤的數據類型的值存儲在Python數組中,該怎麼辦?Apr 27, 2025 am 12:10 AM

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

Python標準庫的哪一部分是:列表或數組?Python標準庫的哪一部分是:列表或數組?Apr 27, 2025 am 12:03 AM

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

您應該檢查腳本是否使用錯誤的Python版本執行?您應該檢查腳本是否使用錯誤的Python版本執行?Apr 27, 2025 am 12:01 AM

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

在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

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

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

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

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

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

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

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

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

Safe Exam Browser

Safe Exam Browser

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

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

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

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能