gpt4 book ai didi

python - pygame spritegroups 需要帮助

转载 作者:太空宇宙 更新时间:2023-11-03 19:28:08 25 4
gpt4 key购买 nike

我修改了 pygame Chimp 示例,将 Chimp 图像替换为圆和使用从圆中心绘制的绘制方法绘制的一条线,并用 10 x 10 黑盒表面替换拳头。

我的问题是这样的。

当黑盒子(拳头)击打圆圈(黑猩猩)时,圆圈(黑猩猩)必须旋转 360 度。它适用于黑猩猩类中描述的单个圆形 Sprite ,但是当我创建多个 Sprite 并将它们添加到 sprite.group 时,只有创建的第一个 Sprite 旋转,但其他圆形(黑猩猩) Sprite 不旋转回应。谁能告诉我如何解决这个问题。我正在使用 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()

最佳答案

答案就在 punch 方法中:

def punch(self,target) :
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5,-5)
return hitbox.colliderect(target.rect)

第一只黑猩猩的属性 self.punching 设置为 1。对于所有其他 block ,该 block 不再执行,因为 self.punching 已经是 1。

尝试取消最后两行的缩进:

def punch(self,target) :
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5,-5)
return hitbox.colliderect(target.rect)

尽管这增加了新的“功能”,您可以在出拳之前再次出拳。

关于python - pygame spritegroups 需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7319124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com