当前位置:   article > 正文

Pygame(五)画线_pygame 画线

pygame 画线

Pygame(五)画线

前情提要

image.png

作业代码

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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

内容提要

image.png

内容详解

画线段

pygame.draw.line(Surface, color, startpos, endpos, width)
  • 1
参数解析
  • 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()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

示例图形:
image.png

画折线(多条连续的线段)

pygame.draw.lines(Surface, color, closed, pointlist, width)
  • 1
参数解析
  • 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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
示例图形

image.png

画反锯齿线段,折线段与多边形

pygame.draw.aaline(Surface, color, startpos, endpos, width)
pygame.draw.aalines(Surface, color, closed, pointlist, width)
pygame.draw.polygon(Surface, color, pointlist, width)
  • 1
  • 2
  • 3

这边就不详细解释.用法了,除了函数名多了两个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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
效果图

image.png

练习:

尝试画出如下组合图形
image.png

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/154447
推荐阅读
相关标签
  

闽ICP备14008679号