每当我在游戏中击中硬币(收藏品)时,它们都必须从屏幕上完全消失,但这不会发生。击中后它确实消失了,但当我位于硬币位置时,它仍然存在,从而增加了我的得分
这是检查碰撞的代码:
# This function works for Coins Only
def coinsHit_or_not(self, hero, thecoin):
hit = pygame.sprite.collide_rect(hero, thecoin)
if hit:
thecoin.hit()
self.score += 2
和我的硬币类(它包含hit()函数):
class Coins(pygame.sprite.Sprite):
coinsFadeAway = [pygame.image.load('CoinsFadeAway/CS1.png'), pygame.image.load('CoinsFadeAway/CS2.png'),
pygame.image.load('CoinsFadeAway/CS3.png'), pygame.image.load('CoinsFadeAway/CS4.png'),
pygame.image.load('CoinsFadeAway/CS5.png'), pygame.image.load('CoinsFadeAway/CS6.png'),
pygame.image.load('CoinsFadeAway/CS7.png'), pygame.image.load('CoinsFadeAway/CS8.png'),
pygame.image.load('CoinsFadeAway/CS9.png'), pygame.image.load('CoinsFadeAway/CS10.png'),
pygame.image.load('CoinsFadeAway/CS11.png')]
def __init__(self, x, y):
super().__init__()
self.x = x
self.y = y
self.coinTouched = False
self.coins = pygame.image.load('CoinOG_Small.png')
self.coinSpin = 0
self.rect = self.coinsFadeAway[0].get_rect().move(x, y)
def draw(self, window):
if not self.coinTouched:
window.blit(self.coins, (self.x, self.y))
if self.coinSpin < 11:
window.blit(self.coinsFadeAway[self.coinSpin], (self.x, self.y))
self.coinSpin += 1
elif self.coinSpin > 11:
self.coinTouched = False
def hit(self):
self.coinTouched = True
有人可以帮助我弄清楚如何从屏幕上或周围完全删除该硬币精灵,以使硬币消失但当我停留在硬币位置时分数不会增加
答案 0 :(得分:0)
仅当尚未击中“硬币”时,才检查碰撞:
def coinsHit_or_not(self, hero, thecoin):
if not thecoin.coinTouched:
hit = pygame.sprite.collide_rect(hero, thecoin)
if hit:
thecoin.hit()
self.score += 2
或将方法添加到类Coins
中进行碰撞测试:
class Coins(pygame.sprite.Sprite):
# [...]
def collide(self, hero)
if self.coinTouched:
return False
self.coinTouched = pygame.sprite.collide_rect(hero, self)
return self.coinTouched
def coinsHit_or_not(self, hero, thecoin):
if thecoin.collide(hero)
self.score += 2