Ask coding questions

← Back to all posts
Obstacles In A Maze
RYANTADIPARTHI (6000)

Hi, I am developing a random maze for no reason, and I want to add something the user can move his object to get the gold. But I don't know how to make it so, something happens when the user's object touches the gold.

Help would be appreciated
Thanks!

Comments
hotnewtop
Coder100 (16982)

From what I can see:

# ... snip ...
# define these
gold_x = 5
gold_y = 10

# ... snip ...
# add this function somewhere
def checkGotGold():
  return char_x == gold_x and char_y = gold_y

# ... snip ...
# this is line 45
        while move:
            print("You are: X") 
            print("Move left, right, up, down? WASD") 
            control = getch.getch()
            control = control.lower() 
            if control == "a": 
                char_x = char_x - 1
            elif control == "d":
                char_x = char_x + 1
            elif control == "w": 
                char_y = char_y - 1
            elif control == "s": 
                char_y = char_y + 1
            else:
                print("Invalid. Choose WASD")
            arena[prev_char_y][prev_char_x] = "O" 
            arena[char_y][char_x] = green + "X" + reset 
            checkGotGold() # this is the new line of code
            prev_char_y = char_y
            prev_char_x = char_x
            make_list(arena)
RYANTADIPARTHI (6000)

@Coder100 ok, so i tried this code, but you had some errors, i fixed it. but, you're just giving the indexes of gold, not the object. Could you also make the object. Like for the player it's a X so for gold, so the user can see it, can you show how to put an object? Thanks!

RYANTADIPARTHI (6000)

@Coder100 ok, nvm, i did that too, so this is my code:

prev_char_y = 13
prev_char_x = 13
char_y = 13
char_x = 13
arena[char_x][char_y] = green + "X" + reset
gold_x = 5
gold_y = 10
arena[gold_x][gold_y] = yellow + "T" + reset
make_list(arena) 


def checkGotGold():
    global gold_x, gold_y
    return char_x == gold_x and char_y == gold_y

move = True 
while True:
    try:
        while move:
            print("You are: X") 
            print("Move left, right, up, down? WASD") 
            control = getch.getch()
            control = control.lower() 
            if control == "a": 
                char_x = char_x - 1
            elif control == "d":
                char_x = char_x + 1
            elif control == "w": 
                char_y = char_y - 1
            elif control == "s": 
                char_y = char_y + 1
            else:
                print("Invalid. Choose WASD")
            arena[prev_char_y][prev_char_x] = "O" 
            arena[char_y][char_x] = green + "X" + reset 
            checkGotGold() 
            prev_char_y = char_y 
            prev_char_x = char_x 
            make_list(arena)
    except IndexError:
        print("That's out of range, please pick somewhere else to move")
        char_y = prev_char_y
        char_x = prev_char_x
        arena[char_y][char_x] = red + "X" + reset
        make_list(arena)

So everything seems to be working. only thing is, i want it to print good when the X has collided with the T or gold object. Could yo ushow how to do that?

Thanks!

realTronsi (906)

@RYANTADIPARTHI why is the player part of the arena array. The player should have an X and Y coord, and should not be in place of the array for obvious reasons.

Then you can just see if their coords are matching to determine if the player is touching the gold

Coder100 (16982)

@RYANTADIPARTHI

prev_char_y = 13
prev_char_x = 13
char_y = 13
char_x = 13
arena[char_x][char_y] = green + "X" + reset
gold_x = 5
gold_y = 10
arena[gold_x][gold_y] = yellow + "T" + reset
make_list(arena) 


def checkGotGold():
    global gold_x, gold_y
    return char_x == gold_x and char_y == gold_y

move = True 
while True:
    try:
        while move:
            print("You are: X") 
            print("Move left, right, up, down? WASD") 
            control = getch.getch()
            control = control.lower() 
            if control == "a": 
                char_x = char_x - 1
            elif control == "d":
                char_x = char_x + 1
            elif control == "w": 
                char_y = char_y - 1
            elif control == "s": 
                char_y = char_y + 1
            else:
                print("Invalid. Choose WASD")
            arena[prev_char_y][prev_char_x] = "O" 
            arena[char_y][char_x] = green + "X" + reset 
            if checkGotGold():
                print("Good") # New line of code
            prev_char_y = char_y 
            prev_char_x = char_x 
            make_list(arena)
    except IndexError:
        print("That's out of range, please pick somewhere else to move")
        char_y = prev_char_y
        char_x = prev_char_x
        arena[char_y][char_x] = red + "X" + reset
        make_list(arena)
RYANTADIPARTHI (6000)

@realTronsi uhh, soo, then if possible, could you show it how in MY code?

RYANTADIPARTHI (6000)

@Coder100 it's still not working. When i move the player , and it touches the T, it does nothing, and just goes on.

Coder100 (16982)

from what I can see, that's what he's doing, but he's just also putting it inside the array so his drawer can draw it later. @realTronsi

realTronsi (906)

