Skip to content
← Back to Community
Obstacles In A Maze
Profile icon
RYANTADIPARTHI

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!

Voters
Profile icon
AliciaCanizalez
Profile icon
MemeManHimself
Profile icon
TheDevBest
Profile icon
chillcafe
Profile icon
Dominicl645
Profile icon
RYANTADIPARTHI
Comments
hotnewtop
Profile icon
Coder100

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)
Profile icon
RYANTADIPARTHI

@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!

Profile icon
RYANTADIPARTHI

@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!

Profile icon
Coder100
Profile icon
realTronsi

@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

Profile icon
Coder100

@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)
Profile icon
RYANTADIPARTHI

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

Profile icon
RYANTADIPARTHI

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

Profile icon
Coder100

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

Profile icon
realTronsi

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

Profile icon
Coder100
Profile icon
Coder100

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

Profile icon
RYANTADIPARTHI

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

Profile icon
Coder100

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

Profile icon
Coder100

did my refactored code not work? @RYANTADIPARTHI

Profile icon
RYANTADIPARTHI

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

Profile icon
RYANTADIPARTHI
Profile icon
realTronsi

@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

Profile icon
badst

@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
Profile icon
Coder100

yeah, that's what I would do @pepelaugh

Profile icon
badst

@Coder100
Also works well with movement.

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

probably not the best but still efficient

Profile icon
badst

@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

Profile icon
Coder100

@pepelaugh oh yes, right, forgot about that

Profile icon
Coder100

how about tuple @pepelaugh

Profile icon
badst

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

Profile icon
RYANTADIPARTHI

@Coder100 have you gotten a solution?

Profile icon
realTronsi

@Coder100 @pepelaugh silly gooses tuples are immutable

Profile icon
badst

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

Profile icon
badst
Profile icon
RYANTADIPARTHI

@pepelaugh could you show me your code?

Profile icon
realTronsi

@pepelaugh ... tuples are immutable

Profile icon
badst

@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

Profile icon
RYANTADIPARTHI

@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.

Profile icon
Coder100

so what part isn't working anyways @RYANTADIPARTHI

Profile icon
badst

@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.

Profile icon
RYANTADIPARTHI

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

Profile icon
RYANTADIPARTHI

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

Profile icon
badst

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

Profile icon
RYANTADIPARTHI
Profile icon
Coder100

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

Profile icon
RYANTADIPARTHI

@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!

Profile icon
Coder100
Profile icon
Coder100

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

Profile icon
RYANTADIPARTHI

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