Skip to content

Commit

Permalink
Cleaned up footprints tracking.
Browse files Browse the repository at this point in the history
  • Loading branch information
TypeDefinition committed Jan 16, 2022
1 parent b0154f1 commit 1551376
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 69 deletions.
21 changes: 12 additions & 9 deletions src/footprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class Footprints(ObjectBase):
NUM_FRAMES = 4
ANIM_DELAY = 0.5
STAY_TIME = 1
STAY_TIME = 10

FOOTPRINTS_LEFT = "assets/animations/footprints_Left.gif"
FOOTPRINTS_RIGHT = "assets/animations/footprints_right.gif"
Expand All @@ -14,19 +14,22 @@ def __init__(self, pos, move_left = True):
file_path = Footprints.FOOTPRINTS_LEFT if move_left else Footprints.FOOTPRINTS_RIGHT
super(Footprints, self).__init__(file_path, Footprints.NUM_FRAMES, Footprints.ANIM_DELAY, False)
self.active = True
self.stayTime = 0
self.window.geometry('+{x}+{y}'.format(x=str(round(pos.x)), y=str((round(pos.y)))))
self.stay_time = 0
self.window.geometry('+%d+%d' % (round(pos.x), round(pos.y)))

def update(self):
if time.time() < self.stayTime + Footprints.STAY_TIME:
if time.time() < self.stay_time + Footprints.STAY_TIME:
return

self.anim.update()
if self.anim.frame_index == 1:
self.stayTime = time.time()
self.stay_time = time.time()

if not self.anim.finished():
self.label.configure(image=self.anim.get_frame())
if self.anim.finished():
self.window.destroy()
self.active = False
else:
self.window.destroy
self.active = False
self.label.configure(image=self.anim.get_frame())

def is_active(self):
return self.active
28 changes: 14 additions & 14 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ def quit_combination():
if quit_combination():
break

if keyboard.is_pressed("1"):
pet.change_state(PetState.IDLE)
elif keyboard.is_pressed("2"):
pet.change_state(PetState.STROLL)
elif keyboard.is_pressed("3"):
pet.change_state(PetState.CATCH_MOUSE)
elif keyboard.is_pressed("4"):
pet.change_state(PetState.OPEN_WINDOW)
elif keyboard.is_pressed("5"):
pet.change_state(PetState.MOVE_WINDOW)
elif keyboard.is_pressed("6"):
pet.change_state(PetState.HEADPAT)
elif keyboard.is_pressed("7"):
pet.change_state(PetState.SCREAM)
# if keyboard.is_pressed("1"):
# pet.change_state(PetState.IDLE)
# elif keyboard.is_pressed("2"):
# pet.change_state(PetState.STROLL)
# elif keyboard.is_pressed("3"):
# pet.change_state(PetState.CATCH_MOUSE)
# elif keyboard.is_pressed("4"):
# pet.change_state(PetState.OPEN_WINDOW)
# elif keyboard.is_pressed("5"):
# pet.change_state(PetState.MOVE_WINDOW)
# elif keyboard.is_pressed("6"):
# pet.change_state(PetState.HEADPAT)
# elif keyboard.is_pressed("7"):
# pet.change_state(PetState.SCREAM)
86 changes: 40 additions & 46 deletions src/pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class PetAnimState(enum.IntEnum):
ATTACK_RIGHT = 5

class Pet():
FOOT_PRINT_SPAWN = 0.4
FOOT_PRINT_SPAWN_INTERVAL = 0.4
FOOT_X_LEFT_OFFSET = 64
FOOT_X_RIGHT_OFFSET = -5
FOOT_Y_OFFSET = 80

TRACK_FOOTPRINT_MAX_TIME = 8
TRACK_FOOTPRINT_MIN_TIME = 5
FOOTPRINT_CHANCE = 3 #30%
TRACK_FOOTPRINT_MAX_DURATION = 8
TRACK_FOOTPRINT_MIN_DURATION = 5
FOOTPRINT_CHANCE = 40 # 40%

def __init__(self):
# Create a window
Expand All @@ -54,12 +54,11 @@ def __init__(self):
# X & Y Coordinates of our window.
self.pos = Vector2(500, 0)

#for tracking footprints
self.footPrintsStorer = []
self.footPrintTime = time.time()
self.trackFootPrint = False
# For tracking footprints.
self.active_footprints = []
self.track_foot_print = False

