Skip to content

Commit

Permalink
added log to file
Browse files Browse the repository at this point in the history
fixed endless sleep bug
  • Loading branch information
ngraham20 committed May 9, 2018
1 parent a5b31fc commit 0909725
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 52 deletions.
80 changes: 41 additions & 39 deletions virtupet/Pudgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pygame
import random
import constants
import logger
from file_handler import JSONHandler
from spritesheet_functions import SpriteSheet
from dna import DNA
Expand Down Expand Up @@ -317,12 +318,12 @@ def import_from_json(self, load):
self.dna = DNA(strand)
self.handler.close()

print("---Importing Pudgi---")
print("UID: " + str(self.uid))
print("Name: " + self.name)
print("Color: " + self.color)
print("Personality: " + self.personality)
print("Parents: " + str(self.parents))
logger.logging.info("---Importing Pudgi---")
logger.logging.info("UID: " + str(self.uid))
logger.logging.info("Name: " + self.name)
logger.logging.info("Color: " + self.color)
logger.logging.info("Personality: " + self.personality)
logger.logging.info("Parents: " + str(self.parents))

def export_to_json(self):
# write information about self to a json file
Expand All @@ -337,14 +338,14 @@ def export_to_json(self):

self.handler.save_as("./data/pudgies/" + self.uid + ".json", self.json_object)

print()
print("---Exporting Pudgi---")
print("UID: " + str(self.uid))
print("Name: " + self.name)
print("Color: " + self.color)
print("Happiness: " + str(self.happiness))
print("Personality: " + self.personality)
print("Parents: " + str(self.parents))
logger.logging.info("")
logger.logging.info("---Exporting Pudgi---")
logger.logging.info("UID: " + str(self.uid))
logger.logging.info("Name: " + self.name)
logger.logging.info("Color: " + self.color)
logger.logging.info("Happiness: " + str(self.happiness))
logger.logging.info("Personality: " + self.personality)
logger.logging.info("Parents: " + str(self.parents))
return

@staticmethod
Expand Down Expand Up @@ -395,16 +396,17 @@ def make_decision(self):
chance_waking = -1
chance_sleeping = -1
# check vitality for sleepiness (using fuzzy logic)
if vitality <= 2:
low = 2.5
high = 9
if vitality <= low:
chance_waking = 0
chance_sleeping = 1
elif 2 < vitality < 9:
if self.sleeping:
chance_waking = ((vitality-2)/7)
chance_sleeping = ((9 - vitality) / 7)
else:
chance_waking = 1
chance_sleeping = 0
elif low < vitality < high:
chance_waking = ((vitality-low)/high-low)
chance_sleeping = ((high - vitality) / high-low)
else:
chance_waking = 1
chance_sleeping = 0

if self.sleeping:
if random.random() <= chance_waking:
Expand Down Expand Up @@ -498,27 +500,27 @@ def make_decision(self):

self.known_decisions[choice_index]["count"] += 1

print()
print("---------------------------------------------")
print("Pudgi: " + self.name)
print("Choice: " + choice["name"])
print("Happiness increased by: " + str(happiness_optimized))
print("Times chosen: " + str(self.known_decisions[choice_index]["count"]))
print("---------------------------------------------")
print(str(self.name) + "'s Happiness: " + str(self.happiness))
print(str(self.name) + "'s Vitality: " + str(self.vitality))
logger.logging.info("")
logger.logging.info("---------------------------------------------")
logger.logging.info("Pudgi: " + self.name)
logger.logging.info("Choice: " + choice["name"])
logger.logging.info("Happiness increased by: " + str(happiness_optimized))
logger.logging.info("Times chosen: " + str(self.known_decisions[choice_index]["count"]))
logger.logging.info("---------------------------------------------")
logger.logging.info(str(self.name) + "'s Happiness: " + str(self.happiness))
logger.logging.info(str(self.name) + "'s Vitality: " + str(self.vitality))

else: # the pudgi is sleeping
self.vitality += 0.8

print()
print("---------------------------------------------")
print("Pudgi: " + self.name)
print("Choice: Sleep")
print("Vitality increased by: 0.8")
print("---------------------------------------------")
print(str(self.name) + "'s Happiness: " + str(self.happiness))
print(str(self.name) + "'s Vitality: " + str(self.vitality))
logger.logging.info("")
logger.logging.info("---------------------------------------------")
logger.logging.info("Pudgi: " + self.name)
logger.logging.info("Choice: Sleep")
logger.logging.info("Vitality increased by: 0.8")
logger.logging.info("---------------------------------------------")
logger.logging.info(str(self.name) + "'s Happiness: " + str(self.happiness))
logger.logging.info(str(self.name) + "'s Vitality: " + str(self.vitality))

def movement(self, direction):
if direction == "L" and self.rect.x > 0:
Expand Down
Empty file added virtupet/logger
Empty file.
17 changes: 17 additions & 0 deletions virtupet/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import logging

# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
format='%(message)s',
filename='./logs/output.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)

27 changes: 14 additions & 13 deletions virtupet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import pygame
import constants
import environments
import logger
from clock import Clock
from pudgi import Pudgi
import random


def main():

print("-Initializing Simulation-")
logger.logging.info("-Initializing Simulation-")

death_count = 0

Expand Down Expand Up @@ -90,10 +91,10 @@ def main():
pudgi = Pudgi(parents)
active_sprite_list.add(pudgi)
active_agent_list.append(pudgi)
print()
print("<<<---It's the miracle of life!--->>>")
print(pudgi.name + " was just born into the world!")
print()
logger.logging.info("")
logger.logging.info("<<<---It's the miracle of life!--->>>")
logger.logging.info(pudgi.name + " was just born into the world!")
logger.logging.info("")
pudgi.export_to_json()
pudgi.rect.y = constants.SCREEN_HEIGHT - 140
pudgi.rect.x = random.randint(100, 800)
Expand All @@ -114,11 +115,11 @@ def main():
active_sprite_list.remove(pudgi)
death_count += 1

print()
print("<<<---" + pudgi.name + " died of old age--->>>")
print("Death Count: " + str(death_count))
logger.logging.info("")
logger.logging.info("<<<---" + pudgi.name + " died of old age--->>>")
logger.logging.info("Death Count: " + str(death_count))

# print(colored(pudgi.name + " died of old age", "red"))
# logger.logging.info(colored(pudgi.name + " died of old age", "red"))
if pudgi.sleeping:
movement[pudgi.name]["direction"] = "S"
pudgi.direction = "S"
Expand Down Expand Up @@ -162,11 +163,11 @@ def main():
active_sprite_list.remove(pudgi)
death_count += 1

print()
print("<<<---" + pudgi.name + " died in childbirth--->>>")
print("Death Count: " + str(death_count))
logger.logging.info("")
logger.logging.info("<<<---" + pudgi.name + " died in childbirth--->>>")
logger.logging.info("Death Count: " + str(death_count))

# print(colored(pudgi.name + " died in childbirth", "red"))
# logger.logging.info(colored(pudgi.name + " died in childbirth", "red"))
if int(time_clock.get_minutes()) % 15 == 0:
pudgi.make_decision()

Expand Down

0 comments on commit 0909725

Please sign in to comment.