本文介绍了关于pygame.sprite.groupcollide的使用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看这段代码(她可能有点长):
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
为什么她一直印着Yoshi"?我只想打印一次,球和石头的碰撞.(我的英文很差)
Why does she keeps printing 'Yoshi'? I just want to print it once, the collision of the Ball and Stone. (My English is poor)
推荐答案
groupcollide
只运行一次.尝试将 groupcollide
移动到 update
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))
此外,游戏逻辑在显示已经绘制后更新.除非您出于某种原因想要这种行为,否则您可以在主循环的最后调用 pygame.display.update
.
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()
这篇关于关于pygame.sprite.groupcollide的使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!