Public Group

# Need help with Python/Pygame Collision

This topic is 2083 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()


• 13
• 18
• 29
• 11
• 27