#set default state
# Set default state.
self.next_state = PetState.IDLE
self.curr_state = PetState.DEFAULT
self.prev_state = PetState.DEFAULT
Expand All @@ -80,13 +79,13 @@ def set_anim_state(self, anim_state):
self.anims[self.anim_state].reset() # Reset previous animation.
self.anim_state = anim_state # Switch to new animation.

#for footprints spawning
# Spawn footprints.
if self.anim_state == PetAnimState.WALK_LEFT or self.anim_state == PetAnimState.WALK_RIGHT:
if random.randrange(0, 10) < Pet.FOOTPRINT_CHANCE and not self.trackFootPrint:
self.trackFootPrint = True
if random.randrange(0, 100) < Pet.FOOTPRINT_CHANCE and not self.track_foot_print:
self.track_foot_print = True
self.init_track_footprints()
else:
self.trackFootPrint = False
self.track_foot_print = False

def get_anim_state(self):
return self.anim_state
Expand All @@ -109,15 +108,15 @@ def get_position(self):
return self.pos

def update(self, delta_time):
# Update animation.
self.anims[self.anim_state].update()
self.label.configure(image=self.anims[self.anim_state].get_frame()) # Update animation frame.

# Update Window
self.window.geometry('+{x}+{y}'.format(x=str(round(self.pos.x)), y=str(round(self.pos.y))))
#self.window.lift()
# Update window.
self.window.geometry('+%d+%d' % (round(self.pos.x), round(self.pos.y)))
self.window.update()

# Update Previous
# Update state.
self.prev_state = self.curr_state
self.curr_state = self.next_state

Expand All @@ -143,41 +142,36 @@ def lift_window(self):
self.window.lift()

def init_track_footprints(self):
self.footprintStartTime = time.time()
self.footprintTime = random.randrange(Pet.TRACK_FOOTPRINT_MIN_TIME, Pet.TRACK_FOOTPRINT_MAX_TIME)
self.footprint_end_time = time.time() + random.randrange(Pet.TRACK_FOOTPRINT_MIN_DURATION, Pet.TRACK_FOOTPRINT_MAX_DURATION)
self.footprint_prev_spawn_time = 0

def track_footprints(self):
deleteQueue = []

#update and check those who are inactive
for footprint in self.footPrintsStorer:
if not footprint.active:
deleteQueue.append(footprint)
continue

footprint.update()

#remove inactive
for footprint in deleteQueue:
self.footPrintsStorer.remove(footprint)
# Update and check those who are inactive.
inactive_footprints = []
for footprint in self.active_footprints:
if footprint.is_active():
footprint.update()
else:
inactive_footprints.append(footprint)

# Remove inactive footprints.
for footprint in inactive_footprints:
self.active_footprints.remove(footprint)
del footprint

deleteQueue.clear()
inactive_footprints.clear()

if not self.trackFootPrint:
if not self.track_foot_print:
return

#intervals between footprints
if time.time() < self.footPrintTime + Pet.FOOT_PRINT_SPAWN:
# Intervals between footprints.
time_now = time.time()
if time_now < self.footprint_prev_spawn_time + Pet.FOOT_PRINT_SPAWN_INTERVAL:
return
self.footprint_prev_spawn_time = time_now

self.footPrintTime = time.time()

walkingLeft = self.anim_state == PetAnimState.WALK_LEFT
footPrintOffset = Vector2(Pet.FOOT_X_LEFT_OFFSET, Pet.FOOT_Y_OFFSET) if self.anim_state == PetAnimState.WALK_LEFT else Vector2(Pet.FOOT_X_RIGHT_OFFSET, Pet.FOOT_Y_OFFSET)

self.footPrintsStorer.append(Footprints(self.pos.__add__(footPrintOffset), walkingLeft))
foot_print_offset = Vector2(Pet.FOOT_X_LEFT_OFFSET, Pet.FOOT_Y_OFFSET) if self.anim_state == PetAnimState.WALK_LEFT else Vector2(Pet.FOOT_X_RIGHT_OFFSET, Pet.FOOT_Y_OFFSET)
self.active_footprints.append(Footprints(self.pos + foot_print_offset, self.anim_state == PetAnimState.WALK_LEFT))

#how long the foot prints would keep spawning for
if time.time() > self.footprintStartTime + self.footprintTime:
self.trackFootPrint = False
# How long the foot prints would keep spawning for.
if time_now > self.footprint_end_time:
self.track_foot_print = False

0 comments on commit 1551376

Please sign in to comment.