Pygame的绘制功能-像素细节

时间:2019-06-22 14:29:21

标签: python pygame draw pixel rect

  1. pygame的Rect几何定义(左,右,顶部,底部,宽度,高度)如何在屏幕上绘制像素?

  2. 宽度如何影响pygame.draw.rect()

1 个答案:

答案 0 :(得分:0)

使用下面的代码,您可以使用pygame.draw并逐像素分析结果。因此,您需要绘制在一个较小的表面上,该表面会按比例放大,然后在屏幕上变灰。

问题1:

Rect由其左,上和其度量来定义    宽度长度。使用pygame.draw.rect(表面,颜色,矩形,    width = 0),则图形的左上像素将具有    (左,上)。像素的宽度方向上的像素数    绘图等于矩形的宽度,因此宽度    最后一个像素的坐标为(左+宽度-1)。高度也一样。因此,右下像素的坐标为(左+宽度-1,上+高度-1)

TLDR:

  • Rect.left返回绘制的矩形的左列像素的x坐标
  • Rect.top返回绘制的矩形顶行像素的y坐标
  • Rect.right返回绘制的矩形右下角的像素列的x坐标
  • Rect.bottom返回绘制的矩形下的下一行像素的x坐标
#draw onto the small surface here

    #=================================================
    rect = pg.Rect(6, 6, 6, 6)
    pg.draw.rect(surface_small, orange, rect, 0)

    rect = pg.Rect(18, 6, 7, 7)
    pg.draw.rect(surface_small, orange, rect, 0)
    #=================================================

Rectangle example picture

问题2:

绘制矩形的宽度定义了绘制线的宽度。宽度为1(像素)的结果是线条,该线条与width = 0(填充的矩形)绘制的同一矩形的轮廓完全匹配。宽度> 1会使这些线变粗。绘制的矩形变得比原始矩形大,并且出现“缺少角”。

宽度不均匀:


    #draw onto the small surface here
    #=================================================
    rect = pg.Rect(6, 6, 12, 12)

    pg.draw.rect(surface_small, orange, rect, 5)
    pg.draw.rect(surface_small, black, rect, 1)
    #=================================================

Uneven width example

甚至宽度:


    #draw onto the small surface here
    #=================================================
    rect = pg.Rect(6, 6, 12, 12)

    pg.draw.rect(surface_small, orange, rect,4 )
    pg.draw.rect(surface_small, black, rect, 1)
    #=================================================

Even width example

完整代码:


    import sys, pygame as pg

    pg.init()
    SCREEN = pg.display.set_mode((800, 600))
    pg.display.set_caption('getRect.squareHead')
    CLOCK  = pg.time.Clock()

    white =     (255,   255,    255 )
    black =     (0,     0,      0   )
    grey_light =(200,   200,    200 )
    grey_dark = (127,   127,    127 )

    orange =    (255,   96,     0   )

    #fill a surface pixel by pixel with alternating colours
    def fill_alternating (surface, width, height):
        counter = None

        for i in range(width):

            if i%2 == 0: counter = False
            else: counter = True

            for j in range(height):
                if counter: colour = white
                else: colour = grey_light
                if i%6 == 0 and j%6 == 0: colour = grey_dark
                pg.draw.circle(surface, colour, (i,j), 0, 0)
                counter = not counter            

    #big surface to blit onto the screen
    width_main, height_main = 700, 500
    surface_main = pg.Surface((width_main, height_main))
    surface_main.fill(orange)
    rect_main = surface_main.get_rect(center = (399, 299))

    #small surface to enlarge onto the big surface
    scale = 20
    width_small, height_small = int(width_main/scale), int(height_main/scale)
    surface_small = pg.Surface((width_small, height_small))
    surface_small.fill(white)
    fill_alternating(surface_small, width_small, height_small)

    #draw onto the small surface here
    #=================================================



    #=================================================

    #scale the small surface onto the main surface
    pg.transform.scale(surface_small, (width_main, height_main), surface_main)

    SCREEN.fill((255, 255, 255))

    #blit main surface onto the screen
    SCREEN.blit(surface_main, (rect_main.x, rect_main.y))

    pg.display.update()

    while True:
        for event in pg.event.get():
            if event.type == QUIT:
                pg.quit()
                sys.exit()
        CLOCK.tick(30)