用Python和Pygame写游戏-从入门到精通(7)学习笔记

pygame.draw中函数的第一个参数总是一个surface,然后是颜色,再后会是一系列的坐标等。稍有些计算机绘图经验的人就会知道,计算机里的坐标,(0,0)代表左上角。而返回值是一个Rect对象,包含了绘制的领域,这样你就可以很方便的更新那个部分了。


pygame.draw.rect

用法:pygame.draw.rect(Surface, color, Rect, width=0)

pygame.draw.rect在surface上画一个矩形,除了surface和color,rect接受一个矩形的坐标和线宽参数,如果线宽是0或省略,则填充。我们有一个另外的方法来画矩形——fill方法,如果你还记得的话。事实上fill可能还会快一点点,因为fill由显卡来完成。

pygame.draw.polygon

用法:pygame.draw.polygon(Surface, color, pointlist, width=0)

polygon就是多边形,用法类似rect,第一、第二、第四的参数都是相同的,只不过polygon会接受一系列坐标的列表,代表了各个顶点。

pygame.draw.circle

用法:pygame.draw.circle(Surface, color, pos, radius, width=0)

很简单,画一个圆。与其他不同的是,它接收一个圆心坐标和半径参数。

pygame.draw.ellipse

用法:pygame.draw.ellipse(Surface, color, Rect, width=0)

你可以把一个ellipse想象成一个被压扁的圆,事实上,它是可以被一个矩形装起来的。pygame.draw.ellipse的第三个参数就是这个椭圆的外接矩形。

pygame.draw.arc

用法:pygame.draw.arc(Surface, color, Rect, start_angle, stop_angle, width=1)

arc是椭圆的一部分,所以它的参数也就比椭圆多一点。但它是不封闭的,因此没有fill方法。start_angle和stop_angle为开始和结束的角度。

在surface上绘制椭圆弧。 rect参数是椭圆将填充的区域。 两个角度参数是以弧度表示的初始和最终角度,右侧为零。 宽度参数是绘制外边缘的厚度。

pygame.draw.line

用法:pygame.draw.line(Surface, color, start_pos, end_pos, width=1)

在surface上绘制直线段。 没有末端帽,末端平整为粗线。

pygame.draw.lines

用法:pygame.draw.lines(Surface, color, closed, pointlist, width=1)

closed是一个布尔变量,指明是否需要多画一条线来使这些线条闭合(感觉就和polygone一样了),pointlist是一个点的数组。

上面的表中我们还有aaline和aalines,玩游戏的都知道开出“抗锯齿(antialiasing)”效果会让画面更好看一些,模型的边就不会是锯齿形的了,这两个方法就是在画线的时候做这事情的,参数和上面一样

pygame.draw. aaline ( )
draw fine antialiased lines
aaline(Surface, color, startpos, endpos, blend=1) -> Rect

Draws an anti-aliased line on a surface. This will respect the clipping rectangle. A bounding box of the affected area is returned as a rectangle. If blend is true, the shades will be be blended with existing pixel shades instead of overwriting them. This function accepts floating point values for the end points.

pygame.draw. aalines ( )
draw a connected sequence of antialiased lines
aalines(Surface, color, closed, pointlist, blend=1) -> Rect

Draws a sequence on a surface. You must pass at least two points in the sequence of points. The closed argument is a simple Boolean and if true, a line will be draw between the first and last points. The Boolean blend argument set to true will blend the shades with existing shades instead of overwriting them. This function accepts floating point values for the end points.


我们用一个混杂的例子来演示一下上面的各个方法:

#-*- coding:utf-8 -*-
import pygame
from pygame.locals import *
from sys import exit

from random import *
from math import pi

pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
points = []

while True:

    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        #按任意键清屏
        if event.type == KEYDOWN:
            points = []
            screen.fill((255, 255, 255))
        #如果鼠标被按下
        if event.type == MOUSEBUTTONDOWN:
            screen.fill((255, 255, 255))
            #画随机矩形
            rc = (randint(0, 255), randint(0, 255), randint(0, 255))#随机颜色
            rp = (randint(0, 639), randint(0, 479))#随机左上坐标
            rs = (639 - randint(rp[0], 639), 479-randint(rp[1], 479))#随机右下坐标
            pygame.draw.rect(screen, rc, Rect(rp, rs))
            #画随机圆
            rc = (randint(0, 255), randint(0, 255), randint(0, 255))#随机颜色
            rp = (randint(0, 639), randint(0, 479))#随机圆心
            rr = randint(1, 200)#随机半径
            pygame.draw.circle(screen, rc, rp, rr)
            #得到鼠标的位置
            x, y = pygame.mouse.get_pos()
            #添加点
            points.append((x, y))
            #角度随鼠标移动改变
            angle = (x/639.)*pi*2
            #根据点击位置画弧线
            pygame.draw.arc(screen, (0, 0, 0), (0, 0, 639, 479), 0, angle, 3)
            #根据点击位置画椭圆
            pygame.draw.ellipse(screen, (0, 255, 0), (0, 0, x, y))
            #从左上和右下画两根线连接到点击位置
            pygame.draw.line(screen, (0, 0, 255), (0, 0), (x, y))
            pygame.draw.line(screen, (255, 0, 0), (640, 480), (x, y))
            #画点击轨迹图
            if len(points) > 1:
                pygame.draw.lines(screen, (155, 155, 0), False, points, 2)
            #将点画成小圆(更明显)
            for p in points:
                pygame.draw.circle(screen, (155, 155, 155), p, 3)
    #刷新
    pygame.display.update()

效果图:



猜你喜欢

转载自blog.csdn.net/qq_41805514/article/details/80641754
今日推荐