Jump to content
  • Advertisement
Sign in to follow this  
Crazylegs830

Need help with Python/Pygame Collision

This topic is 2113 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

 

I read up and the def keyword in python means that it is a function. A function is something that is initialized and then you can call the function whenever you want.

So put this code:

def is_within_borders():
    if y < 50:
        y = 50
    elif y > 1080:
        y = 1080
    if x < 50:
        x = 50
    elif x > 1550:
        x = 1550
    return True

Somewhere outside the loop. At the start of your code maybe.

Then you need to call the function when you want to use it so do this

x+=movex
y+=movey
is_within_borders()

Ugh Still not working.

Share this post


Link to post
Share on other sites
Advertisement

So after not understanding why it does not work, I downloaded python and pygame. And found out some pretty interesting things about it. The reason the function won't work is because x,y are not global variables. So instead of using a function just pop this in after you move:

 x+=movex
 y+=movey

#Checking for out of bounds
if y < 0:
    y = 0
elif y > 1080:
    y = 1080
if x < 0:
    x = 0
elif x > 1550:
    x = 1550
Edited by PragmaOnce

Share this post


Link to post
Share on other sites

 

So after not understanding why it does not work, I downloaded python and pygame. And found out some pretty interesting things about it. The reason the function won't work is because x,y are not global variables. So instead of using a function just pop this in after you move:

 x+=movex
 y+=movey

#Checking for out of bounds
if y < 0:
    y = 0
elif y > 1080:
    y = 1080
if x < 0:
    x = 0
elif x > 1550:
    x = 1550

Thank you works perefectly!!!

Share this post


Link to post
Share on other sites

I would rather avoid such code.

 

I would really use OO programming, programming in a procedural way (like the code mentoined above) really leads to messy code very quickly.

 

global variables are usually a sign of bad coding practice.

Edited by axel1994

Share this post


Link to post
Share on other sites

Of course, but take in consideration that he has stated that he is just a beginner. At this stage it is of critical importance to take the knowledge you have, and convert it into code. He could spend another few months learning the theory behind everything needed to structure a game without ever accomplishing something - or he can learn something, do something practical with it and improve on it when he learns something new.

 

I could have given an example with a player class.. but it still would be "messy" code. I'll have to add something like an:

- Object Manager ... nope still bad coding practice, resources are not loaded properly

- Add an resource manager, nope still not good enough, physics should be handled by it's own class.

- Add a physics manager. Logic and display should be decoupled, add a graphics class..

 

I could go on and on, there will always be more stuff that can be added to try and eliminate "bad practices". I did not want to write a 10 000 line game engine as an answer to his question so I adjusted my programming to fit his style, no matter at what level he is. I saw that he is not yet comfortable with functions so I excluded the idea of reference passing.

 

This guy is trying his best to create something while he is relatively new. As he does more he will realize that he needs something else, and he will ask more questions and so his programming practices will get better over time :)

Edited by PragmaOnce

Share this post


Link to post
Share on other sites

You are certainly correct.

 

My point should have been:

This example solves the problem, it doesn't make things too complicated.

But you'll have to learn better, more complex patterns later on.

 

I do know some people who just get stuck. They keep doing what they're doing, and don't improve.

 

I'm not saying the op is like this (coming to this forum already proves the contrary).

I'm saying this solution isn't the end-all be all way. (such a solution, as you demonstrated, doesn't exist)

 

Thank you for the clarification, and my apologies for the previous answer :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!