九宫格,大家都看多了,各种版本都有,但是我觉得,要学就学通用的,只会做九宫格,只是说你懂了这个点,但是通过做九宫格你会灵活切割图片、灵活画出多种格,那你就NB了。
文章目录
好吧,我觉得可以先上个效果图图,让大家感受一下。
一、上效果图
感兴趣吗?那请继续往下看吧!
二、搭建基本框架
先实现m*n的框框布局
(一)核心函数
def divide(start,end,m,n):
bglist = []
y = end[1]-start[1]
x = end[0]-start[0]
width = int(x/m)
height = int(y/n)
i=0
j=0
while i<m:
while j<n:
bglist.append((start[0]+i*width,start[1]+j*height,width-1,height-1))
j += 1
i += 1
j = 0
return bglist
start:开始坐标
end:结束坐标
m,n:表示m行n列
(二)完整代码
import pygame,sys
def divide(start,end,m,n):
bglist = []
y = end[1]-start[1]
x = end[0]-start[0]
width = int(x/m)
height = int(y/n)
i=0
j=0
while i<m:
while j<n:
bglist.append((start[0]+i*width,start[1]+j*height,width-1,height-1))
j += 1
i += 1
j = 0
return bglist
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800,500))
pygame.display.set_caption("豆腐块也有春天V1.0")
clock = pygame.time.Clock()
bglist = divide((0,0),(800,500),10,10)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_F1:
pygame.quit()
sys.exit()
screen.fill((0,0,0))
for bg in bglist:
pygame.draw.rect(screen,(255,0,0),bg)
clock.tick(30)
pygame.display.flip()
(三)运行效果
初始化一个800X600的窗口,然后分成10X10的格子。
起点(0,0),终点(800,600)
初始化一个500X500的窗口,然后分成10X10的格子。
起点(0,0),终点(800,600)
初始化一个800X600的窗口,然后分成10X10的格子。
起点(100,100),终点(700,500)
三、优化框架函数
(一)优化函数
def divide_display(image,m,n,width=0):
select_rect = []
select_rect1 = []
rect = image.get_rect()
stepx = int(rect[2]/m)
stepy = int(rect[3]/n)
size = (rect[2]+(m-1)*width,rect[3]+(n-1)*width)
# bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)
bgsuface = pygame.Surface(size)
bgsuface.fill((255, 255, 255))
i = 0
j = 0
while i<m:
j = 0
while j<n:
tmp_rect = pygame.Rect(i * stepx, j*stepy, stepx, stepy)
select_rect1.append(image.subsurface(tmp_rect).copy())
bgsuface.blit(image.subsurface(tmp_rect).copy(),(tmp_rect[0]+i*width,tmp_rect[1]+j*width))
j += 1
i += 1
select_rect.append(select_rect1)
通过该实现函数divide_display(image,m,n,width=0)
实现对图片的分割,并将分割后的图像绘制在surface上。
(二)保存图像
如果需要保存生成的图像,那么直接调用方法即可:
pygame.image.save(bgsuface,'out.png')
可以直接看看保存的图像效果:
四、图片资源装载
(一)实现函数
def init_image():
path = './user1/'
files = []
dirs = os.listdir(path)
for diretion in dirs:
files.append(path + diretion)
bglist = []
for file in files:
picture = pygame.transform.scale(pygame.image.load(file), (50, 50))
dSurface = picture.convert()
# dSurface = pygame.image.load(file).convert()
bglist.append(dSurface)
# bglist.append(pygame.image.load(file).convert_alpha())
return bglist
(二)调用方法
bglist = []
init_image()
random.choice(bglist)
每次通过随机函数random.choice(bglist)
调用其中的一张图像。
五、主程序介绍
(一)键盘响应事件
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
print(dSurface.get_rect())
(二)随机生成图像
runsurface,size = divide_display(screen,random.choice(bglist),random.randint(2,5),random.randint(2,5),random.randint(1,5))
(三)按照返回的大小显示图像
screen = pygame.display.set_mode(size)
screen.fill((255, 255, 255))
if runsurface is not None:
screen.blit(runsurface,(0,0))
六、完整代码
import pygame
import random
import os
def init_image():
path = './image/'
files = []
dirs = os.listdir(path)
for diretion in dirs:
files.append(path + diretion)
for file in files:
picture = pygame.transform.scale(pygame.image.load(file), (600, 600))
dSurface = picture.convert()
# dSurface = pygame.image.load(file).convert()
bglist.append(dSurface)
def divide_display(image,m,n,width=0):
select_rect = []
select_rect1 = []
rect = image.get_rect()
stepx = int(rect[2]/m)
stepy = int(rect[3]/n)
size = (rect[2]+(m-1)*width,rect[3]+(n-1)*width)
# bg_suface = pygame.Surface((PANEL_width, PANEL_highly), flags=pygame.SRCALPHA)
bgsuface = pygame.Surface(size)
bgsuface.fill((255, 255, 255))
i = 0
j = 0
while i<m:
j = 0
while j<n:
tmp_rect = pygame.Rect(i * stepx, j*stepy, stepx, stepy)
select_rect1.append(image.subsurface(tmp_rect).copy())
bgsuface.blit(image.subsurface(tmp_rect).copy(),(tmp_rect[0]+i*width,tmp_rect[1]+j*width))
j += 1
i += 1
select_rect.append(select_rect1)
pygame.image.save(bgsuface,'out.png')
return bgsuface,size
pygame.init()
size = (500, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption('多彩的N宫格')
fcclock = pygame.time.Clock()
runsurface = None
bglist = []
init_image()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
runsurface,size = divide_display(random.choice(bglist),random.randint(2,5),random.randint(2,5),random.randint(1,5))
screen = pygame.display.set_mode(size)
screen.fill((255, 255, 255))
if runsurface is not None:
screen.blit(runsurface,(0,0))
fcclock.tick(1)
pygame.display.flip()
七、运行效果
每次按下空格键,就会自动生成一张图像,随机生成N宫格。
ok,介绍完了,希望你喜欢。
喜欢就好,欢迎持续关注!