@Coder100 yeah ik but he is editing the actual arena which is bad, and will cause issues later

Coder100 (16982)

oh, refactor it to:

            arena[prev_char_y][prev_char_x] = "O" 
            arena[char_y][char_x] = green + "X" + reset 
            prev_char_y = char_y 
            prev_char_x = char_x 
            make_list(arena)
            if checkGotGold():
                print("Good") 

@RYANTADIPARTHI

RYANTADIPARTHI (6000)

@Coder100 yeah, so... do you have the solution?

Coder100 (16982)

yes, for one, if he forgets to update some objects, they completely disappear when the player goes over them. @realTronsi

Coder100 (16982)

did my refactored code not work? @RYANTADIPARTHI

RYANTADIPARTHI (6000)

@Coder100 nope, still the same. It's still not working.

realTronsi (906)

@Coder100 yes but he will have to keep track of the object under the player and all that and it's just bad. It will cause the game to be slower and less efficient as well as needing more development time and likely more bugs. Just when you render if you hit the player coord render the player instead of the object. It won't slow down as much in the long term and saves you from plenty of bugs in the long term

badst (655)

@realTronsi @coder100 here's something i made for my rpg, and it could work here:

collision = grid[self.ops[direction](player.position,self.directionOP[direction])]

So grid is the array, where the objects are nested. self.ops stores the operators: so

self.ops = { "d": operator.add, "a": operator.sub,"w":operator.sub,"s":operator.add}

Because D +=1, A -=1 in terms of moving the player.
Direction is obviously, the direction of which the player is moving.
Then we pass in the player's position, with player.position. I calculated this with grid index.
self.position = grid.index("CHARACTER FOR PLAYER")
And self.directionOP stores the amount to add/subtract:
self.directionOP = {"d":1,"a":1,"w":20,"s":20}
Because operator returns a value from 2 values, i pass through the int player.position, followed by the amount to add/sub.
the value of w and s is 20 because that is the length of my board.

Then, you just can just do:

if collision in objects.keys():
	print('blocked!')

And objects would be an dict of symbols and values representing obstacles, such as

objects={
	'l': 'loot',
	'm': 'monster'
}

Remember that this relies on class objects! if you aren't using classes, try replacing the variables.

TLDR

calculate the element's below/above/left/right you's value, and determine if it's a obstacle.

Edit:
remember to

import operator 
Coder100 (16982)

yeah, that's what I would do @pepelaugh

badst (655)

@Coder100
Also works well with movement.

self.position =self.ops[direction](self.position,self.directionOP[direction])

probably not the best but still efficient

badst (655)

@Coder100 also why is OP using x,y lol you can just rely on the array: here's ref: https://repl.it/@pepelaugh/rpg#main.py

Coder100 (16982)

@pepelaugh oh yes, right, forgot about that

badst (655)

@Coder100 works but it's there's not much of a point to that

RYANTADIPARTHI (6000)

@Coder100 have you gotten a solution?

realTronsi (906)

@Coder100 @pepelaugh silly gooses tuples are immutable

badst (655)

@RYANTADIPARTHI dude i solved it just take my code lol.

RYANTADIPARTHI (6000)

@pepelaugh could you show me your code?

badst (655)

@RYANTADIPARTHI I literally gave you it lol read the comment smh. heres a repl but don't steal it all and credit me. https://repl.it/@pepelaugh/rpg#main.py

RYANTADIPARTHI (6000)

@pepelaugh listen, i don't need your code. I just need how to implement printing something when the object in MY code collides with something. You didn't explain that.

Coder100 (16982)

so what part isn't working anyways @RYANTADIPARTHI

badst (655)

@RYANTADIPARTHI I clearly did in my comment. Are you so incompetent that you cannot add what I laid out? Not to say that you literally asked for my code.

If you want me to add it invite me, ill be on in like 1 hour, im doing something on discord.

RYANTADIPARTHI (6000)

@Coder100 when the user's object collides, i want it to print something.

RYANTADIPARTHI (6000)

@pepelaugh ok, i'll invite, but not now, I'm in school.

badst (655)

@RYANTADIPARTHI Alright, sorry for the rant but you can't expect everything to be done for you, the struggle is the part you learn.

Coder100 (16982)

Well, I would like to say I don't see any gold?

goldx = 10
goldy = 10
score = 0
playerx = 0
playery = 0

def executeThisFunctionEachTimeThePlayerMoves():
  if playerx == goldx and playery == goldy:
    score += 1

read the function name

RYANTADIPARTHI (6000)

@Coder100 yeah, you don't see any gold, because that's the question i asked, but ok. Anyway, could you give me some code, based on my type of code. Meaning like, could you explain how i could implement this into my code? Thanks!

Coder100 (16982)

wait is this a snake game? Why is there a prev_char_x? @RYANTADIPARTHI

RYANTADIPARTHI (6000)

@Coder100 no, this is not a snake game. This is a maze game for absolutely no reason.