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):
image = pygame.image.load('Icon.png')
super().__init__(image, 600, 700)
class Prey(Animal):#prey class
def __init__(self):
image = pygame.image.load('Prey.png')
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.image = pygame.image.load('carrot.png')
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()
carrot_group.add(Carrot())
Wolf_group= pygame.sprite.Group()
Wolf_group.add(Predator())
Rabbit_group= pygame.sprite.Group()
Rabbit_group.add(Prey())
all_sprites = pygame.sprite.Group()
all_sprites.add(carrot_group)
all_sprites.add(Wolf_group)
all_sprites.add(Rabbit_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
pygame.display.update()#updates screen
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()