# How can I create a boundary so the sprites don't go off screen?

I'm attempting to create a boundary around the window to stop the sprites of my game from escaping but it doesn't seem to be work, when they hit the area where its supposed to stop them it instead does nothing and sends it into 0,0 not allowing it too move.

    def leavingWindow(Xpos, Ypos):#boundry, keeps sprites in the window
if Xpos<=0:
return True
elif Xpos>=950:
return True
if Ypos<=0:
return True
elif Ypos>=750:
return True

def Boundry(Xpos, Ypos):
if Xpos<=0:
Xmovement=0
elif Xpos>=950:
Xmovement=950
else:
Xmovement=+0

if Ypos<=0:
ymovement=0
elif Ypos>=750:
ymovement=60
else:
ymovement=+0

return Xmovement, ymovement

if len(preys) > 0:
for wolf in Wolf_group:
movementx, movementy = Track(wolf.x, preys[0].x, wolf.y, preys[0].y)#how the predators move
Leaving=leavingWindow(wolf.x,wolf.y)
if Leaving == True:
movementx,movementy=Boundry(wolf.x,wolf.y)
wolf.move(0,1)#movementx, #movementy)


Ive attempted changing around the code but it keeps doing this.

full code

#simulates interactions of wild life
import math
import random
import pygame,sys
import random
import pdb
from pygame.locals  import *

#classes--------------
class Animal(pygame.sprite.Sprite):#main class
def __init__(self, image, x, y):
super().__init__()
self.x = x
self.y = y
self.image = image
self.rect = self.image.get_rect(center = (x, y))
def move(self, mx, my):
self.x += mx
self.y += my
self.rect = self.image.get_rect(center = (self.x, self.y))#sets the image to the position

def is_collided_with(self, sprite):#is the eat function
return self.rect.colliderect(sprite.rect)

class Predator(Animal):#class for predators
def __init__(self):
super().__init__(image, 600, 700)

class Prey(Animal):#prey class
def __init__(self):
xPos = 300
xPos  = 400
super().__init__(image,xPos ,xPos )
#self.rect = self.image.get_rect(center = (self.x, self.y))
def move(self, mx, my):#item that moves the sprite
self.x += mx
self.y += my
self.rect = self.image.get_rect(center = (self.x, self.y))

class Carrot(pygame.sprite.Sprite):#food for prey class
def __init__(self):
super().__init__()
self.x = 100
self.y = 700
self.rect = self.image.get_rect(center = (self.x, self.y))

# Functions-----------
def Track(AgressorX,DefenderX,AgressorY,DefenderY):#finds comapres X and Y, tells sprite how to move
if AgressorX > DefenderX:
XMovement=-1
elif AgressorX< DefenderX:
XMovement=1
else:
XMovement=0

if AgressorY > DefenderY:
YMovement=-1
elif AgressorY < DefenderY:
YMovement=1
else:
YMovement=0
return XMovement, YMovement

def leavingWindow(Xpos, Ypos):#boundry, keeps sprites in the window
if Xpos<=0:
return True
elif Xpos>=950:
return True
if Ypos<=0:
return True
elif Ypos>=750:
return True

def Boundry(Xpos, Ypos):
if Xpos<=0:
Xmovement=0
elif Xpos>=950:
Xmovement=950
else:
Xmovement=+0

if Ypos<=0:
ymovement=0
elif Ypos>=750:
ymovement=60
else:
ymovement=+0

return Xmovement, ymovement

pygame.init()#setsup pygame
screen = pygame.display.set_mode((1000,800))#setsup screen
clock = pygame.time.Clock()#tells program how fast to update

timmer = 1#how long program has gone on for
#setsup the classes
predator = Predator()
predator2=Predator()
prey = Prey()
carrot = Carrot()

#setsup the classes as sprites
carrot_group = pygame.sprite.Group()

Wolf_group= pygame.sprite.Group()

Rabbit_group= pygame.sprite.Group()

all_sprites = pygame.sprite.Group()

#start of program
running=True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type==pygame.QUIT:
running=False

carrots = carrot_group.sprites()
preys = Rabbit_group.sprites()
predators = Wolf_group.sprites()

if len(preys) > 0:
for wolf in Wolf_group:
movementx, movementy = Track(wolf.x, preys[0].x, wolf.y, preys[0].y)#how the predators move
wolf.move(0,1)#movementx, #movementy)

Leaving=leavingWindow(wolf.x,wolf.y)#keeps sprites in window
if Leaving == True:
movementx,movementy=Boundry(wolf.x,wolf.y)
wolf.x=movementx
wolf.y=movementy

if len(carrots) > 0:
for rabbit in Rabbit_group:
movementx, movementy = Track(rabbit.x, carrots[0].x, rabbit.y, carrots[0].y)#how the prey move
rabbit.move(movementx, movementy)

screen.fill((0,128,0))#background
all_sprites.draw(screen)#makes the sprites
timmer=timmer+1

for carrot in carrots:#removes the carrots if they contact a rabbit
for rabbit in Rabbit_group:
if rabbit.is_collided_with(carrot):
print('works')
carrot.kill()

for wolf in Wolf_group:
for rabbit in Rabbit_group:
if rabbit.is_collided_with(wolf):
print("works 2")
rabbit.kill()

pygame.quit()
exit()


The issue lies in the following function:

def Boundry(Xpos, Ypos):
if Xpos<=0:
Xmovement=0
elif Xpos>=950:
Xmovement=950
else:
Xmovement=+0

if Ypos<=0:
ymovement=0
elif Ypos>=750:
ymovement=60
else:
ymovement=+0

return Xmovement, ymovement


In no case you are keeping the values you've passed as parameters, you only overwrite them.

def Boundry(Xpos, Ypos):
Xmovement = Xpos
ymovement = Ypos

if Xpos<=0:
Xmovement=0
elif Xpos>=950:
Xmovement=950

if Ypos<=0:
ymovement=0
elif Ypos>=750:
ymovement=60

return Xmovement, ymovement


Or, more concisely, as suggested via the existing answer:

def Boundry(Xpos, Ypos):
Xmovement = min(max(0, Xpos), 950)
ymovement = min(max(0, Ypos), 750)

return Xmovement, ymovement


That being said, your code contains a lot of inconsistencies and magic numbers which could bite you down the road.

I also suggest you use tools such as pylint and black to help improve the readability, the consistency, and the robustness of your code.

You'd better try the min-max method to set the boundaries for you object movement, something like:

self.x = min(max(0, self.x), screen_width)
self.y = min(max(0, self.y), screen_height)


I hope you can set it to your code.

• just making sure this is meant to go in the animal class area – Flat_Pancake Dec 9 '20 at 11:48