I'm trying to make a text-based RPG kinda game and whenever I try and print the damage done or hp or other things, it sometimes appears blank ( just a bigger space in between the strings) I'm using replit to run and edit my code. In addidtion to this (and this isnt that big of an issue) Whenever its printing out the players HP then it comes to something like 9.7999999999999, why is this?
LOGIC AND CALCULATION CODE BELOW
#Imports
from player import Player
from items import Item
from spells import Spells
from monsters import Monster
from gameplay import Fight
#Creating spells
# Type, Name, Damage, Cost
#Below are buff spells
buff_A = Spells("buff", "Buff", 1.1, 5)
buff_B = Spells("buff", "Stronger Buff", 1.3, 15)
buff_C = Spells("buff", "Strongest Buff", 1.5, 50)
#Below Are heal spells
heal_A = Spells("heal", "Heal", 5, 10)
heal_B = Spells("heal", "Stronger Heal", 20, 30)
heal_C = Spells("heal", "Strongest Heal", 50, 100)
#Creating Items
#Type, Name, Description, Amount
potion = Item("potion", "Health Potion", "Heals you for 10 HP", 10)
elixir = Item("elixir", "Mana Potion", "Regains 15 MP", 15)
#Assiging Variables For Player
player_spells = [buff_A, heal_A]
player_items = [potion, elixir]
#Creating player
#NAME, HP, MP, ATK, DEF, SPELLS, ITEMS
player = Player("Nathan", 10, 20, 5, 5, player_spells, player_items)
#Creating monster
cricket = Monster("Cricket", 3, 3, 7)
enemies = [cricket]
#Fight Setup
fighting = True
#gameplay
fight1 = Fight(player, cricket)
running = True
def player_hurt(enemy):
edamage = enemy.damage_dealt()
player.take_dmg(edamage)
print("You have taken " + str(edamage) + " damage")
print(str(player.hp) + " / " + str(player.maxhp))
#action = input("Please select an action, Attack, Magic, Items ")
while running:
print("1 for attack etc.")
choice = input(player.actions)
list_chooser = int(choice) - 1
#ATTACK CHOOSE
if list_chooser == 0:
dmg = player.generate_dmg()
enemies[0].damage_taken(dmg)
print("You did " + str(dmg) + " damage")
player_hurt(enemies[0])
if cricket.hp == 0:
print("You have killed the cricket")
running = False
#Add Exp element here later
#MAGIC CHOOSE
if list_chooser == 1:
print("Choose 1 for first spell, etc. ")
magic_choice = input()
magic_choice_real = int(magic_choice) - 1
if magic_choice_real == 0:
print("idek how to code this")
if magic_choice_real == 1:
if player.mp < heal_A.cost:
print("Insufficient mana")
if player.mp >= heal_A.cost:
player.heal(heal_A.dmg)
print("You healed for " + str(heal_A.dmg))
player.mp -= heal_A.cost
print("You lost " + str(heal_A.cost) + " mana")
player_hurt(cricket)
PLAYER CODE BELOW
# IMPORTS
import random
#CLASS PLAYER
class Player():
def __init__(self, name, hp, mp, atk, df, magic, items):
self.maxhp = hp
self.hp = hp
self.maxmp = mp
self.mp = mp
self.atklow = atk - 2
self.atkhigh = atk + 2
self.df = df - 100
self.dfc = df / 100
self.magic = magic
self.items = items
self.actions = ["Attack", "Magic", "Items", "Stats"]
self.name = name
#Dealing Damage
def generate_dmg(self):
return random.randrange(self.atklow, self.atkhigh)
#Taking Damage
def take_dmg(self, dmg):
self.hp -= dmg * self.dfc
if self.hp < 0:
self.hp = 0
return self.hp
#Healing
def heal(self, dmg):
self.hp += dmg
if self.hp > self.maxhp:
self.hp = self.maxhp
#Returning HP Values
def get_hp(self):
return self.hp
def get_maxhp(self):
return self.maxhp
#Mana Cost Logic
def reduce_mp(self, cost):
self.mp -= cost
#Actions
def get_stats(self):
print("HP")
print(self.hp)
print("--")
print(self.maxhp)
print("")
print("MP")
print(self.mp)
print("--")
print(self.maxmp)
print("")
print("ATK")
print(self.atklow + self.atkhigh)
print("DEF")
print(self.dfc)
MONSTER CODE BELOW
import random
class Monster:
def __init__(self, name, atk, df, hp):
self.name = name
self.atk = atk
self.hp = hp
self.maxhp = hp
self.atklow = self.atk - 2
self.atkhigh = self.atk + 2
def damage_dealt(self):
return random.randrange(self.atklow, self.atkhigh)
def damage_taken(self, dmg):
self.hp -= dmg
if self.hp < 0:
self.hp = 0
return self.hp
def get_hp(self):
return self.hp
def get_maxhp(self):
return self.maxhp
SPELLS
import random
class Spells:
def __init__(self, type, name, dmg, cost):
self.type = type
self.name = name
self.dmg = dmg
self.cost = cost
def generate_dmg(self):
low = self.dmg + 3
high = self.dmg - 3
return random.randrange(low, high)
ITEMS
class Item:
def __init__(self, type, name, desc, amount):
self.type = type
self.name = name
self.desc = desc
self.amount = amount
GAMEPLAY(not sure if this is gonna be used)
import random
import os
class Fight:
def __init__(self, enemy, player):
self.enemy = enemy
self.player = player
self.playert = True
self.enemyt = False
self.player_action_allowed = False
self.player_action_allowed = False
def turns(self):
if self.playert == True:
self.player_action_allowed = True
self.enemyt = True
self.playert = False
elif self.enemyt == True:
enemy_action_allowed = True
player_action_allowed = False
playert = True
enemyt = False
else:
playert = True