I need to collision between a “player” sprite and “cave” sprite.
My Cave sprite is currently set up as multiple rectangles which serve as the room interior, 1 large rectangle in the centre and 3 smaller rectangles as passageways to the edges of the screen. How can I detect collision between the player and room? Should I modify my setup so the borders of the rooms are rectangles instead?
OLD CODE, JUST READ DOCUMENTATION AND AM MAKING IMPROVEMENTS
gist of class setups
links = [
[8,5,0,2],
[10,1,0,3],
[12,2,0,4],
[14,3,0,5],
[6,4,0,1],
[0,15,5,7],
[17,6,0,8],
[0,7,1,9],
[18,8,0,10],
[0,9,2,11],
[19,10,0,12],
[0,11,3,13],
[20,12,0,14],
[0,13,4,15],
[16,14,0,6],
[0,20,15,17],
[0,16,7,18],
[0,17,9,19],
[0,18,11,20],
[0,19,13,16],
]
class Cave(pygame.sprite.Sprite):
def __init__(self, cave_number, cave_links):
super().__init__()
self.cave = cave_number
self.links = cave_links
self.things = []
self.doorwidth = 200
self.doorlength = 200
self.sizex = 960
self.sizey = 540
global screenx, screeny
self.surface = pygame.Surface((self.sizex, self.sizey))
self.surface.fill((255, 255, 255))
self.passage = [pygame.Surface((self.doorwidth, self.doorlength)),
pygame.Surface((self.doorlength, self.doorwidth)),
pygame.Surface((self.doorwidth, self.doorlength)),
pygame.Surface((self.doorlength, self.doorwidth))]
self.positions = [(screenx//2 - self.doorwidth//2, screeny//2 - self.sizey//2 - self.doorlength),
(screenx//2 + self.sizex//2, screeny//2 - self.doorwidth//2),
(screenx//2 - self.doorwidth//2, screeny//2 + self.sizey//2),
(screenx//2 - self.sizex//2 - self.doorlength, screeny//2 - self.doorwidth//2)]
self.rects = [((self.sizex, self.sizey), (screenx//2 - self.sizex//2, screeny//2 - self.sizey//2)),
((self.doorwidth, self.doorlength), (screenx//2 - self.doorwidth//2, screeny//2 - self.sizey//2 - self.doorlength)),
((self.doorlength, self.doorwidth), (screenx//2 + self.sizex//2, screeny//2 - self.doorwidth//2)),
((self.doorwidth, self.doorlength), (screenx//2 - self.doorwidth//2, screeny//2 + self.sizey//2)),
(self.doorlength, self.doorwidth, screenx//2 - self.sizex//2 - self.doorlength, screeny//2 - self.doorwidth//2)]
del self.rects[self.links.index(0)]
for i, l in enumerate(self.links):
if l != 0:
self.passage[i].fill((255, 255, 255))
def render(self):
displaysurface.blit(self.surface, (screenx//2 - self.sizex//2, screeny//2 - self.sizey//2))
for i, l in enumerate(self.links):
if l != 0:
displaysurface.blit(self.passage[i], self.positions[i])
class Char(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.size = 30
self.surf = pygame.Surface((self.size, self.size))
self.surf.fill((240,200,140))
self.pos = vec((500,500))
self.rect = self.surf.get_rect()
self.speed = 10
self.xv = 0
self.yv = 0
global roomin
def walk(self):
global roomin
self.xv = self.xv*0.9
self.yv = self.yv*0.9
keys = pygame.key.get_pressed()
if keys[K_w]:
self.yv = self.speed
if keys[K_s]:
self.yv = -self.speed
if keys[K_d]:
self.xv = self.speed
if keys[K_a]:
self.xv = -self.speed
## if self.xv != 0 and self.yv != 0:
## self.xv /= math.sqrt(2)
## self.yv /= math.sqrt(2)
if self.pos.x > 1280:
self.pos.x = 0
roomin = room[roomin].links[1]
print(roomin)
if self.pos.x < 0:
self.pos.x = 1280
roomin = room[roomin].links[3]
print(roomin)
if self.pos.y > 720:
self.pos.y = 0
roomin = room[roomin].links[2]
print(roomin)
if self.pos.y < 0:
self.pos.y = 720
roomin = room[roomin].links[0]
print(roomin)
self.pos += vec(self.xv, -self.yv)
self.rect = self.pos
player = Char()
roomin = random.randint(1,20)
for i, l in enumerate(links):
room[i+1] = Cave(i+1, l)`
New contributor
Baldi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.