pygame.draw的使用

2021年2月16日 / 28次阅读 / Last Modified 2021年2月19日
pygame

pygame.draw提供了一些用来画线条的工具,矩形,圆形,椭圆,弧形等等各种形状。

pygame.gfxdraw模块也有类似的接口,不过它们是实验性质的,具体清参考:https://www.pygame.org/docs/ref/gfxdraw.html

下面这段代码,画了几个矩形,并填充了不同的颜色:

import sys
import pygame
from pygame.color import THECOLORS


size = width, height = 800, 600
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame draw rect')
pygame.draw.rect(screen, THECOLORS['blue'], (0,0,200,600))
pygame.draw.rect(screen, THECOLORS['white'], (200,0,200,600))
pygame.draw.rect(screen, THECOLORS['yellow'], (400,0,200,600))
pygame.draw.rect(screen, THECOLORS['red'], (600,0,200,600))


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

pygame.draw.rect画矩形,确定矩形位置大小的是一个四元组,分别是 top-left-x,top-left-y,width,height。

默认情况是画一个全部填充颜色的rect,如果指定边框的width,此时颜色就只对边框起作用,运行下面的测试代码,就能看到效果:

import sys
import random
import pygame
from pygame.color import THECOLORS


size = width, height = 800, 600
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame draw rect')


screen.fill(THECOLORS['blue'])
for i in range(32):
    pygame.draw.rect(screen,
                     THECOLORS['white'],
                     (random.randint(0,800),
                      random.randint(0,600),
                      random.randint(10,500),
                      random.randint(10,500)),
                     random.randint(1,6))


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

下面是画圆(circle)

import sys
import random
import pygame
from pygame.color import THECOLORS


size = width, height = 800, 600
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame draw circle')


screen.fill(THECOLORS['pink'])
pygame.draw.circle(screen, THECOLORS['white'], (100,200),10)
pygame.draw.circle(screen, THECOLORS['white'], (200,300),20,1)
pygame.draw.circle(screen, THECOLORS['white'], (300,400),30)
pygame.draw.circle(screen, THECOLORS['white'], (400,500),40,2)


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

画circle,需要(top-left-x, top-left-y),再加上半径,最后那个参数的含义与rect相同。

下面画椭圆(ellipse)

import sys
import pygame
from pygame.color import THECOLORS


size = width, height = 800, 600
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame draw ellipse')


screen.fill(THECOLORS['lightgreen'])
pygame.draw.ellipse(screen, THECOLORS['white'], (100,200,50,80),1)
pygame.draw.ellipse(screen, THECOLORS['white'], (200,300,20,90))
pygame.draw.ellipse(screen, THECOLORS['white'], (300,400,30,70),2)
pygame.draw.ellipse(screen, THECOLORS['white'], (400,500,100,10))


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

画ellipse,跟画rect很相似,也是需要一个四元组,分别是 top-left-x,top-left-y,width,height。

下面是画线(line)

import sys
import pygame
from pygame.color import THECOLORS


size = width, height = 800, 600
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame draw line')


screen.fill(THECOLORS['gray'])
pygame.draw.line(screen, THECOLORS['white'], (100,200),(50,80),2)
pygame.draw.line(screen, THECOLORS['white'], (200,300),(20,90))
pygame.draw.line(screen, THECOLORS['white'], (300,400),(30,70),10)
pygame.draw.line(screen, THECOLORS['white'], (400,500),(100,10))


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

两点确定一条线,先start_pos,后end_pos,最后是线宽,默认是1,如果是0,这条线就看不见了!

还有一个lines接口,给出多个点,用一段段的直线,将所有的点穿起来:

import sys
import pygame
from pygame.color import THECOLORS


size = width, height = 800, 600
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame draw line')


screen.fill(THECOLORS['gray'])
points = [(100,100),(110,80),(120,70),(130,30),(140,60)]
points2 = [(300,100),(310,80),(320,70),(330,30),(340,60)]
pygame.draw.lines(screen, THECOLORS['white'], True, points)
pygame.draw.lines(screen, THECOLORS['white'], False, points2)


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

第3个参数closed,表示points中的首位两个点,是否要连接起来。

用line和lines接口画出来的线,有很多锯齿,如果不爽,可以使用aaline和aalines接口直接替换,aa就是antialiased的缩写,画出来的线段很平滑!

下面画多边形(polygon)

画多边形跟画lines或aalines很相似,都需要提供多个points:

import sys
import pygame
from pygame.color import THECOLORS


size = width, height = 800, 600
pygame.init()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame draw polygon')


screen.fill(THECOLORS['gray'])
points = [(100,100),(110,80),(120,70),(130,30),(140,60)]
points2 = [(300,100),(310,80),(320,70),(330,30),(340,60)]
pygame.draw.polygon(screen, THECOLORS['white'], points)
pygame.draw.polygon(screen, THECOLORS['green'], points2,2)


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

polygon函数的线宽,与rect一样,如果不是0,就不是填充。

最后还有一个pygame.draw.arc画曲线的接口

这部分对应的官方文档:http://www.pygame.org/docs/ref/draw.html

-- EOF --

本文链接:https://www.pynote.net/archives/3509

留言区

《pygame.draw的使用》有1条留言

您的电子邮箱地址不会被公开。 必填项已用*标注

  • 麦新杰

    画个rect在窗口中跳来跳去:

    import sys
    import random
    import time
    import pygame
    
    
    size = width, height = 800, 600
    speed = [1,1]
    
    pygame.init()
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption('Here is topleft title')
    clock = pygame.time.Clock()
    
    imgrect = pygame.draw.rect(screen, (100,20,30,50), (0,0,100,100))
    
    
    while True:
        # frame rate per second
        clock.tick(240)
        # event list
        for event in pygame.event.get():
            print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
        # adjust speed
        if imgrect.left<0 or imgrect.right>width:
            speed[0] = -speed[0]
        if imgrect.top<0 or imgrect.bottom>height:
            speed[1] = -speed[1]
        # draw and flip
        
        screen.fill((0,100,200))
        imgrect = imgrect.move(speed)
        pygame.draw.rect(screen, (100,20,30,50), imgrect)
        #screen.blit(img, imgrect)
        pygame.display.flip()
        
     [回复]


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top