PYGAME - 简单图形绘制

绘制简单的图形:

语法格式:Pygame.draw.XXX(*params)

矩形: rect(Surface, color, Rect, width=0) (Rect是范围,边框=0(填充),1边框大小(向外扩展边框))

多边形: polygan(Surface, color, pointlist, width=0)

圆形: circle(Surface, color, pos, radius, width=0) (pos圆心位置, radius 半径)

椭圆: ellipse(Surface, color, Rect, width=0)(Rect表示一个限定矩形)

弧线: arc(Surface, color, Rect, start_angle, stop_angle, width=1) (不能填充)

线段: line(Surface, color, start_pos, end_pos, widht=1) (只能表示宽度)

         Lines(Surface, color, closed, pointlist, width=1) (closed表示是否闭合,不能填充)

抗锯齿: aaline(Surface, color, startpos, endpos, blend=1)

           aalines(Surface, color, closed, pointlist, blend=1) (blend表示是否开启抗锯齿,pygame2.2后默认都是true)

EG: 绘制一条小鱼儿:

 

 

import pygame
import sys
from pygame.locals import *
import math

size = width, height = 800, 600
bg = (255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Simple Figures')
pos = (50,251)
moving = False

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

        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                moving = True

        if event.type == MOUSEBUTTONUP:
            if event.button == 1:
                moving = False



    screen.fill(bg)
    # drawing a fish
    pygame.draw.rect(screen, (0, 255, 0), (250, 150, 300, 200), 0)
    pygame.draw.aalines(screen, (255,0,0),1,((80,250),(250,150),(250,350)), 1)
    pygame.draw.polygon(screen, (0,0,255),((550,150),(650,350),(650,150),(550,350)), 0)
    pygame.draw.ellipse(screen, (255,0,0), (110, 210, 75, 75), 0)
    pygame.draw.arc(screen, (125,125,125), (250, 150, 30, 200), -math.pi/2, -3 * math.pi / 2, 2 )

    # a moving ball that you can move with left button of mouse
    if moving:
        pos = pygame.mouse.get_pos()
    pygame.draw.circle(screen, (200, 200, 0), pos, 20.0, 0)

    pygame.display.flip()
    clock.tick(120)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值