I want to create a turn based combat focused game, where you could battle random opponents. The problem comes from the random part. At the beginning of this project, I started things small and simple to give myself time to understand the parts of the code that didn't seem too frightening.
Unfortunately I realized pretty early that I need some more complex line of codes, to able to do some things, and also, to avoid writing the same code 100s of times.
Here's the code with some comments on a few things that have problems/don't work. (there might be some typos/older version of code, cuz I tried to solve things a few different way, so please ignore those if there is any, and only focus on the class/random enemy related stuff. But I tried my best to find all of them, by going through everything a few times)
I'm not asking for help to solve everything, in fact my intuition suggests that most of, if not all problems comes from me not understanding a key part of classes. So, if someone could enlighten me what I'm doing wrong, I would really appreciate it. (unless what I want to do is not possible)
Code: Select all
# python block for classes
init python: # basic stats for the characters, keeping things simple for now
class chara:
def __init__(self, name, max_HP, HP, ATK, crit_chance):
self.name = name
self.max_HP = max_HP
self.HP = HP
self.ATK = ATK
self.crit_chance = crit_chance
def incombat(self): # here, im trying to make something that would help me set up the random enemy
if current_enemy == self.name: # for the battles (there are probably major problems with it, that i dont understand/see)
return True
else:
return False
Creatures = []
exists = 0
while exists < 100:
Creatures.append(chara((""), 0, 0, 0, 0))
exists += 1
##################################
# placeholder images
image player_pic = "player.png"
image enemy_pic = "enemy.png"
image goblin_pic = "goblin.png"
image orc_pic = "orc.png"
##############################
# game starts here
label start:
call variables
menu:
"Enter battle":
$ current_enemy = renpy.random.choice(['Enemy', 'Goblin', 'Orc']) ## the random enemy would be based on this
python: # this, idk, i currently dont know enough to fully understand what is going on here
for q in Creatures: # but what i want, is to use the stats based on the random enemy chosen
if q.incombat(""): # and apply it to the one in battle (i think it should be possible, but im not that smart)
combat_enemy = q.name
combat_enemy.max_HP = q.max_HP
combat_enemy.HP = q.HP
combat_enemy.ATK = q.ATK
comabt_enemy.crit_chance = q.crit_chance
show screen HP_bars
call p_vs_enemy
"Do nothing":
"Nothing happened."
jump start
#########################
# some variables
label variables:
$ Creatures[0] = chara("Player", 50, 50, 5, 50)
$ Creatures[1] = chara("Enemy", 15, 15, 2, 0)
$ Creatures[2] = chara("Goblin", 30, 30, 5, 25)
$ Creatures[3] = chara("Orc", 50, 50, 10, 75)
$ Creatures[4] = chara("combat_enemy", 0, 0, 0, 0) # i dont know if this is correct or needed at all, its here because i thought it would help/needed
$ current_enemy = "none" # i want to use this with "renpy.random.choice" ,and i was hoping that, i would be able to use that as something
# that would help me set up battles with random enemies, if there is an enemy with that name
#############################################
# random number generator for battle actions, nothing here is final
label RNG:
$ p_normal_attack = renpy.random.randint(1, 4)
$ p_big_attack = renpy.random.randint(8, 12)
$ e_normal_attack = renpy.random.randint(2, 8)
$ e_big_attack = renpy.random.randint(10, 16)
$ e_attack_type = renpy.random.randint(1, 2)
$ crit = renpy.random.randint(1, 100) # i use this for calculating crit chance, but it seems to be way too simple, so idk if
# its working as intended (i mean, i think it should work, it seemed to work, until i
return # tried to go for the random enemy set up)
###############################################
# placeholder images, eventually want to set up moving images so the battle feels more alive
label p_vs_enemy:
show player_pic at left
show enemy_pic at right
jump combat
#############################
# screen for hp bars, right now its pretty simple, would like to implement something that displays the number as well later
# also, doesnt fully work, same problem as everywhere else
screen HP_bars:
vbox:
spacing 20
xalign 0.1
yalign 0.0
xmaximum 600
text "Player"
bar value [Player.HP] range [Player.max_HP]
vbox:
spacing 20
xalign 0.9
yalign 0.0
xmaximum 600
text "[combat_enemy]"
bar value [combat_enemy.HP] range [combat_enemy.max_HP]
#################################
# here, i would like to use this for every battle, instead of creating copies for every possible matchup
# right now its just plain text to keep things simple (setting up skills scares me xd)
label combat:
while True: ### here i wanted to set up Player.HP > 0 and combat_enemy.HP > 0: ,but it didnt work
call RNG
menu: ### player turn starts here
"Normal attack":
if Player.crit_chance >= crit:
$ Player.ATK = p_normal_attack*2
$ combat_enemy.HP -= Player.ATK
"Player's noraml attack crits for [Player.ATK] damage!"
else:
$ Player.ATK = p_normal_attack
$ combat_enemy.HP -= Player.ATK
"Player's normal attack does [Player.ATK] damage."
"Hard attack":
if Player.crit_chance >= crit:
$ Player.ATK = p_big_attack*2
$ combat_enemy.HP -= Player.ATK
"Player's hard attack crits for [Player.ATK] damage!"
else:
$ Player.ATK = p_big_attack
$ combat_enemy.HP -= Player.ATK
"Player hard attack does [Player.ATK] damage."
"Do nothing":
"Nothing happened."
if combat_enemy.HP <= 0: # winning the battle
"Player wins."
hide screen HP_bars
hide player_pic
hide enemy_pic
jump start
call RNG # enemy turn starts here
if e_attack_type == 1: # this adds some randomness to the enemy (only 2 options for now)
if combat_enemy.crit_chance >= crit:
$ combat_enemy.ATK = e_normal_attack*2
$ Player.HP -= combat_enemy.ATK
"normal ATK crits for [combat_enemy.ATK] damage"
else:
$ Player.HP -= e_normal_attack
"normal ATK does [e_normal_attack] damage"
else:
if combat_enemy.crit_chance >= crit:
$ combat_enemy.ATK = e_big_attack*2
$ Player.HP -= combat_enemy.ATK
"combat_enemy big ATK crits for [combat_enemy.ATK] damage"
else:
$ Player.HP -= e_big_attack
"combat_enemy big ATK does [e_big_attack] damage"
if Player.HP <= 0: # losing the battle
"you lose"
hide screen HP_bars
hide player_pic
hide enemy_pic
jump start