Pygame编程(3)draw模块

draw模块

函数

  • pygame.draw.rect
    • 画一个矩形
    • rect(surface, color, rect) -> Rect
    • rect(surface, color, rect, width=0, border_radius=0, border_top_left_radius=-1,border_top_right_radius=-1, border_bottom_left_radius=-1, border_bottom_right_radius=-1) -> Rect
  • pygame.draw.polygon
    • 画一个多边形
    • polygon(surface, color, points) -> Rect
    • polygon(surface, color, points, width=0) -> Rect
  • pygame.draw.circle
    • 画一个圆形
    • circle(surface, color, center, radius) -> Rect
    • circle(surface, color, center, radius, width=0, draw_top_right=None, draw_top_left=None, draw_bottom_left=None, draw_bottom_right=None) -> Rect
  • pygame.draw.ellipse
    • 画一个椭圆
    • ellipse(surface, color, rect) -> Rect
    • ellipse(surface, color, rect, width=0) -> Rect
  • pygame.draw.arc
    • 画一条椭圆弧
    • arc(surface, color, rect, start_angle, stop_angle) -> Rect
    • arc(surface, color, rect, start_angle, stop_angle, width=1) -> Rect
  • pygame.draw.line
    • 画一条直线
    • line(surface, color, start_pos, end_pos) -> Rect
    • line(surface, color, start_pos, end_pos, width=1) -> Rect
  • pygame.draw.lines
    • 绘制多个连续的直线段
    • lines(surface, color, closed, points) -> Rect
    • lines(surface, color, closed, points, width=1) -> Rect
  • pygame.draw.aaline
    • 画一条直的抗锯齿线
    • aaline(surface, color, start_pos, end_pos) -> Rect
    • aaline(surface, color, start_pos, end_pos, blend=1) -> Rect
  • pygame.draw.aalines
    • 绘制多个连续的直线抗锯齿线段
    • aalines(surface, color, closed, points) -> Rect
    • aalines(surface, color, closed, points, blend=1) -> Rect

实例

python 复制代码
import sys
import math
import pygame
from pygame.locals import *

COLOR_BLACK = (0, 0, 0)
COLOR_WHITE = (255, 255, 255)
COLOR_RED   = (255, 0, 0)
COLOR_GREEN = (0, 255, 0)
COLOR_BLUE  = (0, 0, 255)

pygame.init()

screen = pygame.display.set_mode((700, 600), flags=0)

# 画一条直线
pygame.draw.line(screen, (255, 0, 0), (100, 10), (400, 20), width=5)

# 画一条抗锯齿直线
pygame.draw.aaline(screen, (255, 0, 0), (100, 50), (400, 60), blend=1)

# 画多条连续的直线
pygame.draw.lines(screen, (255, 0, 0), closed=False, points=[[400, 10], [420, 50], [450, 30], [420, 20]], width=1)

# 画多条连续的抗锯齿直线
pygame.draw.aalines(screen, (255, 0, 0), closed=True, points=[[400+60, 10], [420+60, 50], [450+60, 30], [420+60, 20]], blend=1)

# 画圆形, (线宽等于半径 即可画一个实心圆形)
pygame.draw.circle(screen, (255, 0, 0), (50, 80), 30,  5)
pygame.draw.circle(screen, (255, 0, 0), (50, 150), 30, 5, draw_top_left=True)
pygame.draw.circle(screen, (255, 0, 0), (50, 200), 30, 5, draw_top_right=True)
pygame.draw.circle(screen, (255, 0, 0), (50, 250), 30, 5, draw_bottom_left=True)
pygame.draw.circle(screen, (255, 0, 0), (50, 300), 30, 5, draw_bottom_right=True)

# 画矩形
pygame.draw.rect(screen, (255, 0, 0), rect=(100, 80, 100, 100))
pygame.draw.rect(screen, (255, 0, 0), rect=(250, 80, 100, 100), width=1)
pygame.draw.rect(screen, (0, 255, 0), rect=(400, 80, 100, 100), border_radius=15)
pygame.draw.rect(screen, (0, 255, 0), rect=(550, 80, 100, 100), width=2, border_radius=15)
pygame.draw.rect(screen, (0, 255, 0), rect=(100, 200, 100, 100), border_top_left_radius=15)
pygame.draw.rect(screen, (0, 255, 0), rect=(250, 200, 100, 100), border_top_right_radius=15)
pygame.draw.rect(screen, (0, 255, 0), rect=(400, 200, 100, 100), border_bottom_left_radius=15)
pygame.draw.rect(screen, (0, 255, 0), rect=(550, 200, 100, 100), border_bottom_right_radius=15)

# 画多边形
pygame.draw.polygon(screen, (0, 0, 255), points=[[100, 320],[200, 450], [250, 350]], width=0)
pygame.draw.polygon(screen, (0, 0, 255), points=[[250, 320],[350, 450], [400, 350]], width=3)
pygame.draw.polygon(screen, (0, 0, 255), points=[[250+150, 320],[350+150, 450], [400+150, 350], [400+200, 380]], width=3)

# 绘制椭圆
pygame.draw.ellipse(screen, (255, 255, 255), rect=(20, 400, 100, 50), width=0)
pygame.draw.ellipse(screen, (255, 255, 255), rect=(20, 480, 100, 50), width=2)


# 画一段弧形
rect_arc1 = pygame.draw.arc(screen, (255, 255, 0), rect=(100, 500, 100, 80), start_angle=0, stop_angle=(math.pi/180)*270, width=3)
print(rect_arc1)



# 更新显示
pygame.display.flip()
pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        
相关推荐
技术无疆8 小时前
【Python】Uvicorn:Python 异步 ASGI 服务器详解
运维·服务器·开发语言·网络·python·pygame·python3.11
不写八个1 天前
Python编写的贪吃蛇小游戏
开发语言·python·pygame
@技术无疆4 天前
【Python】Flask-Admin:构建强大、灵活的后台管理界面
开发语言·后端·python·oracle·flask·pip·pygame
@技术无疆7 天前
【Python】FeinCMS:轻量级且可扩展的Django内容管理系统
数据库·python·django·sqlite·pip·pygame·httpx
api茶飘香10 天前
如何根据拍立淘API返回值进行商品数据分析
python·数据挖掘·数据分析·django·virtualenv·pygame·tornado
技术无疆13 天前
【Python】Tartiflette:用 Python 实现的 GraphQL 服务器
服务器·开发语言·后端·python·pygame·graphql·python3.11
技术无疆13 天前
【Python】探索Magenta:音乐与艺术的机器智能创作
开发语言·人工智能·python·scikit-learn·pip·pygame·python3.11
技术无疆14 天前
【Python】Anaconda插件:Sublime Text中的Python开发利器
ide·python·编辑器·pip·pygame·sublime text·python3.11
IT小辉同学14 天前
用 Pygame 实现一个乒乓球游戏
python·游戏·pygame
小泽耳机数码推荐家17 天前
百元级蓝牙耳机推荐有哪些?四大优选品牌衷心推荐
python·django·virtualenv·pygame