python - 如何在pygame中只杀死自己的精灵?
问题描述
下午好,
我正在用 Python 编写一个小程序,使用 Pygame,如果 Cell 精灵与食物精灵碰撞,我希望它被杀死。目前,当两个精灵碰撞时,两个精灵都会被杀死。我如何错误地使用 kill 功能?我告诉细胞精灵只是为了杀死它自己。我知道当 Cell 与 Food 碰撞时死亡是违反直觉的,我只是想学习 Pygame 的功能。下面的代码:
import os
import random
#define background color
BACKGROUND = (17, 109, 145)
#define framerate of simulation
FPS = 60
WIDTH, HEIGHT = 1000, 700
WIN = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Cell Evolver")
game_area = pg.Rect(30, 30, 940, 640)
game_area_color = pg.Color('aquamarine2')
# game loop, check collisions, etc...
def main():
food_sprites = pg.sprite.Group()
food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))
food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))
food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))
food_sprites.add(Food((random.randint(400,540),random.randint(250,390)),game_area))
cell_sprites = pg.sprite.Group()
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
cell_sprites.add(Cell(game_area.center, game_area,))
clock = pg.time.Clock()
run = True
while run:
for event in pg.event.get():
if event.type ==pg.QUIT:
run = False
cell_sprites.update(food_sprites)
WIN.fill(BACKGROUND)
cell_sprites.draw(WIN)
food_sprites.draw(WIN)
pg.draw.rect(WIN, game_area_color, game_area, 1)
pg.display.flip()
clock.tick(FPS)
pg.quit()
class Cell(pg.sprite.Sprite):
def __init__(self, pos, game_area):
super().__init__()
self.image = pg.Surface((6, 6))
self.image.fill(pg.Color('aquamarine2'))
self.rect = self.image.get_rect(center=pos)
self.game_area = game_area
def update(self, food_sprites):
randNumx = random.randint(-2,2)
randNumy = random.randint(-2,2)
self.rect.x +=randNumx
self.rect.y +=randNumy
if pg.sprite.spritecollide(self, food_sprites, True):
self.kill()
class Food(pg.sprite.Sprite):
def __init__(self, pos, game_area):
super().__init__()
self.image = pg.Surface((6, 6))
self.image.fill(pg.Color('white'))
self.rect = self.image.get_rect(center=pos)
self.game_area = game_area
if __name__ == "__main__":
pg.init()
main()
pg.quit()
解决方案
从您使用的方法的文档中:
dokill 参数是一个布尔值。如果设置为 True,所有发生碰撞的 Sprite 将从组中移除。
您是否尝试将 bool 设置为False
?
if pg.sprite.spritecollide(self, food_sprites, False):
self.kill()
参考文档
推荐阅读
- ios - Flutter fastlane ios如何通过--dart-define
- graphql - 有没有办法避免在 Apollo GraphQL 中返回具有空值的键?
- java - 如何在 JavaFX 中的 tabpane 之间添加拆分?
- django - 加载图像时没有这样的文件或目录
- asp.net-core - 如何从 asp.net core 3.1 项目的已发布文件创建整个解决方案?
- docker - Golang,Docker,外部包,找不到路径
- flutter - 向下滚动 ListviewItmes 时,强制屏幕小部件的顶部继续显示,颤振
- mongodb - 使用它们的 _ids 作为比较来删除文档
- c# - 使用 IdentityServer4 和 DataProtection API 进行令牌签名
- java - 用postgresql中的查询替换java循环