pygame.sprite.groupcollide的使用方法

1 投票
1 回答
1383 浏览
提问于 2025-04-17 13:13

看看这段代码(可能有点长):

#Begin
import pygame
from pygame.locals import *

global x, y, picPath
x, y, picPath = 100, 100, 'C:\\Pic\\'

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(picPath + 'boll.png')
        self.image.set_colorkey(0xffffff, 0)
        self.rect = self.image.get_rect()
    def update(self, pos):
        screen.blit(self.image, pos)

class Stone(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(picPath + 'stone.jpg')
        self.rect = self.image.get_rect()
        self.num = 0
        self.up = 5
    def update(self):
        if(self.up != 370)and(self.num == 0):
            self.up += 5
        elif(self.up == 0)and(self.num == 1):
            self.up += 5
            self.num = 0
        else:
            self.up -= 5
            self.num = 1
        screen.blit(self.image, (305, self.up))

class GameStart:
    def __init__(self):
        self.life = 405
        self.bg = pygame.image.load(picPath + 'bg.jpg')
        self.lf = pygame.image.load(picPath + 'life.jpg')
        player1 = pygame.sprite.Group()
        player2 = pygame.sprite.Group()
        self.cB = Ball()
        self.cS = Stone()
        player1.add(self.cB)
        player2.add(self.cS)
        self.collide = pygame.sprite.groupcollide(player1, player2, 1, 0)
    def update(self):    
        if self.collide:
            print 'Yoshi!' 
        screen.blit(self.bg, (0, 0))
        self.cB.update((x, y))
        self.cS.update()
        screen.blit(self.lf, (150, self.life))

    def start(self):
        # This is Start!
        print 'Hello'

pygame.init()
screen = pygame.display.set_mode([700,460])
clock = pygame.time.Clock()

game = GameStart()
game.start()

while True:
    clock.tick(50)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()

    pygame.display.update()
    game.update()

    kname = pygame.key.get_pressed()
    if kname[pygame.K_LEFT]:
        if x > 0: x = x - 5
    elif kname[pygame.K_RIGHT]:
        if x < 700: x = x + 5
    elif kname[pygame.K_UP]:
        if y > 0: y = y - 5
    elif kname[pygame.K_DOWN]:
        if y < 460: y = y + 5
#End

为什么它一直在打印'Yoshi'?我只想打印一次,就是球和石头碰撞的时候。(我的英语不好)

1 个回答

0

groupcollide 这个函数只运行了一次。你可以试着把 groupcollide 放到 update 里面去。

class GameStart:
    def __init__(self):
        self.life = 405
        self.bg = pygame.image.load(picPath + 'bg.jpg')
        self.lf = pygame.image.load(picPath + 'life.jpg')
        self.player1 = pygame.sprite.Group()
        self.player2 = pygame.sprite.Group()
        self.cB = Ball()
        self.cS = Stone()
        self.player1.add(self.cB)
        self.player2.add(self.cS)
    def update(self):    
        if pygame.sprite.groupcollide(self.player1, self.player2, 1, 0):
            print 'Yoshi!' 
        screen.blit(self.bg, (0, 0))
        self.cB.update((x, y))
        self.cS.update()
        screen.blit(self.lf, (150, self.life))

另外,游戏的逻辑是在画面已经显示出来之后才更新的。除非你有特别的原因想要这样做,不然你可以在主循环中把 pygame.display.update 放在最后来调用。

game.update()

kname = pygame.key.get_pressed()
if kname[pygame.K_LEFT]:
    if x > 0: x = x - 5
elif kname[pygame.K_RIGHT]:
    if x < 700: x = x + 5
elif kname[pygame.K_UP]:
    if y > 0: y = y - 5
elif kname[pygame.K_DOWN]:
    if y < 460: y = y + 5

pygame.display.update()

撰写回答