python小游戏编程arcade----坦克动画图片合成

  • 前言
  • 坦克动画图片合成
    • 1、PIL image
      • 1.1 读取文件并转换
      • 1.2 裁切,粘贴
      • 1.3 效果图
      • 1.4 代码实现
    • 2、处理图片的透明度问题
      • 2.1 past 函数的三个参数
      • 2.2 注意点1
      • 2.3 注意点2
      • 2.4 效果![在这里插入图片描述](https://img-blog.csdnimg.cn/2079ef0b307a417fbe18bd501af46da9.png)
      • 2.4 代码实现

前言

接上篇文章继续解绍arcade游戏编程的基本知识。如何通过程序合成所需的动画图片

坦克动画图片合成

游戏素材

如何通过程序合成所需的动画图片

1、PIL image

1.1 读取文件并转换

from PIL import Image
img = Image.open(“images/tank.png”).convert(“RGBA”) #读取系统的内照片

1.2 裁切,粘贴

    def crop(self, box=None):"""Returns a rectangular region from this image. The box is a4-tuple defining the left, upper, right, and lower pixelcoordinate. See :ref:`coordinate-system`.Note: Prior to Pillow 3.4.0, this was a lazy operation.:param box: The crop rectangle, as a (left, upper, right, lower)-tuple.:rtype: :py:class:`~PIL.Image.Image`:returns: An :py:class:`~PIL.Image.Image` object."""if box is None:return self.copy()if box[2] < box[0]:raise ValueError("Coordinate 'right' is less than 'left'")elif box[3] < box[1]:raise ValueError("Coordinate 'lower' is less than 'upper'")self.load()return self._new(self._crop(self.im, box))

crop函数带的参数为(起始点的横坐标,起始点的纵坐标,宽度,高度)
paste函数的参数为(需要修改的图片,粘贴的起始点的横坐标,粘贴的起始点的纵坐标)

1.3 效果图

1.4 代码实现

from PIL import Image
import colorsysi = 1
j = 1
img = Image.open("images/tank.png").convert("RGBA") #读取系统的内照片box=(0,179,185,256)
pp = img.crop(box)
box=(0,132,175,179)
pp2 = img.crop(box)
print(pp2.size)
pp2.show()
nimg= Image.new("RGBA",(200,200))
nimg.paste(pp)
nimg.show()
# nimg.putalpha(pp2)
nimg.paste(pp2,(5,47,180,94))nimg.show()

透明度不对

2、处理图片的透明度问题

2.1 past 函数的三个参数

    def paste(self, im, box=None, mask=None):"""Pastes another image into this image. The box argument is eithera 2-tuple giving the upper left corner, a 4-tuple defining theleft, upper, right, and lower pixel coordinate, or None (same as(0, 0)). See :ref:`coordinate-system`. If a 4-tuple is given, the sizeof the pasted image must match the size of the region.If the modes don't match, the pasted image is converted to the mode ofthis image (see the :py:meth:`~PIL.Image.Image.convert` method fordetails).Instead of an image, the source can be a integer or tuplecontaining pixel values.  The method then fills the regionwith the given color.  When creating RGB images, you canalso use color strings as supported by the ImageColor module.If a mask is given, this method updates only the regionsindicated by the mask. You can use either "1", "L", "LA", "RGBA"or "RGBa" images (if present, the alpha band is used as mask).Where the mask is 255, the given image is copied as is.  Wherethe mask is 0, the current value is preserved.  Intermediatevalues will mix the two images together, including their alphachannels if they have them.See :py:meth:`~PIL.Image.Image.alpha_composite` if you want tocombine images with respect to their alpha channels.:param im: Source image or pixel value (integer or tuple).:param box: An optional 4-tuple giving the region to paste into.If a 2-tuple is used instead, it's treated as the upper leftcorner.  If omitted or None, the source is pasted into theupper left corner.If an image is given as the second argument and there is nothird, the box defaults to (0, 0), and the second argumentis interpreted as a mask image.:param mask: An optional mask image."""if isImageType(box) and mask is None:# abbreviated paste(im, mask) syntaxmask = boxbox = Noneif box is None:box = (0, 0)if len(box) == 2:# upper left corner given; get size from image or maskif isImageType(im):size = im.sizeelif isImageType(mask):size = mask.sizeelse:# FIXME: use self.size here?raise ValueError("cannot determine region size; use 4-item box")box += (box[0] + size[0], box[1] + size[1])if isinstance(im, str):from . import ImageColorim = ImageColor.getcolor(im, self.mode)elif isImageType(im):im.load()if self.mode != im.mode:if self.mode != "RGB" or im.mode not in ("LA", "RGBA", "RGBa"):# should use an adapter for this!im = im.convert(self.mode)im = im.imself._ensure_mutable()if mask:mask.load()self.im.paste(im, box, mask.im)else:self.im.paste(im, box)

2.2 注意点1

原图读取时要有透明度层的数据

img = Image.open(“images/tank.png”).convert(“RGBA”) #读取系统的内照片

2.3 注意点2

pp2 = img.crop(box)
#分离通道
r, g, b, a = pp2.split()
#粘贴要加mask
nimg.paste(pp2,(5,47,180,94),mask=a)

2.4 效果

微调数据

2.4 代码实现

# _*_ coding: UTF-8 _*_
# 开发团队: 信息化未来
# 开发人员: Administrator
# 开发时间:2022/11/30 20:17
# 文件名称: 图片合成.py
# 开发工具: PyCharmfrom PIL import Image
import colorsysi = 1
j = 1
img = Image.open("images/tank.png").convert("RGBA") #读取系统的内照片box=(0,179,185,256)
pp = img.crop(box)
print(pp.size)
box=(0,132,175,179)
pp2 = img.crop(box)
r, g, b, a = pp2.split()
print(pp2.size)
pp2.show()
nimg= Image.new("RGBA",(200,200))
nimg.paste(pp,(0,123,185,200))
nimg.show()
# nimg.putalpha(pp2)
nimg.paste(pp2,(0,153,175,200),mask=a)nimg.show()

今天是以此模板持续更新此育儿专栏的第 39/50次。
可以关注我,点赞我、评论我、收藏我啦。

python小游戏编程arcade----坦克动画图片合成相关推荐

  1. python小游戏编程实例-10分钟教你用Python写一个贪吃蛇小游戏,适合练手项目

    另外要注意:光理论是不够的.这里顺便总大家一套2020最新python入门到高级项目实战视频教程,可以去小编的Python交流.裙 :七衣衣九七七巴而五(数字的谐音)转换下可以找到了,还可以跟老司机交 ...

  2. python小游戏——怀念经典坦克大战代码

    ♥️作者:小刘在这里 ♥️每天分享云计算网络运维课堂笔记,努力不一定有回报,但一定会有收获加油!一起努力,共赴美好人生! ♥️夕阳下,是最美的,绽放,愿所有的美好,再疫情结束后如约而至. 目录 一.效 ...

  3. python小游戏编程实例-Python实现的弹球小游戏示例

    本文实例讲述了Python实现的弹球小游戏.分享给大家供大家参考,具体如下: 弹球 1. Ball 类 draw负责移动Ball 碰撞检测,反弹,Ball检测Paddle 2.Paddle类 draw ...

  4. python 小游戏编程_python 编程 小游戏(原创)

    # -*- coding:utf-8 -*- __author__ = 'Atlantis' '''现在有10个好玩的小游戏还在持续更新中,敬请期待-''' import random '''形状.图 ...

  5. python小游戏编程100例_经典编程100例——python版(例9例10)

    最近事情比较多,python还在学习之中,更新速度慢了一些.今天就2例. 例9:一个数如果恰好等于它的因子之和,这个数就称为"完数".如6=1+2+3.编程找出1000之内所有的完 ...

  6. 寓教于乐——PyGame游戏编程,Python小游戏制作实战教学

    Python非常受欢迎的一个原因是它的应用领域非常广泛,其中就包括游戏开发.而是用Python进行游戏开发的首选模块就是PyGame. 1. 初识Pygame PyGame是跨平台Python模块,专 ...

  7. Python小游戏-坦克大战(tank war)

    Python小游戏-坦克大战(tank war) 前言 这款游戏一直都是我很喜欢的游戏,很童年,太经典啦!也很好玩,所以我来做一做这款游戏. 开发工具 python版本:3.7.3 相关模块:pyga ...

  8. python画图、python小游戏、python刷题、python算法、python编程与数学

    python画图系列整理 python画图系列整理-CSDN博客 python游戏入门书籍推荐 python游戏入门书籍推荐_pygame书籍推荐_dllglvzhenfeng的博客-CSDN博客 p ...

  9. python小游戏代码200行左右,python编程小游戏代码

    大家好,本文将围绕python小游戏代码200行左右展开说明,小游戏程序代码python是一个很多人都想弄明白的事情,想搞清楚python编程小游戏代码需要先了解以下几个事情. 1.python简单小 ...

最新文章

  1. AD恢复(3)使用AD回收站
  2. MySQL not exists 真的不走索引么?
  3. ReactNative学习笔记(一)环境搭建
  4. V-3-3 在没有vCenter的情况下,复制虚拟机
  5. hook NSArray 方法在debug模式下会崩溃, 在release模式下会返回nil
  6. notes java api_如何使用Java来调用Notes API发送邮件(包括附件)
  7. sysV init服务脚本(入门级)
  8. 在数据库中如果组合主键(假设为stuID和stuName)存在则更新,不存在则新增
  9. matlab闰年问题,MATLAB中文上机作业.pdf
  10. PythonStock(37)股票系统:Python股票系统发布V2.0版本,改个名字吧,叫Python全栈股票系统2.0,可以实现数据的抓取(akshare),统计分析,数据报表展示。
  11. Python爬虫实战:爬取贝壳网二手房成交数据,将数据存入Excel。
  12. node基础知识部分小记
  13. 查看电脑配置命令_注册表
  14. java中extends用法_JAVA的extends用法
  15. Num.01- java 之 mybatis 框架
  16. 使用 Live Transcribe 进行实时连续转录
  17. IDEA启动Web项目后,在Tomcat中的webapp文件夹下找不到?
  18. 只要一步就让WorkNC导出应用至UG等不同CAM数控软件的残留毛坯
  19. 精灵骑士二觉_DNF精灵骑士二觉大地女神技能介绍 精灵骑士二觉改动详解
  20. Python爬虫:自动化下载海报

热门文章

  1. mysql 5.6 插入表情符
  2. 基于Java实现的Android拼图游戏设计
  3. 后端知识点链接(二):操作系统、Linux
  4. poj1386 Paly onWords
  5. 2021-10-11 今日总结
  6. linux挂载ntfs硬盘6,CentOS 6.2 挂载 NTFS格式的硬盘
  7. Frontiers in Pharmacology2020 | MOSES+:分子生成模型的benchmark平台
  8. 微信测试号中被动消息回复的测试
  9. xposed定位插件_模拟位置xposed
  10. oracle 字符串中数字转中文大写,金额钱数转中文大写