Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';启动游戏后,我看不到敌人的精灵_Python_Python 3.x_Pygame_Sprite - Fatal编程技术网

Python Can';启动游戏后,我看不到敌人的精灵

Python Can';启动游戏后,我看不到敌人的精灵,python,python-3.x,pygame,sprite,Python,Python 3.x,Pygame,Sprite,我正在学习python,现在我正在玩pygame。我从教程开始就写了一些教程游戏,但在图坦卡蒙自动启动后,敌人的春天从右向左移动。我一整天都在寻找一个bug,我的代码有没有任何变化,但看不到任何变化,但在我启动我的应用程序后(使用Replit可能很重要),只有玩家精灵,没有敌人。我做错了什么?为什么我的敌人没有产卵 代码如下 class Enemy(pygame.sprite.Sprite): def __init__(self): super(Enemy, self).__init__

我正在学习python,现在我正在玩pygame。我从教程开始就写了一些教程游戏,但在图坦卡蒙自动启动后,敌人的春天从右向左移动。我一整天都在寻找一个bug,我的代码有没有任何变化,但看不到任何变化,但在我启动我的应用程序后(使用Replit可能很重要),只有玩家精灵,没有敌人。我做错了什么?为什么我的敌人没有产卵

代码如下

class Enemy(pygame.sprite.Sprite):
def __init__(self):
    super(Enemy, self).__init__()
    self.surf = pygame.Surface((20, 10))
    self.surf.fill((255, 255, 255))
    self.rect = self.surf.get_rect(
        center=(
            random.randint(screen_WIDTH + 20, screen_WIDTH + 100),
            random.randint(0, screen_HEIGHT),
        )
    )
    self.speed = random.randint(5, 20)

# Move the sprite based on speed
# Remove the sprite when it passes the left edge of the screen
def update(self):
    self.rect.move_ip(-self.speed, 0)
    if self.rect.right < 0:
        self.kill()


# Create groups to hold enemy sprites and all sprites
# - enemies is used for collision detection and position updates
# - all_sprites is used for rendering
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)

好的,我发现了一个问题。有时有人带你去看一个地方,你就开始在附近看。多亏了@JanWilamowski,一切都很好,除了触发addEnemy的事件。。。一个缩进太多。

敌人精灵是由自定义的
addEnemy
事件创建的。但是,代码中没有触发此事件的地方。你忘了发布什么东西了吗?@JanWilamowski这是一个看起来像elif事件的事件。类型==addEnemy:#创建新的敌人并将其添加到精灵组新的敌人=敌人()敌人。添加(新的敌人)所有精灵。添加(新的敌人)
while running:
# Look at every event in the queue
for event in pygame.event.get():
    if event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            running = False
        elif event.type == QUIT:
            running = False
        # Add a new enemy?
        elif event.type == addEnemy:
            # Create the new enemy and add it to sprite groups
            new_enemy = Enemy()
            enemies.add(new_enemy)
            all_sprites.add(new_enemy)

# Get the set of keys pressed and check for user input
pressed_keys = pygame.key.get_pressed()

player.update(pressed_keys)

enemies.update()

# Fill the screen with black
screen.fill((0, 0, 0))
# Draw all sprites
for entity in all_sprites:
    screen.blit(entity.surf, entity.rect)

if pygame.sprite.spritecollideany(player, enemies):
  player.kill()
  print("You lose")
  running = False

# Update the display
pygame.display.flip()