python使用pygame创建精灵Sprite
作者:shanghai_in_summer 时间:2021-04-20 06:44:35
一 、精灵(Sprite),屏幕上的对象。精灵组是精灵的组合。创建空的精灵组对象:
精灵组可以对其中的所有精灵调用它们各自的更新方法(self.update)来进行更新,如位置更新、碰撞检测、冲突检测等:
all_sprites.update()
精灵组可以对其中的所有精灵调用它们各自的DRAW方法(self.update)来绘制精灵:
all_sprites.draw(screen)
二、创建精灵
1、创建精灵需要继承基类pg.sprite.Sprite。每个Pygame精灵都必须拥有两个属性: image
和 rect
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.img = pg.Surface((50, 50))
self.img.fill(GREEN)
self.rect = self.img.get_rect()
self.rect.center = (215, 215)
其中,rect有如下定位属性:
其中,topleft, topright, center, bottomleft, bottomright为二元int元组,其余的为int。
2、添加update方法:
def update(self):
self.rect.x += 5
if self.rect.left > WIDTH:
self.rect.right = 0
在游戏循环中,有all_sprites.update()
。这意味着对于组中的每个sprite,Pygame将查找一个update()
函数并运行它。
三、将精灵加入精灵组:
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
来源:https://blog.csdn.net/sunjianqiang12345/article/details/115440951
标签:pygame,创建,Sprite
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python大数据量文本文件高效解析方案代码实现全过程
2023-01-18 04:57:01
python2.7读取文件夹下所有文件名称及内容的方法
2023-12-16 03:27:07
python游戏测试工具自动化遍历游戏中所有关卡
2021-10-05 13:03:20
![](https://img.aspxhome.com/file/2023/9/93589_0s.png)
Python 如何强制限定小数点位数
2022-10-01 15:25:43
Flask项目的部署的实现步骤
2023-08-11 17:59:58
![](https://img.aspxhome.com/file/2023/9/85319_0s.jpg)
关于Python 3中print函数的换行详解
2021-04-09 09:57:48
![](https://img.aspxhome.com/file/2023/5/98325_0s.jpg)
python调用pyaudio使用麦克风录制wav声音文件的教程
2023-12-16 13:32:15
Python利用matplotlib绘制折线图的新手教程
2021-08-18 18:00:01
![](https://img.aspxhome.com/file/2023/5/97265_0s.png)
php生成与读取excel文件
2023-11-15 06:17:48
K近邻法(KNN)相关知识总结以及如何用python实现
2022-07-15 17:10:45
![](https://img.aspxhome.com/file/2023/9/70099_0s.png)
为FCKeditor2.6添加行距功能(最新修改)
2008-08-18 21:09:00
ORACLE 如何查询被锁定表及如何解锁释放session
2023-07-02 11:59:39
ThinkPHP php 框架学习笔记
2023-09-10 08:20:32
Python中Subprocess的不同函数解析
2022-03-10 05:23:00
动态加载JavaScript的小实践
2009-11-12 12:38:00
![](https://img.aspxhome.com/file/UploadPic/200911/12/yui-loader-bug-in-ie-58s.png)
Js的MessageBox
2008-05-16 10:25:00
使用Python下的XSLT API进行web开发的简单教程
2022-07-24 22:07:14
![](https://img.aspxhome.com/file/2023/1/66401_0s.jpg)
如何高效使用Python字典的方法详解
2021-07-26 04:29:03
python中input()与raw_input()的区别分析
2023-02-12 14:08:36
![](https://img.aspxhome.com/file/2023/4/94024_0s.png)
python opencv 画外接矩形框的完整代码
2023-02-25 05:00:46
![](https://img.aspxhome.com/file/2023/3/80413_0s.png)