Skip to content

Commit

Permalink
Merge pull request #33 from AnneMiriam/debugEncounter
Browse files Browse the repository at this point in the history
lookup and delete enemies
  • Loading branch information
AnneMiriam authored Jan 5, 2024
2 parents 0615f04 + e9ae76f commit 10c83d2
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Take a look at the directory structure:
└── lib
├── models
│ ├── __init__.py
│ ├── Enemy.py
│ ├── enemy.py
│ ├── game.py
│ ├── items.py
│ ├── NonPlayChar.py
Expand Down
43 changes: 33 additions & 10 deletions lib/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
from helpers import display_intro, exit_program, output_slower
from models.game import Game
from models.player import Player
from models.Enemy import Enemy
from models.enemy import Enemy



def main():
while True:
Expand All @@ -26,18 +28,11 @@ def main():
# print("Enter your choice >>")
# print("-" * 50)
elif choice == "2":
# create an enemy in the Enemy class
game = Game(None, None)
game.create_enemy()
new_menu()
choice = input("> ")
if choice == "0":
exit_program()
elif choice == "1":
print("-" * 50)
# Start new game session. Initialize game with player and pass onto Game.
game = Game(None, None)
game.create_player()
game.start_game()

break
else:
print("Invalid choice")
Expand All @@ -54,6 +49,34 @@ def new_menu():
print("What do you want to do?")
print("0. Exit the program")
print("1. Take a breath - remember who you are")
print("2. Look up created enemies")
print("3. Delete created enemies")
choice = input("> ")
if choice == "0":
exit_program()
elif choice == "1":
print("-" * 50)
# Start new game session. Initialize game with player and pass onto Game.
game = Game(None, None)
game.create_player()
game.start_game()
elif choice == "2":
# Get all the enemy's names
enemies = Enemy.get_all()
for enemy in enemies:
print(enemy.name)
new_menu()
elif choice == "3":
# get the enemies by name and delete them
value = input("What enemy do you seek? >>")
nemesis = Enemy.find_by_name(value)
if nemesis:
nemesis.delete()
print()
print("You have eliminated an enemy!")
else:
print("This enemy does not exist! Look for another or play the game.")
new_menu()


if __name__ == "__main__":
Expand Down
3 changes: 2 additions & 1 deletion lib/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from models.__init__ import CONN, CURSOR
import ipdb

from models.enemy import Enemy
from models.player import Player

ipdb.set_trace()
2 changes: 1 addition & 1 deletion lib/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# lib/helpers.py
import time
from models.Enemy import Enemy
from models.enemy import Enemy
from models.NonPlayChar import NonPlayableCharacter
from models.player import Player
from models.rooms import MapRoom
Expand Down
14 changes: 11 additions & 3 deletions lib/models/Enemy.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from models.__init__ import CURSOR, CONN

from models.__init__ import CURSOR, CONN
import random


class Enemy:
ALL = {}

def __init__(self, name=None, id=None):
def __init__(self, name, id=None):
self.id = id
self.name = name
self.hp = 5
Expand Down Expand Up @@ -110,7 +110,9 @@ def instance_from_db(cls, row):
enemy.damage = row[3]

else:
enemy = cls(row[1], row[2], row[3])
enemy = cls(row[1])
enemy.hp = row[2]
enemy.damage = row[3]
enemy.id = row[0]
cls.ALL[enemy.id] = enemy
return enemy
Expand All @@ -126,6 +128,12 @@ def find_by_id(cls, id):
sql = """SELECT * FROM enemies WHERE id = ?"""
row = CURSOR.execute(sql, (id,)).fetchone()
return cls.instance_from_db(row) if row else None

@classmethod
def find_by_name(cls, name):
sql = """SELECT * FROM enemies WHERE name is ?"""
row = CURSOR.execute(sql, (name,)).fetchone()
return cls.instance_from_db(row) if row else None


class GrimReaper(Enemy):
Expand Down
140 changes: 68 additions & 72 deletions lib/models/game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import random
from models.items import *
from models.Enemy import Enemy
from models.enemy import Enemy
from data.default_enemies import default_enemies
from models.items import *
from models.NonPlayChar import *
Expand Down Expand Up @@ -76,7 +76,7 @@ def transaction(self, seller, consumer, item):
else:
consumer.hp += item.healing_value
print("Trade sealed in ethereal terms.")
print(f"Ypur updated HP: {consumer.hp} (+{item.healing_value})")
print(f"Your updated HP: {consumer.hp} (+{item.healing_value})")

def create_player(self):
while True:
Expand Down Expand Up @@ -111,22 +111,73 @@ def create_enemy(self):
self.enemy.create_table()
self.enemy.save()

