python sprite 八皇后问题 python求解

用Python和Pygame写游戏-从入门到精通(1) | 目光博客
Theme | Powered byPython:Notepad的基础操作问题,急求解!_百度知道
提问者采纳
我那边已经答你了,这边再补充几句,免得你的分儿浪费了可惜,呵呵。缩进可以用两种字符:空格/Tab符。但在Python,两种不能混用,必须统一。设置:首选项:Tab Settings:python(或改[Default],然后在python中勾选&Use default value&):如果你习惯用空格,就勾选&Replace by space&。如果你习惯用Tab,就取消&Replace by space&。你最好让Notepad++&显示所有字符&,以看清区别。
提问者评价
其他类似问题
为您推荐:
其他1条回答
在你的notepad++按下F5, 弹出Run的窗口,cmd /k python &$(FULL_CURRENT_PATH)& & PAUSE & EXIT
单击Save…。
在Shortcut窗口的设置:Shortcut窗口只是为了设置运行此命令的快捷键其中,Name可以随便输入(例如:Run Python),快捷键也可以选择,唯一的规则就是,不要跟已经设置的冲突,否则将不会起效,而且此快捷键可以修改,所以无须担心(例如Ctrl+F5)。设置完以后,单击OK保存此命令。然后就可以运行python程序,而且缩进也没问题了
能不能设置notepad++让它在我按回车的时候自动对齐原来的4字符缩进呢?
我的就可以的,自动对齐原来的4字节缩进
那我按照你的设置就可以达到这个目的是么?
是的,你设置完成后,运行下notepad++里面的python脚本,然后再进行回车操作的时候就会自动缩进4空格。 我这边是没问题的
没用哎。。。。我试了两个名字和快捷键,还是无法对齐缩进
notepad的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁本文转载自:http://eyehere.net/2011/python-pygame-novice-professional-sprite/
这又是Pygame教程系列的一个——OVA篇,类似于py2exe篇一样,额外写的,也许不是pygame游戏开发必须的东西,但是知道了绝对大有裨益。因此友情大放送~
看pygame模块介绍的时候,细心的人会发现有一个pygame.sprite模块,而在讲动画的时候,虽然引入了精灵这个概念,却没有使用这个模块。在官方文档上也说了,这个模块是轻量级的,在游戏开发中也未必要使用。讲解动画的时候为了避免太多新东西,直接把一个surface画来画去,难道没有人觉得不和谐么:)我们这次试着使用Sprite把动画变的更简单一些(不过这里没有使用GameObjects,两者结合更健康~)。
“sprite”,中文翻译“精灵”,在游戏动画一般是指一个独立运动的画面元素,在pygame中,就可以是一个带有图像(Surface)和大小位置(Rect)的对象。 精灵特别适合用在OO语言中,比如Python。
pygame.sprite.Sprite是pygame精灵的基类,一般来说,你总是需要写一个自己的精灵类继承一下它然后加入自己的代码。举个例子:
12345678910111213141516171819202122232425262728293031323334353637383940import cStringIO, base64import pygamefrom pygame.locals import *&class Ball(pygame.sprite.Sprite):&&&&def __init__(self, color, initial_position):&&&&&&&&pygame.sprite.Sprite.__init__(self)&&&&&&&&ball_file = cStringIO.StringIO(base64.decodestring(&&&iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAABBJJREFUeNqsVj2PG1UUvfPp8XictXfHa+9mlyJCNEQRWiToqACJAgGCLqJNlQZR0IFEj8RPSJkGGooUpEWJkGhR0tAAElI2tsfjjxnPjIdz7oyDF2wSUK72yN43793z7rkfY8N2HFmbbVliGIYiyzIpy1Isy3oHeMswzLOyXJ2tVit9VhTFAxz5Cfge+A7IZIcZmySObQudwIE0veanraB1w/O8l5x6D9eXy6UkSaJYLBa6BvsNuAV8uY3sCQlvX4LANM0Xw/Dgdhj2Xm02m+K6LqPRPXmeS5qmMp/PZTabyXQ6lclkosS1/QJcB+5vkthrAkoAuc4uHx//0B8MvCAIxG/5jEg0kpIkmcwXicTxBIhlHWEURXoedgW4B3wIfHuBJM9yMQ3j5PTk5N7g6MjtdrrS3e9Ku90GUUvc2hkdMYJx5IvnNRC19UReRlRLR/sGeB34UUkMJBcJlcHg6K4SdDvS7/el1+tJp7MnQdCWRqMhDGWZLmWCCFog9rBmGBYc50rOKON4uqkSC+IQSC3moeX7N09PX/i4AwLkAoQDxeFhHziU8CCUzt6e+EFLc2QaJi4mFQHykQLZMpME+WJF1sabdYA7Nq4jQbv9OZPs+75cgkSMYH9/X6PhJ9dpTLjruFLkBRyjACBd1BoLzzY8T3O0IRntJvCZDXsTTnq262CzrzmgRHu4+QEIQhAxNzRWU1mTxfjOwvBIAOlIYNnWtja5bqM33mN/sBEdx9bNPOQ1PWlqZJdAFKoMrEI6R+9gj6t7cUl1zjKnjFvsfaybr1Uqlv94ypXSKCud+aefpezs7O3LL9s4c5U65gCrhGDDpUkqyWIuU1STweNlJRe7nAlmA+ZaVbnmiD4KFNEWC+3VqjB5YImDdMA+YKONx2OVgxefojRL8CzmCxkOhxLhWYy+mGIvz6RKmv096X91PErP4Byazapbs3vZB45bVQqTzBzQkjQBQSTnjx7JcDTCRSLkKNY9SbKACsttHKZdrIqHILnGCNhoDU0qG83U5mNUVTOKShRPYo3m8fAcnT/S/3mWFy2KrXKNOFbuI+Rr1FvLsB731Ho2m2pU7I1Sx8pSHTLaESIZjob6nfso2w77mSR3IMsNzh4mmLOIBAkO6fjAgESdV1MYiV4kiUZHRDjD3E0Qza580D+rjsUdAQEj4fRl8wUkqBttPeo5RlJIuB71jIASc8D+i4W8IoX8CviC5cuI+JlgpLsgcF1ng6RQyaoX1oWX1i67DTxe9w+9/EHW9VOrngCWZfNFpmvVWOfUzZ/mfG0HwHBz4ZV1kz8nvLuL+YPnRPDJ00J8A/j9fzrnW+sjeUbjbP8amDyj86z+tXL5PwzOC4njj4K3gavA8cazczYacLd+p/+6y8mfAgwAsRuLfp/zVLMAAAAASUVORK5CYII=&&&))&&&&&&&&self.image = pygame.image.load(ball_file, 'file').convert_alpha()&&&&&&&&self.rect = self.image.fill(color, None, BLEND_ADD)&&&&&&&&self.rect.topleft = initial_position&pygame.init()screen = pygame.display.set_mode([350, 350])&ball = Ball((255, 0, 0), (100, 100))screen.blit(ball.image, ball.rect)pygame.display.update()while pygame.event.poll().type != KEYDOWN:&&&&pygame.time.delay(10)那一大堆的字符串,相信懂Python的人会明白的,不明白的请去查阅一下base64编码和Python对应的StringIO、base64库。我这里使用这种方法而不是直接读取文件,只是想告诉大家pygame.image.load方法不仅仅可以读取文件,也可以读取文件对象。是不是感觉一下子思路开阔了?Python那么多方便的文件对象,以后游戏的资源文件就可以不用一一独立放出来了,使用zipfile,我们很容易就可以把资源文件打包起来,这样看起来咱的游戏可就专业多了~这是后话,以后有机会再讲。而本例没有直接画一个圆,而是使用用了颜色混合的方法,这样可以画出有立体感的球体,效果如左图。而上面一大堆的字符串,其实就是那个球体的图像文件编码以后的东西。这个和本教程没啥大联系,请自行学习光与色的知识……但是但是,看了上面的代码大家一定会有意见了,这样感觉比直接用Surface写的代码还多啊!一点好处都没有的样子。确实会有这样的错觉,但是一个球看不出好处来,多个球呢?我们就可以这么写了:Python
&&&&&&&&&&&&&&&&&&&&&&&&([0,
&&&&&&&&&&&&&&&&&&&&&&&&([0,
&&&&boxes.append(Box(color,
location))
screen.blit(b.image,
pygame.display.update()
# 我们还能用一种更牛的重绘方式
# rectlist
= [screen.blit(b.image, b.rect) for b in balls]
# pygame.display.update(rectlist)
# 这样的好处是,pygame只会重绘有更改的部分
我就不给出完整代码和效果图了,请大家自己试验。
不过光这样还不足以体现sprite的好处,sprite最大的优势在于动画,这里就需要用一下update方法,举一个例子,把第一个程序,从33行开始换成下面的代码:
MoveBall(Ball):
__init__(self,
initial_position,
&&&&&&&&super(MoveBall,
self).__init__(color,
initial_position)
&&&&&&&&self.speed
&&&&&&&&self.border
&&&&&&&&self.update_time
update(self,
current_time):
&&&&&&&&if
self.update_time
current_time:
&&&&&&&&&&&&if
self.rect.left
self.rect.left
self.border[0]
self.rect.w:
&&&&&&&&&&&&&&&&self.speed[0]
&&&&&&&&&&&&if
self.rect.top
self.rect.top
self.border[1]
self.rect.h:
&&&&&&&&&&&&&&&&self.speed[1]
&&&&&&&&&&&&self.rect.x,
self.rect.y
self.rect.x
self.speed[0],
self.rect.y
self.speed[1]
&&&&&&&&&&&&self.update_time
current_time
pygame.init()
pygame.display.set_mode([350,
&&&&&&&&&&&&&&&&&&&&&&&&([0,
&&&&&&&&&&&&&&&&&&&&&&&&([0,
&&&&balls.append(MoveBall(color,
pygame.event.poll().type
&&&&screen.fill((0,0,0,))
&&&&current_time
pygame.time.get_ticks()
&&&&&&&&b.update(current_time)
&&&&&&&&screen.blit(b.image,
&&&&pygame.display.update()
我们可以看到小球欢快的运动起来,碰到边界就会弹回来,这才是sprite类的真正用处。每一个Sprite类都会有各自的速度属性,每次调用update都会各自更新自己的位置,主循环只需要update+blit就可以了,至于各个小球到底在一个怎样的状态,完全可以不在意。不过精灵的魅力还是不仅在此,上面的代码中我们把每个精灵加入一个列表,然后分别调用每个精灵的update方法,太麻烦了!使用pygame.sprite.Group类吧,建立它的一个实例balls,然后用add方法把精灵加入,然后只需要调用balls.update(args..)就可以了,连循环的不用写。同样的使用balls.draw()方法,你可以让pygame只重绘有变化的部分。请尝试使用(记住还有一个balls.clear()方法,实际写一下就知道这个方法用来干嘛了)。
尽管我们已经说了很多,也着实领略到了精灵的好处,但故事还没有结束,pygame.sprite有着层与碰撞的概念。层的引入是因为Group默认是没有次序的,所以哪个精灵覆盖哪个精灵完全就不知道了,解决方法嘛,使用多个Group、使用OrderedUpdates,或者使用LayeredUpdates,至于具体使用方法,相信如果您需要用到的时候,已经到相当的高度了,随便看看文档就明白了,我就不多说了;而碰撞,又是一个无底洞啊,下次有机会再讲吧~
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:441次
排名:千里之外
转载:46篇pygame.sprite & Pygame v1.9.2 documentation
pygame.sprite
pygame.sprite
pygame module with basic game object classes
Simple base class for visible game objects.
A subclass of Sprite with more attributes and features.
A container class to hold and manage multiple Sprite objects.
Same as pygame.sprite.Group
Same as pygame.sprite.Group
Group sub-class that tracks dirty updates.
RenderUpdates sub-class that draws Sprites in order of addition.
LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
LayeredDirty group is for DirtySprite objects.
Subclasses LayeredUpdates.
Group container that holds a single sprite.
Find sprites in a group that intersect another sprite.
Collision detection between two sprites, using rects.
Collision detection between two sprites, using rects scaled to a ratio.
Collision detection between two sprites, using circles.
Collision detection between two sprites, using circles scaled to a ratio.
Collision detection between two sprites, using masks.
Find all sprites that collide between two groups.
Simple test if a sprite intersects anything in a group.
This module contains several simple classes to be used within games. There is
the main Sprite class and several Group classes that contain Sprites. The use
of these classes is entirely optional when using Pygame. The classes are fairly
lightweight and only provide a starting place for the code that is common to
most games.
The Sprite class is intended to be used as a base class for the different types
of objects in the game. There is also a base Group class that simply stores
sprites. A game could create new types of Group classes that operate on
specially customized Sprite instances they contain.
The basic Sprite class can draw the Sprites it contains to a Surface. The
Group.draw() method requires that each Sprite have a Surface.image
attribute and a Surface.rect. The Group.clear() method requires these
same attributes, and can be used to erase all the Sprites with background.
There are also more advanced Groups: pygame.sprite.RenderUpdates() and
pygame.sprite.OrderedUpdates().
Lastly, this module contains several collision functions. These help find
sprites inside multiple groups that have intersecting bounding rectangles. To
find the collisions, the Sprites are required to have a Surface.rect
attribute assigned.
The groups are designed for high efficiency in removing and adding Sprites to
them. They also allow cheap testing to see if a Sprite already exists in a
Group. A given Sprite can exist in any number of groups. A game could use some
groups to control object rendering, and a completely separate set of groups to
control interaction or player movement. Instead of adding type attributes or
bools to a derived Sprite class, consider keeping the Sprites inside organized
Groups. This will allow for easier lookup later in the game.
Sprites and Groups manage their relationships with the add() and
remove() methods. These methods can accept a single or multiple targets for
membership. The default initializers for these classes also takes a single or
list of targets for initial membership. It is safe to repeatedly add and remove
the same Sprite from a Group.
While it is possible to design sprite and group classes that don’t derive from
the Sprite and AbstractGroup classes below, it is strongly recommended that you
extend those when you add a Sprite or Group class.
Sprites are not thread safe. So lock them yourself if using threads.
class pygame.sprite.Sprite
Simple base class for visible game objects.
Sprite(*groups) -& Sprite
method to control sprite behavior
add the sprite to groups
remove the sprite from groups
remove the Sprite from all Groups
does the sprite belong to any groups
list of Groups that contain this Sprite
The base class for visible game objects. Derived classes will want to
override the Sprite.update() and assign a Sprite.image and
Sprite.rect attributes. The initializer can accept any number of Group
instances to be added to.
When subclassing the Sprite, be sure to call the base initializer before
adding the Sprite to Groups. For example:
class Block(pygame.sprite.Sprite):
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(color)
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.rect = self.image.get_rect()
method to control sprite behavior
update(*args) -& None
The default implementation of this it’s just a
convenient “hook” that you can override. This method is called by
Group.update() with whatever arguments you give it.
There is no need to use this method if not using the convenience method
by the same name in the Group class.
add the sprite to groups
add(*groups) -& None
Any number of Group instances can be passed as arguments. The Sprite will
be added to the Groups it is not already a member of.
remove the sprite from groups
remove(*groups) -& None
Any number of Group instances can be passed as arguments. The Sprite will
be removed from the Groups it is currently a member of.
remove the Sprite from all Groups
kill() -& None
The Sprite is removed from all the Groups that contain it. This won’t
change anything about the state of the Sprite. It is possible to continue
to use the Sprite after this method has been called, including adding it
to Groups.
does the sprite belong to any groups
alive() -& bool
Returns True when the Sprite belongs to one or more Groups.
list of Groups that contain this Sprite
groups() -& group_list
Return a list of all the Groups that contain this Sprite.
class pygame.sprite.DirtySprite
A subclass of Sprite with more attributes and features.
DirtySprite(*groups) -& DirtySprite
Extra DirtySprite attributes with their default values:
if set to 1, it is repainted and then set to 0 again
if set to 2 then it is always dirty ( repainted each frame,
flag is not reset)
0 means that it is not dirty and therefor not repainted again
blendmode = 0
its the special_flags argument of blit, blendmodes
source_rect = None
source rect to use, remember that it is relative to
topleft (0,0) of self.image
visible = 1
normally 1, if set to 0 it will not be repainted
(you must set it dirty too to be erased from screen)
(READONLY value, it is read when adding it to the
LayeredDirty, for details see doc of LayeredDirty)
class pygame.sprite.Group
A container class to hold and manage multiple Sprite objects.
Group(*sprites) -& Group
list of the Sprites this Group contains
duplicate the Group
add Sprites to this Group
remove Sprites from the Group
test if a Group contains Sprites
call the update method on contained Sprites
blit the Sprite images
draw a background over the Sprites
remove all Sprites
A simple container for Sprite objects. This class can be inherited to create
containers with more specific behaviors. The constructor takes any number of
Sprite arguments to add to the Group. The group supports the following
standard Python operations:
test if a Sprite is contained
the number of Sprites contained
test if any Sprites are contained
iterate through all the Sprites
The Sprites in the Group are not ordered, so drawing and iterating the
Sprites is in no particular order.
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.
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 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 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.
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.
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()
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.
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
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)
remove all Sprites
empty() -& None
Removes all Sprites from this Group.
class pygame.sprite.RenderPlain
Same as pygame.sprite.Group
This class is an alias to pygame.sprite.Group(). It has no additional functionality.
class pygame.sprite.RenderClear
Same as pygame.sprite.Group
This class is an alias to pygame.sprite.Group(). It has no additional functionality.
Group sub-class that tracks dirty updates.
RenderUpdates(*sprites) -& RenderUpdates
blit the Sprite images and track changed areas
This class is derived from pygame.sprite.Group(). It has an extended
draw() method that tracks the changed areas of the screen.
blit the Sprite images and track changed areas
draw(surface) -& Rect_list
Draws all the Sprites to the surface, the same as Group.draw(). This
method also returns a list of Rectangular areas on the screen that have
been changed. The returned changes include areas of the screen that have
been affected by previous Group.clear() calls.
The returned Rect list should be passed to pygame.display.update().
This will help performance on software driven display modes. This type of
updating is usually only helpful on destinations with non-animating
backgrounds.
RenderUpdates sub-class that draws Sprites in order of addition.
OrderedUpdates(*spites) -& OrderedUpdates
This class derives from pygame.sprite.RenderUpdates(). It maintains the
order in which the Sprites were added to the Group for rendering. This makes
adding and removing Sprites from the Group a little slower than regular
LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
LayeredUpdates(*spites, **kwargs) -& LayeredUpdates
add a sprite or sequence of sprites to a group
returns a ordered list of sprites (first back, last top).
draw all sprites in the right order onto the passed surface.
returns a list with all sprites at that position.
returns the sprite at the index idx from the groups sprites
removes all sprites from a layer and returns them as a list.
returns a list of layers defined (unique), sorted from botton up.
changes the layer of the sprite
returns the layer that sprite is currently in.
returns the top layer
returns the bottom layer
brings the sprite to front layer
moves the sprite to the bottom layer
returns the topmost sprite
returns all sprites from a layer, ordered by how they where added
switches the sprites from layer1 to layer2
This group is fully compatible with .
You can set the default layer through kwargs using ‘default_layer’ and an
integer for the layer. The default layer is 0.
If the sprite you add has an attribute layer then that layer will be used.
If the **kwarg contains ‘layer’ then the sprites passed will be added to
that layer (overriding the sprite.layer attribute). If neither sprite
has attribute layer nor **kwarg then the default layer is used to add the
New in pygame 1.8.0
add a sprite or sequence of sprites to a group
add(*sprites, **kwargs) -& None
If the sprite(s) have an attribute layer then that is used for the
layer. If **kwargs contains ‘layer’ then the sprite(s) will be added
to that argument (overriding the sprite layer attribute). If neither is
passed then the sprite(s) will be added to the default layer.
returns a ordered list of sprites (first back, last top).
sprites() -& sprites
draw all sprites in the right order onto the passed surface.
draw(surface) -& Rect_list
returns a list with all sprites at that position.
get_sprites_at(pos) -& colliding_sprites
Bottom sprites first, top last.
returns the sprite at the index idx from the groups sprites
get_sprite(idx) -& sprite
Raises IndexOutOfBounds if the idx is not within range.
removes all sprites from a layer and returns them as a list.
remove_sprites_of_layer(layer_nr) -& sprites
returns a list of layers defined (unique), sorted from botton up.
layers() -& layers
changes the layer of the sprite
change_layer(sprite, new_layer) -& None
sprite must have been added to the renderer. It is not checked.
returns the layer that sprite is currently in.
get_layer_of_sprite(sprite) -& layer
If the sprite is not found then it will return the default layer.
returns the top layer
get_top_layer() -& layer
returns the bottom layer
get_bottom_layer() -& layer
brings the sprite to front layer
move_to_front(sprite) -& None
Brings the sprite to front, changing sprite layer to topmost layer (added
at the end of that layer).
moves the sprite to the bottom layer
move_to_back(sprite) -& None
Moves the sprite to the bottom layer, moving it behind all other layers
and adding one additional layer.
returns the topmost sprite
get_top_sprite() -& Sprite
returns all sprites from a layer, ordered by how they where added
get_sprites_from_layer(layer) -& sprites
Returns all sprites from a layer, ordered by how they where added. It
uses linear search and the sprites are not removed from layer.
switches the sprites from layer1 to layer2
switch_layer(layer1_nr, layer2_nr) -& None
The layers number must exist, it is not checked.
class pygame.sprite.LayeredDirty
LayeredDirty group is for DirtySprite objects.
Subclasses LayeredUpdates.
LayeredDirty(*spites, **kwargs) -& LayeredDirty
draw all sprites in the right order onto the passed surface.
used to set background
repaints the given area
clip the area where to draw. Just pass None (default) to reset the clip
clip the area where to draw. Just pass None (default) to reset the clip
changes the layer of the sprite
sets the treshold in milliseconds
This group requires
or any sprite that
has the following attributes:
image, rect, dirty, visible, blendmode (see doc of DirtySprite).
It uses the dirty flag technique and is therefore faster than the
if you have many static sprites. It
also switches automatically between dirty rect update and full screen
drawing, so you do no have to worry what would be faster.
Same as for the . You can specify some
additional attributes through kwargs:
_use_update: True/False
default is False
_default_layer: default layer where sprites without a layer are added.
_time_threshold: treshold time for switching between dirty rect mode
and fullscreen mode, defaults to 1000./80
== 1000./fps
New in pygame 1.8.0
draw all sprites in the right order onto the passed surface.
draw(surface, bgd=None) -& Rect_list
You can pass the background too. If a background is already set, then the
bgd argument has no effect.
used to set background
clear(surface, bgd) -& None
repaint_rect()
repaints the given area
repaint_rect(screen_rect) -& None
screen_rect is in screencoordinates.
set_clip()
clip the area where to draw. Just pass None (default) to reset the clip
set_clip(screen_rect=None) -& None
get_clip()
clip the area where to draw. Just pass None (default) to reset the clip
get_clip() -& Rect
change_layer()
changes the layer of the sprite
change_layer(sprite, new_layer) -& None
sprite must have been added to the renderer. It is not checked.
set_timing_treshold()
sets the treshold in milliseconds
set_timing_treshold(time_ms) -& None
Default is 1000./80 where 80 is the fps I want to switch to full screen
pygame.sprite.GroupSingle()
Group container that holds a single sprite.
GroupSingle(sprite=None) -& GroupSingle
The GroupSingle container only holds a single Sprite. When a new Sprite is
added, the old one is removed.
There is a special property, GroupSingle.sprite, that accesses the
Sprite that this Group contains. It can be None when the Group is empty. The
property can also be assigned to add a Sprite into the GroupSingle
container.
pygame.sprite.spritecollide()
Find sprites in a group that intersect another sprite.
spritecollide(sprite, group, dokill, collided = None) -& Sprite_list
Return a list containing all Sprites in a Group that intersect with another
Sprite. Intersection is determined by comparing the Sprite.rect
attribute of each Sprite.
The dokill argument is a bool. If set to True, all Sprites that collide will
be removed from the Group.
The collided argument is a callback function used to calculate if two
sprites are colliding. it should take two sprites as values, and return a
bool value indicating if they are colliding. If collided is not passed, all
sprites must have a “rect” value, which is a rectangle of the sprite area,
which will be used to calculate the collision.
collided callables:
collide_rect, collide_rect_ratio, collide_circle,
collide_circle_ratio, collide_mask
# See if the Sprite block has collided with anything in the Group block_list
# The True flag will remove the sprite in block_list
blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
# Check the list of colliding sprites, and add one to the score for each one
for block in blocks_hit_list:
pygame.sprite.collide_rect()
Collision detection between two sprites, using rects.
collide_rect(left, right) -& bool
Tests for collision between two sprites. Uses the pygame rect colliderect
function to calculate the collision. Intended to be passed as a collided
callback function to the *collide functions. Sprites must have a “rect”
attributes.
New in pygame 1.8.0
pygame.sprite.collide_rect_ratio()
Collision detection between two sprites, using rects scaled to a ratio.
collide_rect_ratio(ratio) -& collided_callable
A callable class that checks for collisions between two sprites, using a
scaled version of the sprites rects.
Is created with a ratio, the instance is then intended to be passed as a
collided callback function to the *collide functions.
A ratio is a floating point number - 1.0 is the same size, 2.0 is twice as
big, and 0.5 is half the size.
New in pygame 1.8.1
pygame.sprite.collide_circle()
Collision detection between two sprites, using circles.
collide_circle(left, right) -& bool
Tests for collision between two sprites, by testing to see if two circles
centered on the sprites overlap. If the sprites have a “radius” attribute,
that is used to create the circle, otherwise a circle is created that is big
enough to completely enclose the sprites rect as given by the “rect”
attribute. Intended to be passed as a collided callback function to the
*collide functions. Sprites must have a “rect” and an optional “radius”
attribute.
New in pygame 1.8.1
pygame.sprite.collide_circle_ratio()
Collision detection between two sprites, using circles scaled to a ratio.
collide_circle_ratio(ratio) -& collided_callable
A callable class that checks for collisions between two sprites, using a
scaled version of the sprites radius.
Is created with a floating point ratio, the instance is then intended to be
passed as a collided callback function to the *collide functions.
A ratio is a floating point number - 1.0 is the same size, 2.0 is twice as
big, and 0.5 is half the size.
The created callable tests for collision between two sprites, by testing to
see if two circles centered on the sprites overlap, after scaling the
circles radius by the stored ratio. If the sprites have a “radius”
attribute, that is used to create the circle, otherwise a circle is created
that is big enough to completely enclose the sprites rect as given by the
“rect” attribute. Intended to be passed as a collided callback function to
the *collide functions. Sprites must have a “rect” and an optional “radius”
attribute.
New in pygame 1.8.1
pygame.sprite.collide_mask()
Collision detection between two sprites, using masks.
collide_mask(SpriteLeft, SpriteRight) -& point
Returns first point on the mask where the masks collided, or None if
there was no collision.
Tests for collision between two sprites, by testing if thier bitmasks
overlap. If the sprites have a “mask” attribute, that is used as the mask,
otherwise a mask is created from the sprite image. Intended to be passed as
a collided callback function to the *collide functions. Sprites must have a
“rect” and an optional “mask” attribute.
You should consider creating a mask for your sprite at load time if you
are going to check collisions many times.
This will increase the
performance, otherwise this can be an expensive function because it
will create the masks each time you check for collisions.
sprite.mask = pygame.mask.from_surface(sprite.image)
New in pygame 1.8.0
pygame.sprite.groupcollide()
Find all sprites that collide between two groups.
groupcollide(group1, group2, dokill1, dokill2, collided = None) -& Sprite_dict
This will find collisions between all the Sprites in two groups.
Collision is determined by comparing the Sprite.rect attribute of
each Sprite or by using the collided function if it is not None.
Every Sprite inside group1 is added to the return dictionary. The value for
each item is the list of Sprites in group2 that intersect.
If either dokill argument is True, the colliding Sprites will be removed
from their respective Group.
The collided argument is a callback function used to calculate if two sprites are
colliding. It should take two sprites as values and return a bool value
indicating if they are colliding. If collided is not passed, then all
sprites must have a “rect” value, which is a rectangle of the sprite area,
which will be used to calculate the collision.
pygame.sprite.spritecollideany()
Simple test if a sprite intersects anything in a group.
spritecollideany(sprite, group, collided = None) -& Sprite Collision with the returned sprite.
spritecollideany(sprite, group, collided = None) -& None No collision
If the sprite collides with any single sprite in the group, a single
sprite from the group is returned.
On no collision None is returned.
If you don’t need all the features of the pygame.sprite.spritecollide() function, this
function will be a bit quicker.
The collided argument is a callback function used to calculate if two sprites are
colliding. It should take two sprites as values and return a bool value
indicating if they are colliding. If collided is not passed, then all
sprites must have a “rect” value, which is a rectangle of the sprite area,
which will be used to calculate the collision.
Navigation}

我要回帖

更多关于 python 编码问题 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信