使用python+pygame模块一波刮刮卡效果,图像处理路径

前言

使用python模拟开始刮开地刮卡效果,让我们开心吧~

效果展示

开发工具

Python版本: 3.6.4

相关模块:

pygame模块;

以及一些python自带的模块。

环境生态

安装 Python 并添加到环境变量,pip 安装需要相关模块。

为了读者们,我想把我最近收藏的一些编程干货分享给大家,回馈给每个读者,希望能帮到你们。

干货主要有:

① 200 多本 Python 电子书(和经典的书籍)应该有

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且可靠的练手项目及源码)

④ Python基础入门、爬虫、网络开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

全部搞定~私信小编01即可获取完整源代码。。

原理简介

很简单。首先从文件夹中常用的一张图片:

def readImageRandomly():

filenames = os.listdir(IMAGEDIR)

filenames = [f for f in filenames if f.split('.')[-1] in SUPPORTEXTS]

imgpath = os.path.join(IMAGEDIR, random.choice(filenames))

return pygame.transform.scale(pygame.image.load(imgpath), SCREENSIZE)

然后将图片绑定到pygame的屏幕上:

screen.blit(image_used, (0, 0))

明确定义一个表面,并设置为灰色,覆盖在图片上,这样的图片一开始是看不到的:

surface = pygame.Surface(SCREENSIZE).convert_alpha()

surface.fill(GRAY)

screen.blit(surface, (0, 0))

检测到我们点击了左手并在屏幕内划线的时候,将划动鼠标的那个键片表面设置为透明的白色图片,因为当它的区域设置过为透明的白色图片时,当它显示为透明的时候:

mouse_event_flags = pygame.mouse.get_pressed()

if mouse_event_flags[0]:

pygame.draw.circle(surface, WHITE, pygame.mouse.get_pos(), 40)

elif mouse_event_flags[-1]:

surface.fill(GRAY)

image_used = readImageRandomly()

为了方便切换图片,我们加了一个点击鼠标,可以随时重新运行图片的功能。组织一下代码,可以正常运行:

def main():

pygame.init()

pygame.mixer.init()

pygame.mixer.music.load(BGMPATH)

pygame.mixer.music.play(-1, 0.0)

pygame.mouse.set_cursor(*pygame.cursors.diamond)

screen = pygame.display.set_mode(SCREENSIZE)

pygame.display.set_caption('刮刮乐——卫星:ilove-python')

surface = pygame.Surface(SCREENSIZE).convert_alpha()

surface.fill(GRAY)

image_used = readImageRandomly()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

sys.exit(-1)

mouse_event_flags = pygame.mouse.get_pressed()

if mouse_event_flags[0]:

pygame.draw.circle(surface, WHITE, pygame.mouse.get_pos(), 40)

elif mouse_event_flags[-1]:

surface.fill(GRAY)

image_used = readImageRandomly()

screen.blit(image_used, (0, 0))

screen.blit(surface, (0, 0))

pygame.display.update()

文章到这里就结束了你的观看,Python 图像,特效下个感谢分享 Python 实现抖音特效的眼睛。

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...