Pygame(五)畫線
前情提要

作業代碼
import sys
import pygame
from math import pi
def homework():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
# 畫正方形
rect = (300,200,200,200)
pygame.draw.rect(screen, (0,0,255), rect,5)
# 畫上側的弧
rect = (300, 100, 200, 200)
pygame.draw.arc(screen, (0,255,0), rect, pi, 2*pi, width=5)
# 畫下側的弧
rect = (300, 300, 200, 200)
pygame.draw.arc(screen, (0, 255, 0), rect, 0, pi, width=5)
# 畫左側的弧
rect = (200, 200, 200, 200)
pygame.draw.arc(screen, (0, 255, 0), rect, - pi/2, pi/2, width=5)
# 畫右側弧
rect = (400, 200, 200, 200)
pygame.draw.arc(screen, (0, 255, 0), rect, pi / 2, -pi / 2, width=5)
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
if __name__ == '__main__':
homework()
內容提要

內容詳解
畫線段
pygame.draw.line(Surface, color, startpos, endpos, width)
引數決議
- Surface: Surface物件
- color: 線段顏色
- startpos: 線段的起點坐標, 一個二元組(x,y)
- endpos: 線段的終點坐標, 一個二元組(x,y)
- width: 線條的寬度,默認值為1
示例程式
def draw_line():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
start_pos = 400, 300 # 起點
end_pos = 400, 200 # 終點
RED = (255, 0, 0) # 定義紅色
pygame.draw.line(screen, RED, start_pos, end_pos, 20) # 畫線段
pygame.display.update()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
示例圖形:

畫折線(多條連續的線段)
pygame.draw.lines(Surface, color, closed, pointlist, width)
引數決議
- Surface: Surface物件
- color: 線條顏色
- closed: 是否封閉圖形,布爾型.True:起點與終點會連起來成為一個封閉圖形 ,False:起點與終點不會連起來
- pointlist: 折線各個節點的坐標串列. 元素是二元組的串列
- width: 線寬.默認值為1
示例程式
def draw_lines():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
p_list = [(400, 300), (400, 200), (500, 200), (500, 400)] # 定義各個折點的坐標
RED = (255, 0, 0) # 定義紅色
pygame.draw.lines(screen, RED, True, p_list, 1) # 畫封閉的圖
pygame.display.update() # 更新螢屏
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
示例圖形

畫反鋸齒線段,折線段與多邊形
pygame.draw.aaline(Surface, color, startpos, endpos, width)
pygame.draw.aalines(Surface, color, closed, pointlist, width)
pygame.draw.polygon(Surface, color, pointlist, width)
這邊就不詳細解釋.用法了,除了函式名多了兩個a之外用法與畫線段與畫折線段一樣.
綜合示例
示例代碼
def draw_balance():
pygame.init()
screen = pygame.display.set_mode((800, 600))
screen.fill((255, 255, 255))
RED = (255, 0, 0) # 定義紅色
# 底座三角形三個點
triangle_list = [(400, 300), (350, 350), (450, 350)]
pygame.draw.polygon(screen, RED, triangle_list)
# 秤桿
start_pos = 150, 275
end_pos = 700, 325
# pygame.draw.line(screen, RED, start_pos, end_pos, 3)
pygame.draw.aaline(screen, RED, start_pos, end_pos, 3)
# 畫重物
# 左邊放一個正方形,右邊放一個圓, 因為秤桿是斜的,所以正方形不能直接用rect來畫.選擇用多邊形來畫
# 定義藍色
BLUE = (0, 0, 255)
rect_list = [(150, 275),(200, 280),(208,230), (158, 225)]
pygame.draw.polygon(screen, BLUE, rect_list)
circle_center = 680, 300
r = 22
pygame.draw.circle(screen, BLUE, circle_center, r)
pygame.display.update() # 更新螢屏
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
效果圖

練習:
嘗試畫出如下組合圖形

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/244749.html
標籤:其他
上一篇:簡易彈球游戲
