Python编程从入门到实践---pygame精灵组

初学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, 0
    surf.fill(color, rect)

empty()

remove all Sprites

empty() -> None

Removes all Sprites from this Group.

### 回答1: 《PythonPygame 写游戏 - 从入门到精通.pdf》是一本关于使用Python语言和Pygame库编写游戏的书籍。Python是一种简单易学的编程语言,具有丰富的库和工具,非常适合初学者入门。而Pygame是为了方便开发2D游戏而设计的库,提供了丰富的函数和类,可以帮助开发者轻松地创建游戏。 这本书的目标是帮助读者从游戏开发的基础知识入手,逐步了解PythonPygame的使用方法,并逐渐提高到精通水平。书中按照渐进式的学习方式,从基本的Python语法开始介绍,然后逐步引入Pygame库的功能和特性。读者可以学习如何创建游戏窗口,绘制图形和精灵,处理用户输入,实现游戏逻辑等。 此外,书中还涵盖了一些高级的游戏开发技术,比如碰撞检测、音效处理、动画效果和物理模拟等。通过学习这些内容,读者将能够掌握更多复杂游戏的开发方法,并能够自己设计和实现自己的游戏。 总的来说,《PythonPygame 写游戏 - 从入门到精通.pdf》是一本适合初学者和有一定编程基础的读者学习游戏开发的书籍。读者可以通过学习这本书,掌握使用PythonPygame开发游戏的基本技能,从而进一步提升自己在游戏开发领域的能力。 ### 回答2: 《PythonPygame 写游戏-从入门到精通》是一本关于使用Python编程语言和Pygame游戏开发库来编写游戏的指南。它逐步介绍了从入门到精通的过程,并教会读者如何利用PythonPygame创建自己的游戏。 Python是一种简单易学的高级编程语言,被广泛应用于各种领域,包括游戏开发。Pygame是一个基于Python的库,专门用于开发2D游戏。它提供了许多功能强大的工具和函数,可以帮助开发者处理游戏图形、声音、输入等方面的内容。 《PythonPygame 写游戏-从入门到精通》一书首先向读者介绍了PythonPygame的基础知识,包括安装和配置开发环境以及PythonPygame的基本语法和功能。然后,它逐渐深入探讨了游戏开发的不同方面,包括游戏循环、图形绘制、碰撞检测、游戏物理等。书中使用了大量的示例代码和实际案例来帮助读者理解和应用所学知识。 通过学习《PythonPygame 写游戏-从入门到精通》,读者将获得从入门到精通的游戏开发技能。他们将学会创建各种类型的游戏,从简单的益智游戏到复杂的角色扮演游戏。此外,书中还提供了一些高级技巧和技术,如使用人工智能和网络功能来增强游戏体验。 总之,这本书是一本全面而深入的学习资源,适合那些希望利用PythonPygame开发游戏的初学者和有经验的开发者。它将引导读者从零开始掌握游戏开发的基本技能,并帮助他们创建自己的精彩游戏作品。 ### 回答3: 《PythonPygame写游戏-从入门到精通.pdf》是一本专门介绍如何使用Python及其游戏开发库Pygame来编写游戏的书籍。 Python是一种高级编程语言,易于学习和使用。它具有简洁的语法和丰富的标准库,可以进行各种编程任务,包括游戏开发。Pygame是一个基于Python的开源游戏开发库,提供了丰富的功能和工具,方便开发者进行游戏的设计和制作。 这本书从入门到精通的目标,意味着它适合各种编程经验水平的读者。对于初学者,它会介绍PythonPygame的基本知识和概念,例如变量、条件语句、循环和函数等。然后,它将引导读者学习如何使用Pygame库中的各种功能和模块来创建游戏窗口、处理用户输入、绘制图形等。通过实际的示例和练习,读者可以逐步掌握游戏设计和开发的基本技能。 对于有一定编程经验的读者,本书也提供了更高级的内容和技巧,例如碰撞检测、动画效果、游戏物理学等。读者可以通过这些深入的学习,进一步提升自己的游戏开发能力,设计出更加有趣和复杂的游戏。 总的来说,《PythonPygame写游戏-从入门到精通.pdf》是一本对于想要学习如何使用PythonPygame编写游戏的读者来说非常有价值的书籍。通过它的指导,读者可以系统地学习游戏开发的基础知识和技能,并逐步提高自己的水平,成为一名优秀的游戏开发者。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值