Skip to content

Commit

Permalink
Confine sheep to the fence
Browse files Browse the repository at this point in the history
  • Loading branch information
ana-borges committed Jan 27, 2024
1 parent 7f497a1 commit 70b1727
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
31 changes: 22 additions & 9 deletions counting_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, "assets")

screen_width = 1000
screen_height = 1000

# Roaming bounds (rb)
rb_topleft = (0.05 * screen_width, 0.05 * screen_height)
rb_topright = (0.95 * screen_width, rb_topleft[1])
rb_botleft = (rb_topleft[0], 0.8 * screen_height)
rb_botright = (rb_topright[0], rb_botleft[1])

class SpriteSheet:
def __init__(self, filename):
Expand All @@ -22,7 +30,7 @@ def image_at(self, rectangle, colorkey=None):
image = pg.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey is -1:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pg.RLEACCEL)
return image
Expand Down Expand Up @@ -74,7 +82,7 @@ class CountingObject(pg.sprite.Sprite):
directionX = 1
directionY = 0

def __init__(self, initialPos=(200, 200), allowedRoamingSpace=200, maxChangeDirectionRandom=250):
def __init__(self, initialPos, maxChangeDirectionRandom=250):
pg.sprite.Sprite.__init__(self) # call Sprite initializer
ramSS = SpriteSheet("assets/ram_walk.png")
ramSprites = ramSS.load_2dstrip((0, 0, 128, 128), 4, colorkey=(0, 0, 0))
Expand All @@ -85,22 +93,27 @@ def __init__(self, initialPos=(200, 200), allowedRoamingSpace=200, maxChangeDire
self.area = screen.get_rect()
self.rect.topleft = initialPos
self.initialPos = initialPos
self.allowedRoamingSpace = allowedRoamingSpace
self.maxChangeDirectionRandom = maxChangeDirectionRandom

def update(self):
self.currentSpriteIndex += 1

if self.rect.x - self.initialPos[0] > self.allowedRoamingSpace:
# Check whether out of bounds
# The coordinates refer to the top left corner, so we also need to consider the rest of the sheep
if self.rect.x + 0.7 * self.rect.width > rb_topright[0]:
# turn left
self.directionX = -1
self.directionY = 0
if self.initialPos[0] - self.rect.x > self.allowedRoamingSpace:
if self.rect.x + 0.2 * self.rect.width < rb_topleft[0]:
# turn right
self.directionX = 1
self.directionY = 0
if self.rect.y - self.initialPos[1] > self.allowedRoamingSpace:
if self.rect.y + 0.7 * self.rect.height > rb_botleft[1]:
# turn up
self.directionX = 0
self.directionY = -1
if self.initialPos[1] - self.rect.y > self.allowedRoamingSpace:
if self.rect.y + 0.2 * self.rect.height < rb_topleft[1]:
# turn down
self.directionX = 0
self.directionY = 1

Expand Down Expand Up @@ -137,8 +150,8 @@ def getDirection(self):
def generateHerd(num):
herd = []
for _ in range(num):
randX = random.randint(200, 600)
randY = random.randint(200, 600)
randX = random.randint(rb_topleft[0], rb_topright[0] - 100)
randY = random.randint(rb_topleft[1], rb_botleft[1] - 100)
herd.append(CountingObject(initialPos=(randX,randY)))
return herd

Expand Down
27 changes: 9 additions & 18 deletions main-window.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,13 @@
if not pg.mixer:
print("Warning, sound disabled")

screen_width = 1000
screen_height = 700

# Roaming bounds (rb)
rb_topleft = (0.05 * screen_width, 0.05 * screen_height)
rb_topright = (0.95 * screen_width, rb_topleft[1])
rb_botleft = (rb_topleft[0], 0.8 * screen_height)
rb_botright = (rb_topright[0], rb_botleft[1])

def main():
"""this function is called when the program starts.
it initializes everything it needs, then runs in
a loop until the function returns."""
# Initialize Everything
pg.init()
screen = pg.display.set_mode((screen_width, screen_height), pg.SCALED)
screen = pg.display.set_mode((co.screen_width, co.screen_height), pg.SCALED)
pg.display.set_caption("Counting Simulator")

# Create The Background
Expand All @@ -35,7 +26,7 @@ def main():
background.fill((102, 204, 10))

# Load level and failure sound
currentLevel: LevelInterface = SheepLevel("How many objects are there?", 0)
currentLevel: LevelInterface = SheepLevel("How many objects are there?", 10)

# Put Text On The Background, Centered
if not pg.font:
Expand Down Expand Up @@ -104,19 +95,19 @@ def main():
screen.blit(background, (0, 0))

# Draw fence
full_fence_height = rb_botleft[1] - rb_topleft[1]
full_fence_height = co.rb_botleft[1] - co.rb_topleft[1]
small_fence_height = vertical_fence.get_height()
number_of_small_vfences = math.ceil(full_fence_height / small_fence_height)
for i in range(number_of_small_vfences):
screen.blit(vertical_fence,(rb_topleft[0], rb_topleft[1] + small_fence_height * i))
screen.blit(vertical_fence,(rb_topright[0], rb_topright[1] + small_fence_height * i))
screen.blit(vertical_fence,(co.rb_topleft[0], co.rb_topleft[1] + small_fence_height * i))
screen.blit(vertical_fence,(co.rb_topright[0], co.rb_topright[1] + small_fence_height * i))

full_fence_width = rb_topright[0] - rb_topleft[0]
full_fence_width = co.rb_topright[0] - co.rb_topleft[0]
small_fence_width = horizontal_fence.get_width() + 0.1 * horizontal_fence.get_width()
number_of_small_hfences = math.ceil(full_fence_width / small_fence_width)
for i in range(number_of_small_hfences):
screen.blit(horizontal_fence,(rb_topleft[0] + small_fence_width * i, rb_topleft[1]))
screen.blit(horizontal_fence,(rb_botleft[0] + small_fence_width * i, rb_botleft[1]))
screen.blit(horizontal_fence,(co.rb_topleft[0] + small_fence_width * i, co.rb_topleft[1]))
screen.blit(horizontal_fence,(co.rb_botleft[0] + small_fence_width * i, co.rb_botleft[1]))

if currentLevel.is_stopped():
text = font.render("Press enter to reset", True, (10, 10, 10))
Expand All @@ -131,7 +122,7 @@ def main():

if not currentLevel.is_stopped():
textinput.update(events)
screen.blit(textinput.surface, (background.get_width() / 2 - 150, rb_botleft[1] + 100), (0,0,300,100))
screen.blit(textinput.surface, (background.get_width() / 2 - 150, co.rb_botleft[1] + 100), (0,0,300,100))

timerColor=(0,0,0)
if timer <= 5:
Expand Down

0 comments on commit 70b1727

Please sign in to comment.