The method for the use of about pygame.sprite.groupcollide

怎甘沉沦 提交于 2021-02-11 08:45:01

问题


Look at this code (She may be a little long):

#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

Why does she keeps printing 'Yoshi'? I just want to print it once, the collision of the Ball and Stone. (My English is poor)


回答1:


groupcollide is only running once. Try moving groupcollide into 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))

Also, the game logic is updating after the display is already drawn. Unless you want that behavior for some reason, you can call pygame.display.update last in the main loop.

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()


来源:https://stackoverflow.com/questions/14356730/the-method-for-the-use-of-about-pygame-sprite-groupcollide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!