# def random_encounter(self):
# enemy_data = random.choice(default_enemies)
# # Create an Enemy instance with random attributes
# if "hp" in enemy_data and "damage" in enemy_data and "name" in enemy_data:
# random_enemy = Enemy(
# hp=enemy_data["hp"],
# damage=enemy_data["damage"],
# name=enemy_data["name"],
# )
# # Save the enemy to the database
# random_enemy.create_table()
# random_enemy.save()
# # Set the current_enemy to the randomly encountered enemy
# self.current_enemy = random_enemy
# else:
# print("Invalid enemy data. Missing required attributes.")

# Battle Code
def random_encounter(self):
enemy_data = random.choice(default_enemies)
# Create an Enemy instance with random attributes
if "hp" in enemy_data and "damage" in enemy_data and "name" in enemy_data:
random_enemy = Enemy(
hp=enemy_data["hp"],
damage=enemy_data["damage"],
name=enemy_data["name"],
)
# Save the enemy to the database
random_enemy.create_table()
random_enemy.save()
# Set the current_enemy to the randomly encountered enemy
self.current_enemy = random_enemy
else:
print("Invalid enemy data. Missing required attributes.")
enemy_types = [
GrimReaper, BlackCat, Poltergeist, BlackWidow,
Enemy.find_by_id(-1)]
random_enemy_type = random.choice(enemy_types)
# random_enemy_type = EnemyAndFriends(enemy_types)
random_enemy = random_enemy_type()
self.current_enemy = random_enemy

def battle(self):
if not self.player or not self.current_enemy:
print("Must be alive to battle")
return
# print(f"A {self.current_enemy} appeared!")
while self.player.hp > 0 and self.current_enemy.hp > 0:
print(f"{self.player.name}'s HP: {self.player.hp}")
print(f"{self.current_enemy.name}' HP: {self.current_enemy.hp}")

player_choice = input("(a)ttack or (r)un >> ")
if player_choice == "a":
self.attack(self.player, self.current_enemy)
if player_choice == "r":
print()
output_slow(
"You flee back to the attic room. It may not be home, but it's the only room you've been safe in so far."
)
Game.return_attic_room(self)
else:
print("Invalid choice. You must battle or flee...")

if self.current_enemy.hp <= 0:
print(f"{self.current_enemy.dead_text} \nYou defeated the {self.current_enemy.name}!")
break

self.attack(self.current_enemy, self.player)
if self.player.hp <= 0:
print()
output_slow("You have become another soul within the tower")
output_slower("GAME OVER")
exit()

# attack code
def attack(self, attacker, target):
attacker_damage = attacker.damage
print(f"{attacker.name} attacks {target.name} for {attacker_damage} damage!")
target.hp -= attacker_damage




# START GAME and room navigation
def start_game(self):
Expand Down Expand Up @@ -507,58 +558,3 @@ def return_entryway(self):
# # Set the current_enemy to the randomly encountered enemy
# self.current_enemy = random_enemy

# Battle Code
def random_encounter(self):
enemy_types = [
GrimReaper,
BlackCat,
Poltergeist,
BlackWidow,
Enemy.find_by_id(-1),
]
random_enemy_type = random.choice(enemy_types)
# random_enemy_type = EnemyAndFriends(enemy_types)

random_enemy = random_enemy_type()

self.current_enemy = random_enemy

def battle(self):
if not self.player or not self.current_enemy:
print("Must be alive to battle")
return
# print(f"A {self.current_enemy} appeared!")
while self.player.hp > 0 and self.current_enemy.hp > 0:
print(f"{self.player.name}'s HP: {self.player.hp}")
print(f"{self.current_enemy.name}' HP: {self.current_enemy.hp}")

player_choice = input("(a)ttack or (r)un >> ")
if player_choice == "a":
self.attack(self.player, self.current_enemy)
if player_choice == "r":
print()
output_slow(
"You flee back to the attic room. It may not be home, but it's the only room you've been safe in so far."
)
Game.return_attic_room(self)
else:
print("Invalid choice. You must battle or flee...")

if self.current_enemy.hp <= 0:
print(
f"{self.current_enemy.dead_text} \nYou defeated the {self.current_enemy.name}!"
)
break

self.attack(self.current_enemy, self.player)
if self.player.hp <= 0:
print()
output_slow("You have become another soul within the tower")
output_slower("GAME OVER")
exit()

# attack code
def attack(self, attacker, target):
attacker_damage = attacker.damage
print(f"{attacker.name} attacks {target.name} for {attacker_damage} damage!")
target.hp -= attacker_damage
1 change: 0 additions & 1 deletion lib/models/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from models.items import *
from models.__init__ import CURSOR, CONN


class Player:
ALL = {}

Expand Down
2 changes: 1 addition & 1 deletion lib/models/rooms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import random
from models.Enemy import Enemy, BlackCat, BlackWidow, Poltergeist, GrimReaper
from models.enemy import Enemy, BlackCat, BlackWidow, Poltergeist, GrimReaper

# from data.default_enemies import default_enemies
from models.NonPlayChar import Casper
Expand Down

0 comments on commit 10c83d2

Please sign in to comment.