I made a game map like this
class Game_map:
def __init__(self):
self.land = pygame.image.load(r"C:\Users\name\OneDrive\Documents\A level python codes\final game\land.png").convert()
self.height = 200
self.width = 200
self.land = pygame.transform.scale(self.land, (420, 250))
self.land.set_colorkey((0, 0, 0))
self.map_width = 6000
self.map_height = 2000
# 0 = emepty
# 1 = land
self.layout = [[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0 ],
[ 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0 ]]
def draw_map(self):
y = 0
for layer in self.layout:
x = 0
for land in layer:
if land == 1:
D.blit(self.land, (x * 400 - scroll[0], y * 250 - scroll[1]))
if land == 0:
pass
x += 1
y += 1
game_map = Game_map()
I have a player class as well with self.x
, self.y
, self.width
and self.height
. How would i implement collision detection with this? My initial idea was that i would use self.x
and self.y
in the draw_map()
function so that i could later check for collisions like this
def draw_map(self):
for layer in self.layout:
for land in layer:
if land == 1:
D.blit(self.land, (self.x * 400 - scroll[0], self. y * 250 - scroll[1]))
if land == 0:
pass
x += 1
y += 1
But when i do this, nothing gets drawn on the screen. I also tried adding x
and y
values in the original draw_map
function mentioned above to a list and detect collision with that but it got really messy and didnt work. Is there any other way to do collision detection ? Thanks
The straight forward method is to find the collisions of the player rectangle and the obstacle rectangles.
Create a list of the field indices of all obstacles:
Setup a
pygame.Rect
for the player:Use
colliderect()
to find the collisions of the player and the obstacles:The more tricky by far more efficient method is to compute the field indices for the 4 corners of the player rectangle and to evaluate if one of the corners is in a field with an obstacle.
Setup the player rectangle:
Compute the coordinates for corners of the rectangle:
Compute the field indices, where the corner points are on:
Evaluate if any of the field contains an obstacle: