简体   繁体   中英

Implementing Collision of a list and a object

I've been trying to implement collision of a list and an image. The list is supposed to be a map, and the player/image isn't supposed to be able to move through those walls. The walls are getting resembled by 1´s on the map and the non-walls as 0. I make the map by using

def draw_wall():
    pygame.draw.rect(fenster, ORANGE, [kor(spalte) + 1, kor(reihe) + 1, kor(1) - 1, kor(1) - 1])

and then using

for x in range(0, 43):
    for y in range(0, 29):
        if map[y][x] != 0:
            draw_wall(x, y)

The map looks kinda like this:

map[
    [0, 0, 1, 0, 0, 0],
    [0, 0, 1, 1, 0, 0],
    [0, 1, 1, 1, 0, 0]
]

I've tried using colliderect , collidelist and collidelist , but that didn't work. I've also tried replacing the image with a black box, detecting for collision and then putting the image/player up again, but that didn't work either. Thanks for your help. I'm very new to programming so I'm not quite sure how to poste questions here, so you can give me tips on that as well. I can show the entire code too if you need it.

PS: I've been trying to solve this problem for one and a half hour, but non of the other solutions helped me(although there were definitely right solutions there).

I did it now by impementing a counter, which goes up one(x) when you move the right and down one when it goes to the left and then I check for collision with

if karte [counter_x][counter_y] !=0:
     movement_x=0
     movement_y=0

I also have a x_counter_reset var which gets set to 1 or 2 if there is a key right/left input, which I use to reset the x counter prober if there is a collision. There are definetly way more effective ways than this, but it works I guess. I´d still like to hear a better way to do this for the future tho.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM