日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python使用draw類繪制圖形示例講解_python

作者:acktomas ? 更新時(shí)間: 2022-09-30 編程語言

視頻

觀看視頻

Pygame模塊之pygame.draw

本文將主要介紹Pygame的draw模塊,主要內(nèi)容翻譯自pygame的官方文檔

pygame.draw 模塊用于在Surface上繪制一些簡單的圖形,比如點(diǎn)、直線、矩形、圓、弧等。

pygame.draw中函數(shù)的第一個(gè)參數(shù)總是一個(gè)surface,然后是顏色,再后會是一系列的坐標(biāo)等。稍有些計(jì)算機(jī)繪圖經(jīng)驗(yàn)的人就會知道,計(jì)算機(jī)里的坐標(biāo),(0,0)代表左上角。而返回值是一個(gè)Rect對象,包含了繪制的領(lǐng)域,這樣你就可以很方便的更新那個(gè)部分了。

先從整體來看pygame.draw有哪些函數(shù):

  • pygame.draw.rect:繪制矩形
  • pygame.draw.polygon:繪制任意邊數(shù)的多邊形
  • pygame.draw.circle:繪制圓
  • pygame.draw.ellipse:在矩形內(nèi)繪制橢圓
  • pygame.draw.arc:繪制圓弧(或者橢圓的一部分)
  • pygame.draw.line:繪制直線(線段)
  • pygame.draw.lines:從一個(gè)點(diǎn)列表中連續(xù)繪制直線段
  • pygame.draw.aaline:繪制一根平滑的線(反鋸齒)
  • pygame.draw.aalines:繪制一系列平滑的線

大多數(shù)函數(shù)接受一個(gè)width參數(shù)表示線條(畫筆)的寬度,如果該值設(shè)置為0,則表示填充整個(gè)圖形。

所有的繪制函數(shù)都會尊重指定的Surface編輯區(qū),而且會限制在這個(gè)區(qū)域內(nèi)。函數(shù)的返回值是一個(gè)Rect,表示的是受影響的Surface區(qū)域。(原文:All the drawing functions respect the clip area for the Surface, and will be constrained to that area. The functions return a rectangle representing the bounding area of changed pixels.)

顏色參數(shù)通常是一個(gè)RGB三元組(R, G, B)。也可以接受RGBA形式的顏色值。

這些繪制函數(shù)會臨時(shí)鎖定所操作的Surface對象。

pygame.draw.rect

原型:pygame.draw.rect(Surface, color, Rect, width=0): return Rect

用途:在Surface上繪制矩形,color參數(shù)是線條(或填充)的顏色,參數(shù)Rect的形式是((x, y), (width, height)),表示的是所繪制矩形的區(qū)域,其中第一個(gè)元組(x, y)表示的是該矩形左上角的坐標(biāo),第二個(gè)元組 (width, height)表示的是矩形的寬度和高度。width表示線條的粗細(xì),單位為像素;默認(rèn)值為0,表示填充矩形內(nèi)部。

此外,Surface.fill 同樣可以用來繪制填充矩形。

pygame.draw.polygon

原型:pygame.draw.polygon(Surface, color, pointlist, width=0): return Rect

用途:polygon是多邊形,這個(gè)函數(shù)和rect類似,除了第三個(gè)參數(shù)。顧名思義,pointlist是一個(gè)坐標(biāo)點(diǎn)的列表,表示多邊形的各個(gè)頂點(diǎn)。

pygame.draw.circle

原型:pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect

用途:用于繪制圓形。第三個(gè)參數(shù)pos是圓心的位置坐標(biāo),radius指定了圓的半徑。

pygame.draw.ellipse

原型:pygame.draw.ellipse(Surface, color, Rect, width=0): return Rect

用途:ellipse是橢圓形,這個(gè)函數(shù)在矩形 Rect 內(nèi)部繪制一個(gè)內(nèi)接橢圓。

pygame.draw.arc

原型:pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1): return Rect

用途:繪制一段圓弧,或者其實(shí)是上面提到的橢圓的一部分。與ellipse函數(shù)相比,多了兩個(gè)參數(shù):start_angle是該段圓弧的起始角度,stop_angle是終止角度。這兩個(gè)都是用弧度制來表示的,而原點(diǎn)就是矩形Rect的中心。在Rect平面上建立坐標(biāo)系,原點(diǎn)是中心,簡單示意圖如下。0弧度的起點(diǎn)是右邊的中點(diǎn)處。

pygame.draw.line

原型:pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect

用途:繪制直線段,start_pos 和 end_pos 分別表示起始點(diǎn)和終止點(diǎn),用坐標(biāo)表示。width為線條寬度,默認(rèn)為1. 線條兩端自然結(jié)束,沒有明顯的端點(diǎn)(如實(shí)心黑點(diǎn))。

pygame.draw.lines

原型:pygame.draw.lines(Surface, color, closed, pointlist, width=1): return Rect

