python 图像mode是什么_就从python开始吧(17)

1f015179849071a0972fb71655d9077f.png

Hello 大家好,我们终于终于终于终于又见面了。这次我托更了,因为。。。懒。。。最近开学了,上完课懒得更,出去完没时间更,总之我托更了,向关注我的朋友们说声:不(qing)好(du)意(cu)思(wo)。这么久不见,不知道大家还记不记得前面学过的东西(反正我是忘的差不多了),反正可以回去看,不打紧不打紧,记得关注就好了哈哈哈哈哈哈。言归正传,开始学习~

上次(遥远的以前),我们学习了借助python中的pygame模块绘制图像和数学函数,不过这些图像都是静态的,actually,pygame也能编辑动态图形。

顺带的,我们先介绍一下pygame也可以直接将图片导入,先把需要导入的图片放到python程序的同一文件夹中。(我复制了一个波动拳的免抠图,搓搓手,期待~)

626e580044c25c6d21e47e57aac45561.gif

然后新建程序,键入代码

import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load("hadoken.png") #加载图像
screen.blit(my_hadoken, [50, 50]) #设定图像位置
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

这里大家可能会遇到一个问题就是,图片加载失败,报错error: Couldn't open hadoken.png

如果出现的话,可以把加载图像的位置补全,并在路径前加r,以上面的举例就是这样

my_hadoken = pygame.image.load("r"C:Python27hadoken.png") #加载图像

应该就可以了,反正我的成功了~出来就是这样的(嘻嘻)。

33f8b8b982a8750cebd57f2cca17cb52.png

下一步,就是要我的波动拳动起来!

6bb973510cc2f12f72f36ea42a7d64ae.png

其实,制作这种小动画并不难,这里面的逻辑就是定义起始和结束位置,然后加入延迟,看到移动过程的发生。上代码

#coding: UTF-8
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load(r"C:Python27hadoken.png") #加载图像
screen.blit(my_hadoken, [50, 50]) #设定图像起始位置
pygame.display.flip()
pygame.time.delay(2000) #delay时间
screen.blit(my_hadoken, [150, 50]) #设定图像终止位置
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

运行之后,我们会发现,在第一个波动拳出现后几秒,第二个波动拳出现了,所以只要我们再把第一个波动拳擦掉,不就算是移动了吗。所以我们在这里实现移动的方式是通过复制粘贴然后删除先前的图像实现的。是不是有点tricky。现在擦掉原来的图像。

#coding: UTF-8
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load(r"C:Python27hadoken.png") #加载图像
screen.blit(my_hadoken, [50, 50]) #设定图像起始位置
pygame.display.flip()
pygame.time.delay(2000) #delay时间
screen.blit(my_hadoken, [150, 50]) #设定图像终止位置
pygame.draw.rect(screen, [255, 255, 255], [50,50,90,90], 0) #用白色覆盖原图
pygame.display.flip()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

这样就可以出现波动拳移动的画面了,这次新添加的代码,是用screen的颜色去覆盖原来的图片,也就是说事实上不算是擦掉第一个波动拳,而是用白色覆盖了原来的波动拳。很明显这种方法在单色底色的场景下是比较容易的,如果底色是一个画面,过程就复杂的多,甚至要重绘整个场景。

在完成一次移动后,我们的目标当然是要波动拳可以实现流畅的多次移动,大家应该想到了,这里怕是要加入一个循环来实现这个动画。

#coding: UTF-8
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load(r"C:Python27hadoken.png") #加载图像
x=50
y=50
screen.blit(my_hadoken, [x, y]) #用变量替换定量
pygame.display.flip()
for looper in range (1, 100):
    pygame.time.delay(20)
    pygame.draw.rect(screen, [255,255,255], [x,y,90,90],0)
    x=x+5
    screen.blit(my_hadoken, [x, y])
    pygame.display.flip()
    
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

哇!我的波动拳!动起来了!

但是看到它停在最右边的一霎那,我觉得世界不够完整,我要让我的波动拳一直飞~

我们现在研究一下循环语句的起始

for looper in range (1, 100):

大家可以尝试把边界定为(1,200),结果会看到波动拳消失在右边,这是因为我们窗口大小的限制。那么如果我想要我的波动拳一直动的话,要不然就让它反弹,要不然就让它从头再来(似乎又是循环语句)。

首先,我们先来尝试从头再来。

#coding: UTF-8
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load(r"C:Python27hadoken.png") #加载图像
x=50
y=50
x_speed=10 #重新定义波动拳速度
    
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255,255,255], [x,y,90,90],0)
    x=x+x_speed
    if x > screen.get_width(): #定义边界
        x=0 #归0重新再来
    screen.blit(my_hadoken, [x, y])
    pygame.display.flip()
pygame.quit()

因垂丝汀~this is eay~我们还可以加入垂直方向上的移动。

#coding: UTF-8
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load(r"C:Python27hadoken.png") #加载图像
x=50
y=50
x_speed=10 #水平方向速度
y_speed=5 #垂直方向速度
    
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255,255,255], [x,y,90,90],0)
    x=x+x_speed
    y=y+y_speed
    if x > screen.get_width(): 
        x=0
    if y > screen.get_height():
        y=0
    screen.blit(my_hadoken, [x, y])
    pygame.display.flip()
pygame.quit()

这个用来做流星雨不错~(天马流星拳)

接下来,我们尝试让波动拳反弹。这个原理很简单,就是定义波动拳在距离窗口右边界多远的地方反弹。然后如果我们想要球一直这样来回反弹下去,我们就用一个while循环。先来试一下

#coding: UTF-8
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load(r"C:Python27hadoken.png") #加载图像
x=50
y=50
x_speed=10 #重新定义波动拳速度
    
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255,255,255], [x,y,90,90],0)
    x=x+x_speed
    if x > screen.get_width() - 90 or x<0: #定义两个边界
        x_speed = -x_speed #用速度正负定义方向
    screen.blit(my_hadoken, [x, y])
    pygame.display.flip()
pygame.quit()

波动拳一直在来回运动了,但是,回来的时候,波动拳的朝向似乎不太对。easy,加入一个翻转的指令就可以了。

#coding: UTF-8
import pygame, sys
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255,255,255]) 
my_hadoken = pygame.image.load(r"C:Python27hadoken.png") #加载图像
x=50
y=50
x_speed=10 #重新定义波动拳速度
    
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.time.delay(20)
    pygame.draw.rect(screen, [255,255,255], [x,y,90,90],0)
    x=x+x_speed
    if x > screen.get_width() - 90 or x<0: #定义两个边界
        x_speed = -x_speed #用速度正负定义方向
        my_hadoken = pygame.transform.rotate(my_hadoken, 180) #在边界翻转图片
    screen.blit(my_hadoken, [x, y])
    pygame.display.flip()
pygame.quit()

大功告成!!!!

今天的波动拳就练到这里了,期待下次相遇哦~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值