Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm writing a top down game and I'm trying to figure out how to make certain types of terrains impassable.

I have a dictionary of sprites:

terrains = [{"sprite": black}, {"sprite": dirt}, {"sprite": grass}, {"sprite": water}, {"sprite": mountian}]

And here's my movement code:

    for event in pygame.event.get():
            if event.type == pygame.QUIT: 
                sys.exit()        
            elif event.type == pygame.KEYDOWN:            
                if event.key == pygame.K_RIGHT:
                    right = True

"""I want to check around here
pseudo-code: 
If next terrain has the passable option set to false then dont move if its
set to true then move."""

                if event.key == pygame.K_LEFT:
                    left = True
                if event.key == pygame.K_UP:
                    up = True
                if event.key == pygame.K_DOWN:
                    down = True
            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_RIGHT:
                    right = False
                if event.key == pygame.K_LEFT:
                    left = False
                if event.key == pygame.K_UP:
                    up = False
                if event.key == pygame.K_DOWN:
                    down = False            

        if right == True:            
            lnkx = lnkx + 16
        if left == True:            
            lnkx = lnkx - 16
        if up == True:            
            lnky = lnky - 16
        if down == True:            
            lnky = lnky + 16

So I would have to add a passable option to the "terrains" dictionary and check if the next terrain is passable in the movement code.

share|improve this question
1  
This is a "how to write my code" question. You're asking us to modify this code to do what you want. That's the task of the developer making the game. –  Byte56 Oct 14 at 21:22
 
Can you show us the movement code? It's hard to suggest what you should change in your movement code without knowing what you're doing already. –  amitp Oct 14 at 21:23
 
@amitp The movement code is in there. Right after the "game loop" comment –  Cagentdog Oct 14 at 21:24
 
ok I've rewritten it to make it clearer –  Cagentdog Oct 14 at 21:37
 
Yes, check if the tile you're moving to has terrain that is passable. Before you set variables lnkx, lnky, calculate the new values they will have. Check if the new location is passable. If it is, then copy the new location to lnkx, lnky. –  amitp Oct 14 at 21:44
show 2 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.