pygame检测是否单击绘制的圆圈精灵失败

发布于 2025-02-11 03:42:49 字数 2043 浏览 2 评论 0原文

我正在制作一个小型游戏,如果您单击一个圆圈,它会发出流行声音,然后回到底部,我的问题是当我切换到使用类和精灵以允许多个圆圈时,它不再会检测到是否单击一个人我做错了吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双手揣兜 2025-02-18 03:42:49

什么是self.xself.y?您不需要这些属性。您有self.Rect.xself.Rect.y。碰撞检测取决于rect属性中存储的位置。

 如果b.Rect.collidepoint(pygame.mouse.get_pos()):
 

但是,self.rect.xself.rect.y当您更改self.xself时,请勿神奇地更改。 yrect属性中存储的位置与self.xself.y.y无关。摆脱self.xself.y

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.way = 0
       self.rect.x = self.bx
       self.rect.y = 930

   def move(self):
       pygame.draw.circle(tv, blue, self.rect.center, self.radius)
       
       self.rect.y -= 2
       if self.rect.x == self.bx+20:
           self.way = 1
       elif self.rect.x == self.bx-20:
           self.way = 0
       else:
           pass

       if self.way == 0:
           self.rect.x += 1
       else:
           self.rect.x -= 1

       if self.rect.y <= 0:
           self.bx = random.randint(70, 1466)
           self.rect.x = self.bx
           self.rect.y = 930
           self.radius=random.randint(50,100)
       else:
           pass

另请参阅我如何在pygame中检测碰撞?

What are self.x and self.y for? You don't need these attributes. You have self.rect.x and self.rect.y. The collision detection depends on the position stored in the rect attribute.

if b.rect.collidepoint(pygame.mouse.get_pos()):

However, self.rect.x and self.rect.y do not magically change when you change self.x and self.y. The position stored in the rect attribute is not tied to self.x and self.y. Get rid of self.x and self.y:

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.way = 0
       self.rect.x = self.bx
       self.rect.y = 930

   def move(self):
       pygame.draw.circle(tv, blue, self.rect.center, self.radius)
       
       self.rect.y -= 2
       if self.rect.x == self.bx+20:
           self.way = 1
       elif self.rect.x == self.bx-20:
           self.way = 0
       else:
           pass

       if self.way == 0:
           self.rect.x += 1
       else:
           self.rect.x -= 1

       if self.rect.y <= 0:
           self.bx = random.randint(70, 1466)
           self.rect.x = self.bx
           self.rect.y = 930
           self.radius=random.randint(50,100)
       else:
           pass

See also How do I detect collision in pygame?.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文