pygame를 통해 좌표계, 그림 그리기, 선 그리기, 점 그리기, 글씨 쓰기, 캡처(코드) 그리기

3028 단어 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()

글꼴 라이브러리는 윈도우즈가 자체로 가지고 있는 것을 복사할 수 있습니다.ttf 파일
C:\Windows\Fonts

좋은 웹페이지 즐겨찾기