转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi

据说在任天堂FC时代,精灵的作用相当巨大,可是那时候只知道怎么玩超级玛丽、魂斗罗,却对精灵一点也不知。pygame.sprite.Sprite就是Pygame里面用来实现精灵的一个类,使用时,并不需要对它实例化,只需要继承他,然后按需写出自己的类就好了,因此非常简单实用。
一、什么是精灵
精灵可以认为成是一个个小图片,一种可以在屏幕上移动的图形对象,并且可以与其他图形对象交互。精灵图像可以是使用pygame绘制函数绘制的图像,也可以是原来就有的图像文件。
二、sprite中主要且常用的变量有以下几个:更多详细的见http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
self.image这个负责显示什么。如self.image=pygame.Surface([x,y])说明该精灵是一个x,y大小的距形,self.image=pygame.image.load(filename)说明该精灵调用显示filename这个图片文件。
self.image.fill([color]),负责对self.image着色,如self.image=pygame.Surface([x,y])
self.image.fill([255,0,0])
对x,y距形填充红色。
self.rect负责在哪里显示。一般来说,先用self.rect=self.image.get_rect()获得image距形大小,然后给self.rect设定显示的位置,一般用self.rect.topleft(topright、bottomleft、bottomright)来设定某一个角的显示位置。另外,self.rect.top、self.rect.bottom、self.rect.right、self.rect.left分别表示上下左右。
self.update 负责使精灵行为生效。
Sprite.add  添加精灵到group中去。
Sprite.remove 从group中删除
Sprite.kill 从groups中全部删除精灵
Sprite.alive 判断精灵是否属于groups

三、建立一个简单的精灵
所有精灵在建立时都是从pygame.sprite.Sprite中继承的。
(1)做一个精灵,绘制一个宽30、高30的距形,具体代码如下:

