python实战 - 从零手搓三国杀(2)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

前言

一、图片导入

二、创建矩形

三、创建边框

总结


前言

上一次做了Image类,现在基于Image类可以完成更多操作


一、图片导入

pygame有一个pygame.image.load()函数,可以通过路径将图片导入

def imageLoad(path,size = None,width = None,height = None):
    surface = pygame.image.load(path)
    return Image(surface,size=size,width=width,height=height)

 该方法接收路径引入图片,同时对图片进行缩放

二、创建矩形

在很多时候,尤其是多个图片组合而成的图片,创建一个矩形做背景是非常方便的

def createTansparent(size,rbga = (0,0,0,0)):
    surface = pygame.Surface(size, pygame.SRCALPHA) 
    surface.fill(rbga)
    return Image(surface,size=size)

该方法可以创建一个 矩形,默认是一个透明矩形

三、创建边框

def createFrame(image,color,size):
    surface = createTansparent(image.rect.inflate(2*size,2*size).size)
    image.draw(surface.image,pos=(size,size))
    pygame.draw.rect(surface.image, color,surface.rect, size)
    return surface

该函数接收三个参数,image对象、边框色、边框粗细

创建一个比image对象大一点的透明矩形,长和宽各加2倍的边框粗细

随后将image对象画在矩形中央,边框画在矩形(0,0)点


总结

以上就是今天要讲的内容,本文完成了导入图片、创建矩形和边框的操作

你可能感兴趣的:(python,pygame)