0

Note: platforms is the module where I have defined the dimensions of the light and here I am just placing the object into the screen level using the x and y coordinates.

How to make the player and the object 'LIGHT' collide in my game?

Code for the player:

class Player(pygame.sprite.Sprite):
    change_x = 0
    change_y = 0
    walking_frames_l = []
    walking_frames_r = []
    direction = "R"
    level = None

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        sprite_sheet = SpriteSheet("p2_jump.png")
        image = sprite_sheet.get_image(0, 0, 66, 90)
        self.walking_frames_r.append(image)
        image = sprite_sheet.get_image(0, 0, 66, 90)
        image = pygame.transform.flip(image, True, False)
        self.walking_frames_l.append(image)
        self.image = self.walking_frames_r[0]
        self.rect = self.image.get_rect()

Code for the object:

class Level_01(Level):
    def __init__(self, player):
        Level.__init__(self, player)
        self.background = pygame.image.load("120111.png").convert()
        self.background.set_colorkey(setup.WHITE)
        self.level_limit = -2200
        level = [[platforms.GRASS_LEFT, 500, 500],
                 [platforms.GRASS_MIDDLE, 570, 500],
                 [platforms.GRASS_RIGHT, 640, 500],
                 [platforms.GRASS_LEFT, 800, 400],
                 [platforms.GRASS_MIDDLE, 870, 400],
                 [platforms.GRASS_RIGHT, 940, 400],
                 [platforms.GRASS_LEFT, 1000, 500],
                 [platforms.GRASS_MIDDLE, 1070, 500],
                 [platforms.GRASS_RIGHT, 1140, 500],
                 [platforms.GRASS_LEFT, 1120, 280],
                 [platforms.GRASS_MIDDLE, 1190, 280],
                 [platforms.GRASS_RIGHT, 1260, 280],
                 [platforms.GRASS_LEFT, 1870, 400],
                 [platforms.GRASS_MIDDLE, 1940, 400],
                 [platforms.GRASS_RIGHT, 2010, 400],
                 [platforms.GRASS_LEFT, 2080, 280],
                 [platforms.GRASS_MIDDLE, 2150, 280],
                 [platforms.GRASS_RIGHT, 2220, 280],
                 [platforms.GRASS_LEFT, 2350, 450],
                 [platforms.GRASS_MIDDLE, 2420, 450],
                 [platforms.GRASS_RIGHT, 2490, 450],
                 [platforms.LIGHT1, 570, 430],
                 [platforms.LIGHT2, 870, 250],
                 [platforms.LIGHT3, 1190, 150],
                 [platforms.LIGHT4, 1940, 350],
                 [platforms.LIGHT5, 2200, 95],
                 [platforms.LIGHT6, 2420, 350],
                ]
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    What do you mean "make the player and the object 'LIGHT'"? – martineau Jun 25 '16 at 15:02
  • I want to make the player and the object collide. In my case, the object is defined as LIGHT. i have used arrays to place those on the screen – Vanshika Kapoor Jun 26 '16 at 06:31
  • You have multiple `platforms.LIGHT` in the `levels` array, so it's still unclear what you mean by _the_ object 'LIGHT'. Also what do you mean by make the two collide? Folks usually want to _check_ for collisions, not make them. – martineau Jun 26 '16 at 12:51
  • do you mean you want them to move to toward each other until they collide or do you want to know when they have collided? – DCA- Jun 28 '16 at 13:35

0 Answers0