# Need help with Python/Pygame Collision

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

## Recommended Posts

Hello, I just need some easy (if possible) code that prevents the player from going off screen, sorry if this is in the wrong place or is a stupid question thanks.

##### Share on other sites

Hello, I just need some easy (if possible) code that prevents the player from going off screen, sorry if this is in the wrong place or is a stupid question thanks.

Hi.

Usually a collision test looks something like this: (not real code)

if yPosition > 460:
yPosition = 460


If your window is 640 x 480 pixels and you have a platform at the bottom of your window that is 20 pixels by 20 pixels (copied across the bottom of your screen), then you will want to check if the falling object's Y-position has reached 460 pixels. And if it has reached 460 pixels, then you set it's position to 460 pixels.

The x coordinates are read from left to right. The Y coordinates are read from top to bottom (not like in math class). So counting from the top to the bottom, the y position of the top of the platform will be at 460 pixels (480-20 because the platform is 20 pixels tall). You can also use an if statement to print something once the falling object has reached 460 pixels to check if it works.

I hope this photo isn't too big.

One more note. The falling object has a size also. If your falling object is 10 x 10 pixels, then from the top of your object to the middle is 5 pixels. I think pygame uses the center of your object graphic as the collision point, so you will want to offset that 460 by 5 pixels (if your graphic is  10 x 10).

Edited by Tutorial Doctor

##### Share on other sites
def is_within_borders():
# Assume we have 500, 500 window with 50, 50 player size.
if player.x < 0:
player.x = 0
elif player.x > 450:
player.x = 450
return True


##### Share on other sites

Thanks for the answers, but I am still confused. When I type in the code it doesn't work.

##### Share on other sites

Alright sorry, I am very new to this. Also sorry if code isn't pretty or anything I am new.

pygame.init()

screen=pygame.display.set_mode((1600,1130),0,32)

#Variables for shapes
color=(128,64,0)
pos1=(0,0)
pos2=(0,1130)
pos3=(1600,0)
pos4=(1600,1130)
#Variables for shapes

x,y=0,0
movex, movey=0,0

#Attempting collision
def is_within_borders():
if yPosition > 50:
yPosition = 50
elif xPosition > 1550:
xPostion = 1550
return True
#Attempting collision

#Controls
while True:

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_LEFT:
movex=-4
elif event.key==K_RIGHT:
movex=+4
elif event.key==K_UP:
movey=-4
elif event.key==K_DOWN:
movey=+4
if event.type==KEYUP:
if event.key==K_LEFT:
movex=0
elif event.key==K_RIGHT:
movex=0
elif event.key==K_UP:
movey=0
elif event.key==K_DOWN:
movey=0

#Controls

screen.lock()
line1=pygame.draw.line(screen, color, pos1, pos2, 50)
line2=pygame.draw.line(screen, color, pos3, pos4, 50)
line3=pygame.draw.line(screen, color, pos1, pos3, 50)
line4=pygame.draw.line(screen, color, pos2, pos4, 50)
screen.unlock()

x+=movex
y+=movey

screen.blit(mouse_c,(x,y))

pygame.display.update()

##### Share on other sites

Okay I have never programmed in Python/PyGame before so I might be completely wrong... but

if the variables x, y define the position of your object :

Then this

#Attempting collision
def is_within_borders():
if yPosition > 50:
yPosition = 50
elif xPosition > 1550:
xPostion = 1550
return True

Should be:

#Attempting collision

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

Also. You want to check this every step/tick of the game. So it should be checked in your main loop. I have no idea how pygame works as I said before.. so it might have already been in there  So check for the object being out of bounds just after you have moved your object.

EDIT: Thanks CoreLactose, I didn't even bother to check the parameters :/

Edited by PragmaOnce

##### Share on other sites

PragmaOnce's suggestion still looks wrong to me.

Assuming this is  the correct syntax, with a character sprite of 50x50...

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


Something like that?

That should enforce both vertical and horizontal, in both ends.

Also, it needs to be called in your main loop, as PragmaOnce said.

EDIT: It should be called after you've updated the positions.

Edited by CoreLactose

##### Share on other sites

PragmaOnce's suggestion still looks wrong to me.

Assuming this is  the correct syntax, with a character sprite of 50x50...

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


Something like that?

That should enforce both vertical and horizontal, in both ends.

Also, it needs to be called in your main loop, as PragmaOnce said.

EDIT: It should be called after you've updated the positions.

Thanks, but it still won't work:

pygame.init()

screen=pygame.display.set_mode((1600,1130),0,32)

#Variables for shapes
color=(128,64,0)
pos1=(0,0)
pos2=(0,1130)
pos3=(1600,0)
pos4=(1600,1130)
#Variables for shapes

x,y=0,0
movex, movey=0,0

#Controls
while True:

for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_LEFT:
movex=-4
elif event.key==K_RIGHT:
movex=+4
elif event.key==K_UP:
movey=-4
elif event.key==K_DOWN:
movey=+4
if event.type==KEYUP:
if event.key==K_LEFT:
movex=0
elif event.key==K_RIGHT:
movex=0
elif event.key==K_UP:
movey=0
elif event.key==K_DOWN:
movey=0
#Controls
#Collsion

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
#Collision

screen.lock()
line1=pygame.draw.line(screen, color, pos1, pos2, 50)
line2=pygame.draw.line(screen, color, pos3, pos4, 50)
line3=pygame.draw.line(screen, color, pos1, pos3, 50)
line4=pygame.draw.line(screen, color, pos2, pos4, 50)
screen.unlock()

x+=movex
y+=movey

screen.blit(mouse_c,(x,y))

pygame.display.update()

##### Share on other sites

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()


##### Share on other sites

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

Edited by PragmaOnce

##### 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 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 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 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 :)