使用about pygame.sprite.groupcollide的方法

时间:2013-01-16 10:55:41

标签: python pygame

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

#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'?我只想打印一次,Ball和Stone的碰撞。 (我的英语很差)

1 个答案:

答案 0 :(得分: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()