I'm writing a gravity simulation in python and pygame, and had a quick question. Say I have an object that I can move. Lets call it "square". Then I created a line like so...

pygame.draw.line(screen, (0, 0, 0), (84, 368), (510, 368))

How would I tell python not to let "square" go through the line?:?:

Recommended Answers

All 4 Replies

You would need to keep track of the boundary of 'square', which would be the leading edge of movement (this gets really complicated if square is rotating while falling), and then do a comparison to make sure that the leading edge isn't at the line... if it is past the line either stop movement or reverse the direction with the proper exit angle, momentum, and kinetic energy.

Any idea where I could get some example code for something like this?

What you're looking for is collision checking. Off the top of my head, I don't have any examples of it, but a search on 'Python Square Collision Checking' or something like that might turn up an answer.

Will the square rotate, or will it always be aligned with the x/y axes? Because if it doesn't rotate, it would be much simpler to simulate.

as of right now, the square is just x and y. No rotations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.