用途:用于繪制一系列直線段。closed是一個(gè)布爾變量,如果closed為真,那么表示需要把第一點(diǎn)和最后一點(diǎn)連接起來。這些點(diǎn)來自pointlist,一個(gè)包含坐標(biāo)點(diǎn)的列表。這個(gè)函數(shù)不會繪制線條的端點(diǎn),也沒有斜角連接(miter joints),而且角度小和線條粗的連線看起來會有點(diǎn)奇怪( Lines with sharp corners and wide line widths can have improper looking corners.)。

pygame.draw.aaline

原型:pygame.draw.aaline(Surface, color, startpos, endpos, blend=1): return Rect

用途:繪制一條平滑的(消除鋸齒)直線段。

pygame.draw.aalines

原型:pygame.draw.aalines(Surface, color, closed, pointlist, blend=1): return Rect

用途:繪制連續(xù)的抗鋸齒線段。該函數(shù)還有上面的aaline的用法和前兩個(gè)類似。

示例1

繪制圖形時(shí),函數(shù)使用方法舉例。

import pygame
from math import pi
#初始化
pygame.init()
# 設(shè)置主屏幕大小
size = (500, 450)
screen = pygame.display.set_mode(size)
#設(shè)置標(biāo)題
pygame.display.set_caption("小小工坊")
# 設(shè)置一個(gè)控制主循環(huán)的變量
done = False
#創(chuàng)建時(shí)鐘對象
clock = pygame.time.Clock()
while not done:
    # 設(shè)置游戲的fps
    clock.tick(10)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True  # 若檢測到關(guān)閉窗口,則將done置為True
    # 繪制一條寬度為 3 的紅色對角線
    pygame.draw.line(screen, (0, 255, 0), [0, 0], (500, 450), 3)
    # 繪制多條藍(lán)色的直線(連續(xù)直線,非抗鋸齒),F(xiàn)alse 表示首尾不相連
    pygame.draw.lines(screen, (0, 0, 255), False, [[0, 80], [50, 90], [200, 80], [220, 30]], 1)
    # 繪制一個(gè)灰色的矩形區(qū)域,以灰色填充區(qū)域
    pygame.draw.rect(screen, (155, 155, 155), (75, 10, 50, 20), 0)
    # 繪制一個(gè)線框?qū)挾葹?的矩形區(qū)域
    pygame.draw.rect(screen, (0, 0, 0), [150, 10, 50, 20],2)
    # 繪制一個(gè)橢圓形,其線寬為2
    pygame.draw.ellipse(screen, (255, 0, 0), (225, 10, 50, 20), 2)
    # 繪制一個(gè)實(shí)心的紅色橢圓形
    pygame.draw.ellipse(screen, (255, 0, 0), (300, 10, 50, 20))
    # 繪制一個(gè)綠色邊框(寬度為2)三角形
    pygame.draw.polygon(screen, (100, 200, 45), [[100, 100], [0, 200], [200, 200]], 2)
    # 繪制一個(gè)藍(lán)色實(shí)心的圓形,其中[60,250]表示圓心的位置,40為半徑,width默認(rèn)為0
    pygame.draw.circle(screen, (0, 0, 255), [60, 250], 40)
    # 繪制一個(gè)圓弧,其中0表示弧線的開始位置,pi/2表示弧線的結(jié)束位置,2表示線寬
    pygame.draw.arc(screen, (255, 10, 0), (210, 75, 150, 125), 0, pi / 2, 2)
    # 刷新顯示屏幕
    pygame.display.flip()
# 點(diǎn)擊關(guān)閉,退出pygame程序
pygame.quit()

示例2

繪制臉部圖形

import pygame, sys
from math import pi
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption("pygame 繪圖")
GOLD = 255, 251, 0
RED = pygame.Color('red')
WHITE = 255, 255, 255
GREEN = pygame.Color('green')
e1rect = pygame.draw.ellipse(screen,GREEN, (50,50,500,300),3)
c1rect = pygame.draw.circle(screen, GOLD, (200, 180), 30, 5)
c2rect = pygame.draw.circle(screen, GOLD,(400, 180), 30, 5)
r1rect = pygame.draw.rect(screen, RED, (170, 130, 60, 10), 3)
r2rect = pygame.draw.rect(screen, RED, (370, 130, 60, 10))
plist = [(295,170), (285,250),(260,280),(340,280),(315,250),(305,170)]
l1rect = pygame.draw.lines(screen,GOLD,True,plist,2)
a1rect = pygame.draw.arc(screen,RED,(200,220,200,100), 1.4*pi, 1.9*pi, 3)
while True:
	for e in pygame.event.get():
		if e.type == pygame.QUIT:
			pygame.quit()
			sys.exit()
	pygame.display.update()

原文鏈接:https://acktomas.blog.csdn.net/article/details/125856587

欄目分類
最近更新