【问题标题】:How do I make my player pass through an enemy when they collide in pygame?当我的玩家在pygame中碰撞时如何让我的玩家穿过敌人?
【发布时间】:2019-08-04 20:19:38
【问题描述】:

我正在测试一款平台游戏。我有一个玩家和一个敌人。然而,当我击中它时,敌人就像一堵墙,玩家无法通过,即使我没有告诉程序去。我还告诉程序在发生碰撞时打印(“Hit”),但没有任何反应。有谁知道如何解决这一问题?注意:我导入了文件,所以这段代码并不都在同一个文件中。

SLIME_WALK = (52, 125, 50, 28)


class Mob(pygame.sprite.Sprite):
    def __init__(self, sprite_sheet_data):
    """ Mob constructor. Assumes constructed with user passing in
        an array of 5 numbers like what's defined at the top of this
        code. """

        # Call the parent's constructor
        super().__init__()

        sprite_sheet = SpriteSheet('enemies_spritesheet.png')
        # Grab the image for this platform
        self.image = sprite_sheet.get_image(sprite_sheet_data[0],
                                        sprite_sheet_data[1],
                                        sprite_sheet_data[2],
                                        sprite_sheet_data[3])

        self.rect = self.image.get_rect()


class Level01(Level):
     def __init__(self, player):

        # Call the parent constructor
        Level.__init__(self, player)
        # Array with type of mob, and x, y location of the mob
        level_enemies = [[mobs.SLIME_WALK, 500, 300]]

        # Go through the array above and add mobs
        for mob in level_enemies:
            enemy = mobs.Mob(mob[0])
            enemy.rect.x = mob[1]
            enemy.rect.y = mob[2]
            enemy.player = self.player
            self.platform_list.add(enemy)


class Player(pygame.sprite.Sprite):


    # -- Methods
    def __init__(self):
        """ Constructor function """

        # Call the parent's constructor
        super().__init__()

    def update(self):
        # See if we hit anything
        mob_hit_list = pygame.sprite.spritecollide(self, 
        self.level.enemy_list, False)
        if mob_hit_list:
            print("Hit")

【问题讨论】:

  • 您问题的编辑历史显示为 3 个完全不同的问题。如果您的问题发生了重大变化,请问一个新问题。我最初对您问题的唯一答案投了反对票,因为它甚至与您的问题无关......然后我看到了您疯狂的编辑历史。
  • 抱歉,我问了一个新问题。你能检查一下吗?

标签: python python-3.x pygame


【解决方案1】:

我猜你可以使用 webbrowser 包来做到这一点

import webbrowser

a_website = "https://www.google.com"

# Open url in a new window of the default browser, if possible
webbrowser.open_new(a_website)

# Open url in a new page (“tab”) of the default browser, if possible
webbrowser.open_new_tab(a_website)

webbrowser.open(a_website, 1) # Equivalent to: webbrowser.open_new(a_website)
webbrowser.open(a_website, 2) # Equivalent to: webbrowser.open_new_tab(a_website)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-26
    • 2013-12-01
    • 2021-05-10
    • 1970-01-01
    • 2023-03-21
    • 2013-04-07
    • 1970-01-01
    相关资源
    最近更新 更多