pygame检测是否单击绘制的圆圈精灵失败
我正在制作一个小型游戏,如果您单击一个圆圈,它会发出流行声音,然后回到底部,我的问题是当我切换到使用类和精灵以允许多个圆圈时,它不再会检测到是否单击一个人我做错了吗?
class Bubble(pygame.sprite.Sprite):
def __init__(self):
self.radius = random.randint(50,100)
self.image = pygame.Surface([self.radius/2,self.radius/2])
self.image.fill(blue)
self.image = pygame.image.load(bPath+"Images\\Bubble.png")
self.rect = self.image.get_rect()
self.bx = random.randint(70, 1466)
self.x = self.bx
self.y = 930
self.way = 0
self.rect.x = self.x
self.rect.y = self.y
def move(self):
pygame.draw.circle(tv,blue,(self.x,self.y),self.radius)
self.y -= 2
if self.x == self.bx+20:
self.way = 1
elif self.x == self.bx-20:
self.way = 0
else:
pass
if self.way == 0:
self.x += 1
else:
self.x -= 1
if self.y <= 0:
self.bx = random.randint(70, 1466)
self.x = self.bx
self.y = 930
self.radius=random.randint(50,100)
else:
pass
bubbleList = []
nBubble = 0
while True:
tv.fill(white)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for b in bubbleList:
if b.rect.collidepoint(pygame.mouse.get_pos()):
print("hi")
pygame.mixer.music.play()
if pygame.time.get_ticks() > nBubble:
nBubble += 5000
if len(bubbleList) < 10:
bubbleList.append(Bubble())
else:
pass
for b in bubbleList:
b.move()
pygame.display.update()
FramePerSec.tick(fps)
我在这里查看了其他几篇类似的帖子,并可以看到它们实现了它们。它在我的计算机上没有任何错误,它在单击时什么都不做。
I am working on a small game where if you click a circle it makes a pop sound and goes back to the bottom my problem is when I switched to using a class and sprite to allow multiple circles it no longer will detect if one is clicked what am I doing wrong?
class Bubble(pygame.sprite.Sprite):
def __init__(self):
self.radius = random.randint(50,100)
self.image = pygame.Surface([self.radius/2,self.radius/2])
self.image.fill(blue)
self.image = pygame.image.load(bPath+"Images\\Bubble.png")
self.rect = self.image.get_rect()
self.bx = random.randint(70, 1466)
self.x = self.bx
self.y = 930
self.way = 0
self.rect.x = self.x
self.rect.y = self.y
def move(self):
pygame.draw.circle(tv,blue,(self.x,self.y),self.radius)
self.y -= 2
if self.x == self.bx+20:
self.way = 1
elif self.x == self.bx-20:
self.way = 0
else:
pass
if self.way == 0:
self.x += 1
else:
self.x -= 1
if self.y <= 0:
self.bx = random.randint(70, 1466)
self.x = self.bx
self.y = 930
self.radius=random.randint(50,100)
else:
pass
bubbleList = []
nBubble = 0
while True:
tv.fill(white)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
for b in bubbleList:
if b.rect.collidepoint(pygame.mouse.get_pos()):
print("hi")
pygame.mixer.music.play()
if pygame.time.get_ticks() > nBubble:
nBubble += 5000
if len(bubbleList) < 10:
bubbleList.append(Bubble())
else:
pass
for b in bubbleList:
b.move()
pygame.display.update()
FramePerSec.tick(fps)
I have looked at a couple other similar posts on here and have implemented them as you can probably see. It throws no errors on my computer it just does nothing when clicked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
什么是
self.x
和self.y
?您不需要这些属性。您有self.Rect.x
和self.Rect.y
。碰撞检测取决于rect
属性中存储的位置。但是,
self.rect.x
和self.rect.y
当您更改self.x
和self时,请勿神奇地更改。 y
。rect
属性中存储的位置与self.x
和self.y.y
无关。摆脱self.x
和self.y
:另请参阅我如何在pygame中检测碰撞?。
What are
self.x
andself.y
for? You don't need these attributes. You haveself.rect.x
andself.rect.y
. The collision detection depends on the position stored in therect
attribute.However,
self.rect.x
andself.rect.y
do not magically change when you changeself.x
andself.y
. The position stored in therect
attribute is not tied toself.x
andself.y
. Get rid ofself.x
andself.y
:See also How do I detect collision in pygame?.