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],
]