通过pygame 画坐标系 、画图、画线、画点、写字、截图(代码)

import os
import sys

import pygame

class Draw:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        pygame.init()
        pygame.display.set_caption('hello world')
        self.screen = pygame.display.set_mode([self.x, self.y])
        self.screen.fill([255, 255, 255])

    def drawText(self, text, posx, posy, textHeight=15, fontColor=(0, 0, 0), backgroudColor=(255, 255, 255)):
        ttf_abs = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res', 'demo.ttf')
        fontObj = pygame.font.Font(ttf_abs, textHeight)  # 通过字体文件获得字体对象
        textSurfaceObj = fontObj.render(text, True, fontColor, backgroudColor)  # 配置要显示的文字
        textRectObj = textSurfaceObj.get_rect()  # 获得要显示的对象的rect
        textRectObj.center = (posx, posy)  # 设置显示对象的坐标
        self.screen.blit(textSurfaceObj, textRectObj)  # 绘制字

    def draw_info(self):
        color = 0, 0, 0
        width = 2
        # 画坐标系
        pygame.draw.line(self.screen, color, (0, 0), (self.x, 0), width)
        pygame.draw.line(self.screen, color, (0, 0), (0, self.y), width)
        pygame.draw.line(self.screen, color, (self.x, 0), (self.x - 10, 10), width)
        pygame.draw.line(self.screen, color, (0, self.y), (10, self.y - 10), width)
        x_list = []
        y_list = []
        self.drawText(str(0), 10, 10, textHeight=12)
        self.drawText(str(self.x), self.x - 10, 14, textHeight=12)
        self.drawText(str(self.y), 15, self.y - 10, textHeight=12)
        n = 5  # 轴分为n段
        for i in range(1, n):
            x_list.append(int(self.x / n * i))
            y_list.append(int(self.y / n * i))
        for x_item in x_list:
            pygame.draw.line(self.screen, color, (x_item, 0), (x_item, 10), width)
            self.drawText(str(x_item), x_item, 12, textHeight=15)
        for y_item in y_list:
            pygame.draw.line(self.screen, color, (0, y_item), (10, y_item), width)
            self.drawText(str(y_item), 16, y_item, textHeight=14)
        # 加载画图
        pic_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'res', 'a.jpg')
        space = pygame.image.load(pic_path).convert_alpha()
        # 获取位图的宽和高
        width, height = space.get_size()
        # 对图片进行缩放
        space = pygame.transform.smoothscale(space, (width // 5, height // 5))
        # 对图片进行设置位置
        self.screen.blit(space, (100, 100))
        # 画点(其实是画圆)
        pygame.draw.circle(self.screen, color, (300,500), 1, 1)
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
            pygame.display.update()
            pygame.image.save(self.screen, "circle" + ".png")  # 这句话保存图片
            pygame.quit()
            break

    def run(self):
        self.draw_info()


if __name__ == '__main__':
    x = 540
    y = 960
    Draw(x, y).draw_info()

字体库可以复制windows自带的.ttf文件

C:\Windows\Fonts

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值