初学Python,跟着书上的外星人项目,写了一下,碰到一个这样的问题:

AttributeError: 'Alien' object has no attribute 'add_internal'

找到的原因是,精灵组里面只能添加精灵,不能添加其他的非精灵的类,这也是我在这里犯的错。将需要添加到精灵组的类,需要继承Sprite类,犯错代码块如下:

正确的是:

既然碰到了精灵组,那就放些相关的知识吧。

pygame.sprite的官网api

https://www.pygame.org/docs/ref/sprite.html?highlight=sprite#module-pygame.sprite

常用的sprite方法

sprites()

list of the Sprites this Group contains

sprites() -> sprite_list

Return a list of all the Sprites this group contains. You can also get an iterator from the group, but you cannot iterator over a Group while modifying it.

copy()

duplicate the Group

copy() -> Group

Creates a new Group with all the same Sprites as the original. If you have subclassed Group, the new object will have the same (sub-)class as the original. This only works if the derived class's constructor takes the same arguments as the Group class's.

add()

add Sprites to this Group

add(*sprites) -> None

Add any number of Sprites to this Group. This will only add Sprites that are not already members of the Group.

Each sprite argument can also be a iterator containing Sprites.

remove()

remove Sprites from the Group

remove(*sprites) -> None

Remove any number of Sprites from the Group. This will only remove Sprites that are already members of the Group.

Each sprite argument can also be a iterator containing Sprites.

has()

test if a Group contains Sprites

has(*sprites) -> None

Return True if the Group contains all of the given sprites. This is similar to using the "in" operator on the Group ("if sprite in group: ..."), which tests if a single Sprite belongs to a Group.

Each sprite argument can also be a iterator containing Sprites.

update()

call the update method on contained Sprites

update(*args) -> None

Calls the update() method on all Sprites in the Group. The base Sprite class has an update method that takes any number of arguments and does nothing. The arguments passed to Group.update() will be passed to each Sprite.

There is no way to get the return value from the Sprite.update() methods.

draw()

blit the Sprite images

draw(Surface) -> None

Draws the contained Sprites to the Surface argument. This uses the Sprite.image attribute for the source surface, and Sprite.rect for the position.

The Group does not keep sprites in any order, so the draw order is arbitrary.

clear()

draw a background over the Sprites

clear(Surface_dest, background) -> None

Erases the Sprites used in the last Group.draw() call. The destination Surface is cleared by filling the drawn Sprite positions with the background.

The background is usually a Surface image the same dimensions as the destination Surface. However, it can also be a callback function that takes two arguments; the destination Surface and an area to clear. The background callback function will be called several times each clear.

Here is an example callback that will clear the Sprites with solid red:

def clear_callback(surf, rect):color = 255, 0, 0surf.fill(color, rect)

empty()

remove all Sprites

empty() -> None

Removes all Sprites from this Group.

