pygame spritegroups 需要帮助
我修改了 pygame Chimp 示例,将 Chimp 图像替换为圆和一条使用从圆中心绘制的绘制方法绘制的线,并用 10 x 10 黑盒表面替换拳头。
我的问题是这样的。
当黑盒子(拳头)击打圆圈(黑猩猩)时,圆圈(黑猩猩)必须旋转 360 度。它适用于黑猩猩类中描述的单个圆形精灵,但是当我创建多个精灵并将它们添加到 sprite.group 时,只有创建的第一个精灵旋转,但其他圆形(黑猩猩)精灵不旋转回应。谁能告诉我如何解决这个问题。我正在使用 python 3x 和 pygame 1.9
#Import Modules
import os, pygame
from pygame.locals import *
from pygame.compat import geterror
import random
def load_image(x=40,y=40,r=0,g=0,b=0):
image = pygame.Surface((x,y))
image.fill((r,g,b))
pygame.draw.circle(image,(r,g,255),(20,20),10)
pygame.draw.line(image,(r,255,b),(20,20),(30,30),2)
image = image.convert()
image.set_colorkey((255,0,0))
return image, image.get_rect()
class Fist(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image,self.rect = load_image(x=10,y=10)
self.punching = 0
def update(self):
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5,10)
def punch(self,target) :
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5,-5)
return hitbox.colliderect(target.rect)
def unpunch(self):
self.punching = 0
class Chimp (pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image,self.rect = load_image(r=255)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = random.randint(0,300),random.randint(0,300)
self.move = 0
self.dizzy = 0
def update(self):
if self.dizzy:
self._spin()
else:
self._walk()
def _walk(self):
newpos = self.rect.move((self.move,0))
if self.rect.left < self.area.left or self.rect.right > self.area.right:
self.move = -self.move
newpos = self.rect.move((self.move,0))
self.rect = newpos
def _spin(self) :
center = self.rect.center
self.dizzy = self.dizzy + 6
if self.dizzy >= 360:
self.dizzy = 0
self.image = self.original
else :
rotate = pygame.transform.rotate
self.image = rotate(self.original,self.dizzy)
self.rect = self.image.get_rect(center=center)
def punched(self):
if not self.dizzy:
self.dizzy = 1
self.original = self.image
def main():
pygame.init()
screen = pygame.display.set_mode((640,480))
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250,250,250))
screen.blit(background,(0,0))
pygame.display.flip()
clock = pygame.time.Clock()
fist = Fist()
allsprites = pygame.sprite.Group()
for i in range(10):
chimp = Chimp()
allsprites.add(chimp)
fistsprite = pygame.sprite.Group((fist))
going = True
while going:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
going = False
elif event.type == MOUSEBUTTONDOWN:
for chimp in allsprites.sprites:
if fist.punch(chimp):
chimp.punched()
elif event.type == MOUSEBUTTONUP:
fist.unpunch()
allsprites.update()
fistsprite.update()
screen.blit(background,(0,0))
allsprites.draw(screen)
fistsprite.draw(screen)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()
I have modified the pygame Chimp example by replacing the Chimp image with the circle and a line using the draw method drawn from the center of the circle and also by replacing the fist with a 10 by 10 black box surface.
My problem is this.
When the black box(fist) punches the circle(chimp), the circle(chimp) has to rotate by 360 degrees. It works fine for a single circle sprite which is described in the chimp class, but when i create more than one sprite and add them to a sprite.group only the first sprite that is created rotates but the other circle(Chimp) sprites don't respond. Can anyone please tell me how to solve this problem. I'm using python 3x and pygame 1.9
#Import Modules
import os, pygame
from pygame.locals import *
from pygame.compat import geterror
import random
def load_image(x=40,y=40,r=0,g=0,b=0):
image = pygame.Surface((x,y))
image.fill((r,g,b))
pygame.draw.circle(image,(r,g,255),(20,20),10)
pygame.draw.line(image,(r,255,b),(20,20),(30,30),2)
image = image.convert()
image.set_colorkey((255,0,0))
return image, image.get_rect()
class Fist(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image,self.rect = load_image(x=10,y=10)
self.punching = 0
def update(self):
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5,10)
def punch(self,target) :
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5,-5)
return hitbox.colliderect(target.rect)
def unpunch(self):
self.punching = 0
class Chimp (pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image,self.rect = load_image(r=255)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = random.randint(0,300),random.randint(0,300)
self.move = 0
self.dizzy = 0
def update(self):
if self.dizzy:
self._spin()
else:
self._walk()
def _walk(self):
newpos = self.rect.move((self.move,0))
if self.rect.left < self.area.left or self.rect.right > self.area.right:
self.move = -self.move
newpos = self.rect.move((self.move,0))
self.rect = newpos
def _spin(self) :
center = self.rect.center
self.dizzy = self.dizzy + 6
if self.dizzy >= 360:
self.dizzy = 0
self.image = self.original
else :
rotate = pygame.transform.rotate
self.image = rotate(self.original,self.dizzy)
self.rect = self.image.get_rect(center=center)
def punched(self):
if not self.dizzy:
self.dizzy = 1
self.original = self.image
def main():
pygame.init()
screen = pygame.display.set_mode((640,480))
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250,250,250))
screen.blit(background,(0,0))
pygame.display.flip()
clock = pygame.time.Clock()
fist = Fist()
allsprites = pygame.sprite.Group()
for i in range(10):
chimp = Chimp()
allsprites.add(chimp)
fistsprite = pygame.sprite.Group((fist))
going = True
while going:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
going = False
elif event.type == MOUSEBUTTONDOWN:
for chimp in allsprites.sprites:
if fist.punch(chimp):
chimp.punched()
elif event.type == MOUSEBUTTONUP:
fist.unpunch()
allsprites.update()
fistsprite.update()
screen.blit(background,(0,0))
allsprites.draw(screen)
fistsprite.draw(screen)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
答案就在
punch
方法中:对于第一只黑猩猩,属性
self.punching
设置为 1。对于所有其他的,该块不再执行,因为self.punching
已经是 1。尝试取消最后两行的缩进:
尽管这添加了新的“功能”,您可以在之前再次打孔拳头已被打出。
The answer lies in the
punch
method:The attribute
self.punching
is set to 1 for the first chimp. For all the others, the block isn't executed anymore, sinceself.punching
is already 1.Try unindenting the last two lines instead:
Though this adds the new "feature" that you can punch again before the punch has been unpunched.