Search code examples
pygamecollision-detectionpython-3.4

How can I use the coordinates of my player and the coordinates of any block in an array to detect collision?


I have array of Lists:

blocks0 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1 ,1 ,1 ,1]
blocks1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks2 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks3 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks4 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]                   
blocks5 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks6 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks7 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks8 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks9 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks10= [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
blocks11= [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1 ,1 ,1 ,1 ,1]

blockList = [blocks0, blocks1, blocks2, blocks3, blocks4, blocks5, blocks6, blocks7, blocks8, blocks9, blocks10, blocks11]

A 1 signifies a block. A 0 signifies an open space. Each digit is a 50x50 pixel block/space. Using blockList[y][x] I can find each number seperately.

y signifies which list in the group of lists. x signifies which number in the list y.

However I did not use x and y in my code for this.

I have a character who is 50x50 pixels and moves 10 pixels every loop if i use the arrow keys.

How can I detect collision between my player and a number 1?

I have tried:

lefX = x
rigX = x + player_width
topY = y
botY = y + player_height

lefX -= int(lefX % 50)
rigX -= int(rigX % 50)
topY -= int(topY % 50)
botY -= int(botY % 50)

lefX = int(lefX/50)
rigX = int(rigX/50)
topY = int(topY/50)
botY = int(botY/50)

if blockList[topY][lefX] == 1:     ##This is just for going left.
    if not((blockList[topY][lefX]*50)+50 >= x):
        x += x_changeLeft
else:
    x += x_changeLeft

I did try some other ways but I got rid of them when they didn't work.

Any help is appreciated. Any questions will be answered as soon as possible.

Thanks


Solution

  • (I have changed the block size to 20, but you can easily adjust it back.) Use z/x/p/l to move the player.

    import pygame, sys
    from pygame.locals import *
    
    blocks0 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1 ,1 ,1 ,1]
    blocks1 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks2 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks3 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks4 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]                   
    blocks5 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks6 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks7 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks8 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks9 = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks10= [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,1]
    blocks11= [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1 ,1 ,1 ,1 ,1]
    
    blockList = [blocks0, blocks1, blocks2, blocks3, blocks4, blocks5, blocks6, blocks7, blocks8, blocks9, blocks10, blocks11]
    
    class player:
    
        def __init__(self):
            self.row = 3
            self.col = 4
            self.leftcount = 0
            self.rightcount = 0
            self.upcount = 0
            self.downcount = 0
    
    pygame.init()
    size = width, height = 510, 700
    screen = pygame.display.set_mode(size)
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((0,0,0))
    
    def init():
        global theplayer, clock
        theplayer = player()
        clock = pygame.time.Clock() 
    
    def run():
    
        global theplayer
    
        while True:
            events = pygame.event.get()
            for event in events:
                if event.type == pygame.QUIT:
                    sys.exit()
    
            keys=pygame.key.get_pressed()
    
            if keys[K_z]:
                theplayer.leftcount+=1
                if theplayer.leftcount == 5:
                    theplayer.leftcount = 0
                    if theplayer.col>0:
                        theplayer.col -=1
                        if blockList[theplayer.row][theplayer.col] == 1:
                            print "hit"
    
            if keys[K_x]:
                theplayer.rightcount+=1
                if theplayer.rightcount == 5:
                    theplayer.rightcount = 0
                    if theplayer.col<15:
                        theplayer.col +=1
                        if blockList[theplayer.row][theplayer.col] == 1:
                            print "hit"
    
            if keys[K_p]:
                theplayer.upcount+=1
                if theplayer.upcount == 5:
                    theplayer.upcount = 0
                    if theplayer.row>0:
                        theplayer.row -=1
                        if blockList[theplayer.row][theplayer.col] == 1:
                            print "hit"
    
            if keys[K_l]:
                theplayer.downcount+=1
                if theplayer.downcount == 5:
                    theplayer.downcount = 0
                    if theplayer.row <11:
                        theplayer.row +=1
                        if blockList[theplayer.row][theplayer.col] == 1:
                            print "hit"
    
            clock.tick(30)  
            update()
    
    def update():
    
            background.fill((0,0,0))
            size = 20
    
            for row in range(12):
                for col in range(16):
                    if blockList[row][col] == 1:
                        pygame.draw.rect(background, (255,255,255), (size*col, size*row, size, size))
    
            pygame.draw.rect(background, (255,255,255), (theplayer.col*size, theplayer.row*size, size, size))
            screen.blit(background, (0, 0))
            pygame.display.flip()
    
    
    if __name__ == "__main__":
        init()
        run()