Python编程从入门到实践---pygame精灵组相关推荐

  1. python开发之父亲自编写《python编程从入门到实践》中译版图书销量破百万册,完整版PDF开放下载

    为什么要学习python 主流编程语言 世界上的编程语言有600多种,但真正大家主流在使用的最多二三十种,不同的语言有自己的特点和擅长领域,随着计算机的不断发展,新语言在不断诞生,也同时有很多老旧的语 ...

  2. 《Python编程从入门到实践 第2版》 读后感

    <Python编程从入门到实践 第2版> 读后感 一直在想,一本好的Python书籍应该是怎样的?是将所有的基础知识点都讲解透彻?还是仅仅从方法上对读者授之以渔呢?我想这也是很多著书人在开 ...

  3. Python语法学习测试记录(Python编程从入门到实践——Eric Matthes著)

    注:工作之余,学习一门脚本语言,选择了当下火热的Python.2020-05-27日开始学习Python基础语法,期间参考<Python编程从入门到实践--Eric Matthes著>一书 ...

  4. python编程从入门到实践之外星人入侵全代码

    好久没有更新了,最近寒假在家没事做,就想学点东西,在网上找到一本关于python的书籍<python编程从入门到实践>,经过大概一个多月的时间吧,也算是大致把本书浏览了一遍. 本人感觉这本 ...

  5. python编程 从入门到实践怎么样-python编程从入门到实践这本书怎么样

    <Python编程-从入门到实践>作者: Eric Matthes,已翻译为中文,人民邮电出版社出版. python编程从入门到实践怎么样? 我们一起看看已经学习的同学对这本书的口碑和评价 ...

  6. python编程入门指南怎么样-python编程从入门到实践这本书怎么样

    <Python编程-从入门到实践>作者: Eric Matthes,已翻译为中文,人民邮电出版社出版. python编程从入门到实践怎么样? 我们一起看看已经学习的同学对这本书的口碑和评价 ...

  7. python编程 入门到实践-终于懂了python编程从入门到实践

    Python语言是一种典型的脚本语言,简洁,语法约束少,接近人类语言.有丰富的数据结构,例如列表.字典.集合等.具有可移植性,支持面向过程和面向对象编程,并且开源.以下是小编为你整理的python编程 ...

  8. python编程 从入门到实践豆瓣-三周刷完《Python编程从入门到实践》的感受

    本文将以对话采访的形式展现 为什么会选择学习Python 语法简洁,实用,和golang部分类似,学习性价比高: 应用范围广,涉及后端,机器学习,图像处理,游戏等: 好奇这是一门怎样的语言 计划如何学 ...

  9. python入门到实践-Python编程从入门到实践(基础入门)

    Python编程从入门到实践-------基础入门 1.Python中的变量 2.Python首字母大写使用title()方法,全部大写upper()方法,全部小写lower()方法 3.Python ...

最新文章

  1. CentOS7.0使用Yum安装Nginx
  2. 备赛脱脂经验分享_IEO国际经济学奥林匹克初选宏观备赛经验分享
  3. brainfuck 在线_私有化部署,开源轻量级的团队在线协作工具——Kooteam
  4. 电信主机托管费用_电信服务器托管价格
  5. AC自动机-算法详解
  6. cornerstone 使用
  7. 试图加载 Crystal Reports 运行时出现错误
  8. 三菱梯形图转换c语言软件,三菱PLC编程软件的梯形图程序的生成与编辑
  9. MongoDBCompass使用教程
  10. 关于火狐浏览器不能看中国大学mooc上视频的解决办法
  11. 学术与应用的碰撞,精准医疗和生物医药大数据的盛宴
  12. 机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折
  13. java 生成图片验证码结合struts2使用
  14. 安卓APP源码和设计报告——仿淘宝水果商城
  15. 怀揣巨资等了一季 陈天桥距门户野心咫尺之遥
  16. MySQL认证介绍 (转帖)
  17. [题集]Lecture 4. Leftist Heaps and Skew Heaps
  18. 冰河亲自整理的Git命令汇总,悄悄努力,然后惊艳所有人
  19. html5账号秘密,JavaScript有关的10个秘密和怪癖
  20. Linux的远程终端连不上,Linux系统远程连接终端连接不上怎么办

热门文章

  1. 该虚拟机似乎正在使用中
  2. 分组急救技能竞赛方法在急诊专科护士培训中的运用
  3. 优酷土豆联姻 视频网站格局的几大猜想
  4. 微信聊天记录没有备份可以恢复吗?吐血整理分享
  5. 在CAD中加载大影像的一种方法
  6. 头像截图上传两种方式(SWFUpload、一个简单易用的flash插件)
  7. 马赛克颗粒感天空Canvasjs特效
  8. Calendar代替Date类获取当前年月日,getYear,getMonth,getDay
  9. Quick BI企业报表制作——个人笔记
  10. Unity 打包项目发红