class Temp(pygame.sprite.Sprite):
def __init__(self,color,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([30,30])
self.image.fill(color)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position

这里逐句进行一下分析,pygame.sprite.Sprite.__init__(self)完成初始化。self.image = pygame.Surface([30,30])定义显示30*30的一个距形surface。self.image.fill(color)用color来填充颜色。self.rect=self.image.get_rect()获取self.image大小。self.rect.topleft=initial_position确定左上角显示位置,当然也可以用topright、bottomrigh、bottomleft来分别确定其他几个角的位置。精灵的显示,在一个640*480大小的白色窗体[50,100]的位置绘制一个30*30大小的红色距形,完整代码如下:

#小五义 http://www.cnblogs.com/xiaowuyi
import pygame,sys
pygame.init()
class Temp(pygame.sprite.Sprite):
def __init__(self,color,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([30,30])
self.image.fill(color)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
b=Temp([255,0,0],[50,100])
screen.blit(b.image,b.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()

(2)做一个精灵,显示内容为某一图片,这里以前面用过的小车图片为例,代码如下:

#小五义 http://www.cnblogs.com/xiaowuyi
import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
#self.rect.topleft=initial_position
self.rect.bottomright=initial_position
print self.rect.right screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
b=Car(fi,[150,100])
screen.blit(b.image,b.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()

这段代码与(1)的不同之处在于self.image定义为pygame.image.load(filename),用来显示filename文件,本代码使用了ok1.jpg文件,并定义了小车右底角的显示位置是[150,100]。

三、学习精灵组
(1)使用精灵在屏幕上放多个图像,这种方法没用利用精灵组的概念,而是利用了list来生成每一个精灵。Cargroup用来存储不同位置的Car,screen.blit(carlist.image,carlist.rect)逐个显示每一个精灵。具体见代码:

#小五义 http://www.cnblogs.com/xiaowuyi
import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.bottomright=initial_position screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,360],[250,280])
Cargroup=[]
for lo in locationgroup:
Cargroup.append(Car(fi,lo))
for carlist in Cargroup:
screen.blit(carlist.image,carlist.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()

具体效果见图:

(2)使用精灵组来实现多个图像。上面精灵是存在一个列表中,很方便,就是有点不太好用。除了精灵,pygame还提供了精灵组,它很适合处理精灵列表,有添加,移除,绘制,更新等方法。具体如下:http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
Group.sprites 精灵组
Group.copy 复制
Group.add 添加
Group.remove 移除
Group.has 判断精灵组成员
Group.update 更新
Group.draw 位块显示
Group.clear - 绘制背景
Group.empty 清空
同样还是上面的这个例子,这里用精灵组来实现。

#小五义 http://www.cnblogs.com/xiaowuyi
import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.bottomright=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,360],[250,280])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
Cargroup.add(Car(fi,lo)) for carlist in Cargroup.sprites():
screen.blit(carlist.image,carlist.rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()

两个例子都是在[150,200],[350,360],[250,280]三个位置显示三辆小车,不同之处第一个用的是list,第二个用的是精灵组。差别就在几句话上,一是Cargroup=pygame.sprite.Group()定义Cargroup为精灵组,二是Cargroup.add(Car(fi,lo))用add代替了append,三是for carlist in Cargroup.sprites()这句中逐个显示精灵,这里试了一下,直接用for carlist in Cargroup也是可以的。精灵组的代码是高度优化过了,常常比列表还快。插入和删除都是常见的操作,代码还可以避免内存在循环中反复消耗。
四、动画
利用精灵组做动画会显得比较方便,这里我们首先让上面的三辆小车运动起来。
(1)三辆小车以不同的速度前行,利用random.choice随机生成[-10,-1]之间的值作为速度让小车从下向上运动,并且当到达顶部时,再从底部出现。代码如下:

#小五义 http://www.cnblogs.com/xiaowuyi
import pygame,sys
from random import *
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position,speed):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
self.speed=speed
def move(self):
self.rect=self.rect.move(self.speed)
if self.rect.bottom < 0: #当小车底部到达窗口顶部时,让小车从下面出来
self.rect.top=480
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
speed=[0,choice([-10,-1])]
Cargroup.add(Car(fi,lo,speed)) while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
pygame.time.delay(20)
screen.fill([255,255,255])
for carlist in Cargroup.sprites():
carlist.move()
screen.blit(carlist.image,carlist.rect)
pygame.display.update()

(2)可以通过左右键控制三辆小车的左右移动,按左键向左移动,当到达最左边时,不再移动,按右键向右移动,当到达最右边时,不再移动。具体代码如下:

#小五义 http://www.cnblogs.com/xiaowuyi
import pygame,sys
from random import *
pygame.init()
class Car(pygame.sprite.Sprite):
def __init__(self,filename,initial_position,speed):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(filename)
self.rect=self.image.get_rect()
self.rect.topleft=initial_position
self.speed=speed
def move(self):
self.rect=self.rect.move(self.speed)
if self.rect.bottom < 0:
self.rect.top=480
def moveleft(self):
self.rect.left=self.rect.left-10
if self.rect.left<0:
self.rect.left=0
def moveright(self):
self.rect.right=self.rect.right+10
if self.rect.right>640:
self.rect.right=640
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
speed=[0,choice([-10,-1])]
Cargroup.add(Car(fi,lo,speed)) while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
for carlist in Cargroup.sprites():
carlist.moveleft()
screen.blit(carlist.image,carlist.rect)
if event.key==pygame.K_RIGHT:
for carlist in Cargroup.sprites():
carlist.moveright()
screen.blit(carlist.image,carlist.rect)
pygame.time.delay(20)
screen.fill([255,255,255])
for carlist in Cargroup.sprites():
carlist.move()
screen.blit(carlist.image,carlist.rect)
pygame.display.update()

pygame学习笔记(5)——精灵的更多相关文章

  1. pygame学习笔记(3)——时间、事件、文字

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.运动速率    上节中,实现了一辆汽车在马路上由下到上行驶,并使用了pygame.time.delay(200 ...

  2. PyGame学习笔记之壹

    新建窗口 代码 '''PyGame学习笔记之壹''' import pygame # 引入 PyGame 库 pygame.init() # PyGame 库初始化 screen = pygame.d ...

  3. pygame学习笔记(4)——声音

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi pygame.mixer是一个用来处理声音的模块,其含义为“混音器”.游戏中对声音的处理一般包括制造声音和播放声音 ...

  4. pygame学习笔记(1)——安装及矩形、圆型画图

    pygame是一个设计用来开发游戏的python模块,其实说白了和time.os.sys都是一样的东东.今天开始正式学习pygame,下载地址:www.pygame.org.下载后安装完成即可,在py ...

  5. pygame学习笔记

    pygame参考文档pdf版:pygame API html版 pygame API 石头剪子布的简单小游戏,待改进的地方,自适应大小.感兴趣的小伙伴可以依据get_surface()返回值(即当前窗 ...

  6. pygame学习笔记(6)——一个超级简单的游戏

    转载请注明:@小五义  http://www.cnblogs.com/xiaowuyi 学了这么长时间的Pygame,一直想写个游戏实战一下.看起来很简单的游戏,写其来怎么这么难.最初想写个俄罗斯方块 ...

  7. pygame学习笔记(2)——从画点到动画

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 1.单个像素(画点)利用pygame画点主要有三种方法:方法一:画长宽为1个像素的正方形 #@小五义 http:/ ...

  8. PYGAME学习笔记_01

    01_使用PYGAME创建图形窗口 1.1_游戏的初始化和退出 pygame.init() 写入并初始化所有PYGAME模块,使用其他模块之前,必须先调用init方法 pygame.quit() 卸载 ...

  9. 转 pygame学习笔记(1)——安装及矩形、圆型画图

    http://www.cnblogs.com/xiaowuyi/archive/2012/06/06/2538921.html

随机推荐

  1. RAC建立过程回顾--建立用户和组

    一共需要建立6个组: oinstall dba asmadmin asmdba asmoper oper 要建立两个用户: oracle 和 grid 然后还要给各个用户建立各自的环境变量. 以下的操 ...

  2. BSGS算法总结

    BSGS算法总结 \(BSGS\)算法(Baby Step Giant Step),即大步小步算法,用于解决这样一个问题: 求\(y^x\equiv z\ (mod\ p)\)的最小正整数解. 前提条 ...

  3. Asp.net中使用缓存(cache)

    做了一个时间优化的项目,目的就是缩短程序过程中的时间花费,最后发现了asp.net和asp.net core 中都有缓存工具来进行缓存,以加快访问速度. 找了官方demo来进行分析: ObjectCa ...

  4. 交换机 路由器 OSI7层模型

    第1章 网络基础 1.1 网络的出现 解决计算机通讯的需求 实现计算机信息可以传递 1.2 主机之间实现通讯基本要求(三要素) ①. 需要在两台主机之间建立物理连接,物理连接的方式有网线 光纤线 wi ...

  5. flask中的简单的前端写入

    那么flask这个框架是web开发,那么肯定离不开前端的一些代码,那么python用的web开发框架 开发所用的前端模板就是jinja2模板.相对于jinja1比起来性能做到了很大的提升,那么Vue一 ...

  6. 相机标定与矫正opencv+MATLAB

    博客转载自:http://blog.csdn.net/Loser__Wang/article/details/51811347 本文目的在于记录如何使用MATLAB做摄像机标定,并通过opencv进行 ...

  7. 在CentOS7上部署PostgreSQL11数据库系统

    在数据库上的选择,也是基于了稳定性为前提.其实选择的范围并不是太大,基本可以选择的范围也就是SQLServer.MySQL.PostgreSQL这三种.SQL Server是微软的商业数据库,无论是性 ...

  8. NO--14 微信小程序,左右联动二

    上一篇讲解了左=>右联动,那个还比较简单,本篇写剩下比较核心的部分,也是本次开发过程中遇到最难的部分,右=>左联动,先简单看一下演示   右左联动.gif 一.关键技术: (1) 小程序 ...

  9. zabbix切换中文,监控图下方显示乱码,监控图X轴不显示时间问题解决(适用于所有版本)

    一.现象: abbix3.4安装好后添加zabbix图形,发现有好多方块 这是因为zabbix web程序缺少中文字体 二.解决方案1: 1.在windows系统找一个中文字体上传到服务器中,我这里找 ...

  10. 【CentOS 7】nginx配置web服务器

    1,安装过程 [root@VM_1_14_centos ~]# cd /data/ [root@VM_1_14_centos data]# wget http://nginx.org/download ...