#🔒 Help me with some pygame issues

66 messages · Page 1 of 1 (latest)

finite oasis
#

Is anyone here skilled in pygame and could help me with a collision problem im having

odd idolBOT
#

@finite oasis

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ancient oar
#

Maybe describe the issue a bit more and give an example of what not working

#

Then it will be easier to know how to help

finite oasis
# ancient oar Maybe describe the issue a bit more and give an example of what not working

I am creating a treasure hunt game and i made a map outline and created collision for the doors, but my issue is I want to make it so the character cannot leave the map outline once it goes through the doors, tbh im not sure how to do it, I want to make it so if the game detects the player colliding with the map and not the door it will stop it from going through

ancient oar
#

Do you have maybe a picture or something of what you’re talking about

finite oasis
ancient oar
#

You should send the code to the paste bin

finite oasis
#

!paste

odd idolBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

ancient oar
#

But I’d also like to see a picture of the game running so I know what you mean

#

By doors

finite oasis
finite oasis
#

the red parts are the doors in the map

#

and i already made a collision for them so the character gets pushed so it looks like it goes through the door but my only issue right now is that my character can move freely through the map outlines

#

u control the character with arrow keys btw

ancient oar
#

Ah

#

You are directly drawing the rects for the outline

#

Instead I’d make rect variables

#

Then draw those

#

So then later on you have access to the rect for each outline

finite oasis
#

Are you talking about the red lines?

ancient oar
#

The black

finite oasis
#

ah

#

I used rects for the map outline

#

bc i planned on adding the doors to the map so i wouldnt have to make lines for each part

ancient oar
#

Here’s what I’m saying

#

draw.rect(screen, BLACK, (460, 125, 240, 170), 4)

#

You currently have something like this

#

Instead

#

rect1 = pygame.Rect(460, 125, 240, 170)

draw.rect(screen, BLACK,rect1, 4)

#

Now you aren’t directly drawing

finite oasis
#

Ohhhh I see what you mean

ancient oar
#

You have it saved in a variable

#

So you can check collision with it later

finite oasis
#

Ah that makes sense, how would I create a collision for the rects though, because for my walls I knew which direction i wanted it to go in, I just made it so if either move in the x or y axis, for the map outline the character can come from any part of the outline

#

Sry if the question sounds dumb im new to pygame lol

ancient oar
#

So what I would do

#

Is use

#

.collideRect()

finite oasis
#

One of my friend recommended to use the old position of the character, so if it detects it hitting the outline it will send it to its old pos x or y

ancient oar
#

And before moving the player, check if it would be colliding

#

Instead of sending the player to its old x and y

#

You can

#

Calculate the new x and y before the player moves

#

And see it that would have a collision

#

If it does you don’t move the player

#

Does that make sense

#

Basically you catch the collision before it happens. Instead of letting the player collide and sending it back to the old x y

#

Both ways work

finite oasis
#

Yeah that makes sense, how would I create that code though?

#

a new variable called new x and new y?

ancient oar
#

Sure

#

You make a new rect

#

That has the same position as the player

#

Except it’s shifted by the amount the player is about to move

#

so you make a rect with

#

(playerx + xshift, playery + yshift, playerwidth, playerheight)

finite oasis
#

ahh I see

#

let me try rq

ancient oar
#

Good luck

finite oasis
odd idolBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.