Pygame扫雷项目代码仅加print()才生效,无print则失效求助
Pygame扫雷项目代码异常问题
问题现象
开发Pygame扫雷项目时遇到奇怪问题:一段负责碰撞检测并在碰撞精灵上方绘制新精灵的代码,只有在碰撞判断后添加print("abc")语句时才能正常执行;去掉该打印语句后,代码完全失效,没有任何预期效果。
有效代码(可正常运行)
for Sprite in self.CellsSprites: if Sprite.rect.colliderect(self.rect): print("abc") if time.time() - self.time > 0.1 and self.block == False: self.block = True self.time = time.time() Cell_Hold((Sprite.rect.x,Sprite.rect.y),{self.CellsSprites}) break
无效代码(无法正常运行)
for Sprite in self.CellsSprites: if Sprite.rect.colliderect(self.rect): if time.time() - self.time > 0.1 and self.block == False: self.block = True self.time = time.time() Cell_Hold((Sprite.rect.x,Sprite.rect.y),{self.CellsSprites}) break
代码功能说明
这段代码用于遍历精灵组self.CellsSprites,检测与当前对象的碰撞;当满足时间间隔(与上次操作间隔超过0.1秒)且未处于阻塞状态时,调用Cell_Hold函数在碰撞精灵的位置绘制新精灵,并设置阻塞状态避免重复触发。
内容的提问来源于stack exchange,提问作者user20125225
相关产品推荐
相关产品推荐

