How do you add first person fighting inside the game?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
question

How do you add first person fighting inside the game?

#1 Post by question »

First person as you cannot see yourself and with choices such as attack, defend, etc.

Aenakume
Regular
Posts: 182
Joined: Mon Aug 11, 2008 4:38 am
Projects: Arts... i hate arts -_-
Contact:

Re: How do you add first person fighting inside the game?

#2 Post by Aenakume »

i'm no expert, but if you mean turn-based first-person fighting (like paper-rock-scissors, except maybe parry-attack-defend), that's a snap.

Just give yourself and your opponent a stamina value:

Code: Select all

init python:
  my_stamina = 100
  their_stamina = 100
Then enter a loop:

Code: Select all

while not (my_stamina == 0 or their_stamina == 0):
  
  # Here you can show your opponent getting ready for the next round of fisticuffs
  # You can even use their_stamina to show them more or less injured
  
  # Make your move
  menu:
    "Attack":
      $ my_move = 1
    "Parry":
      $ my_move = 2
    "Defend":
      $ my_move = 3
  
  # They make theirs
  $ their_move = renpy.random.randint(1, 3)
  
  # Figure out the result of the round
  if my_move == 1 and their_move = 1:
    "You both throw powerful punches at the same time!"
    $ my_stamina = my_stamina - 5
    $ their_stamina = their_stamina - 5
  if my_move == 1 and their_move = 2:
    "You throw a powerful punch... but your opponent parries your attack!"
    $ my_stamina = my_stamina - 5
  if my_move == 1 and their_move = 3:
    "You throw a powerful punch... that your opponent tries to block!"
    $ their_stamina = their_stamina - 1
  # And so on for the other 6 combinations

# When you get here, the fight is over
if my_stamina == 0 and their_stamina == 0:
  "Double KO!"
elif my_stamina == 0:
  "You suck."
elif their_stamina == 0:
  "You stand over their battered, twitching body, cracking your knuckles... who's next?"
That's just the basics on how to start. Next you can add levels to control how much damage you do/take with each attack, modifiers for things like weapons/armour, and of course a status display to show your stamina (see this cookbook recipe for an idea on how).
“You can lead a fool to wisdom, but you cannot make him think.”

sciencewarrior
Veteran
Posts: 356
Joined: Tue Aug 12, 2008 12:02 pm
Projects: Valentine Square (writer) Spiral Destiny (programmer)
Location: The treacherous Brazilian Rainforest
Contact:

Re: How do you add first person fighting inside the game?

#3 Post by sciencewarrior »

I created a very simplistic example:

Code: Select all

while my_hp > 0 and enemy_hp > 0
    menu fight:
        "Your HP:%(my_hp)d  %(enemy_name)s HP:%(enemy_hp)d"
        "Attack":
            $ enemy_hp -= my_damage
            "You attack your enemy."
        "Use Potion" if potions > 0:
            $ my_hp += 10
            $ potions -= 1
            "You drink a refreshing potion."
    $ my_hp -= enemy_damage
    "%(enemy_name)s attacks you."
if my_hp <= 0
    "Sorry, you died"
    return
But Aenakume posted in the meantime, and I liked that better. :mrgreen:
Last edited by sciencewarrior on Tue Aug 26, 2008 9:38 pm, edited 1 time in total.
Keep your script in your Dropbox folder.
It allows you to share files with your team, keeps backups of previous versions, and is ridiculously easy to use.

Aenakume
Regular
Posts: 182
Joined: Mon Aug 11, 2008 4:38 am
Projects: Arts... i hate arts -_-
Contact:

Re: How do you add first person fighting inside the game?

#4 Post by Aenakume »

sciencewarrior wrote:But Aenakume posted in the meantime, and I liked that better. :mrgreen:
Actually, yours is more adaptable. ^_^; i didn't even make the strings mention the enemy's name during the fight. How lazy is that? "Ya, uh, you're bein attacked by... something." ^_^;

i suppose it might even be possible to make the fight real-time - *hinthint!* ^_-
“You can lead a fool to wisdom, but you cannot make him think.”

User avatar
tigerrenko
Regular
Posts: 103
Joined: Mon Aug 25, 2008 9:32 pm
Projects: Partisans
Organization: Red Viking
IRC Nick: tigerrenko
Deviantart: tigerrenko
Skype: rankogm
Location: Belgrade
Contact:

Re: How do you add first person fighting inside the game?

#5 Post by tigerrenko »

Wow! Great thanks to you guys!

Based on your code I made small hunting game! User has 12 arrows and picks what game to hunt. Some game is harder to catch but is worth more points. Game checks for arrows, tracks score, reports failure or success, etc. I know, it's really a simple loop, but as a total newb I could not have done it without you!

This has to be first loop in my life :)

Cheers
TR

Amaku
Regular
Posts: 50
Joined: Thu Jun 19, 2008 9:35 pm
Contact:

Re: How do you add first person fighting inside the game?

#6 Post by Amaku »

I love the script *o* But i have a pair of questions. Is it possible to show messages after an attack passes like : "Be careful! Now you have 20 HP!" or "Well done! Enemy HP -10!" ?

And also to have the enemy with different attacks from you? Thanks a lot :3

Verity
Regular
Posts: 67
Joined: Wed Jul 30, 2008 11:43 am
Projects: Wanderer
Location: USA
Contact:

Re: How do you add first person fighting inside the game?

#7 Post by Verity »

Amaku wrote:I love the script *o* But i have a pair of questions. Is it possible to show messages after an attack passes like : "Be careful! Now you have 20 HP!" or "Well done! Enemy HP -10!" ?

And also to have the enemy with different attacks from you? Thanks a lot :3
As to the first, after your turn, you could add an if statement like this:

Code: Select all

if my_hp <= 20:
    "Be careful!  Now you have 20 HP!"
Something similar can be done for your other example—the "if" being whether your attack is successful or not.

As to the second question, you just need to define separate attacks for the enemy and then decide what you want them to do. You can use the renpy.random.choice or renpy.random.randint functions to choose one attack from several if you want more than one, and then use an if statement to determine which one was chosen and do the associated action(s). (I hope this makes sense.)

sciencewarrior
Veteran
Posts: 356
Joined: Tue Aug 12, 2008 12:02 pm
Projects: Valentine Square (writer) Spiral Destiny (programmer)
Location: The treacherous Brazilian Rainforest
Contact:

Re: How do you add first person fighting inside the game?

#8 Post by sciencewarrior »

@TR: Glad to help. :D If you need a hand in your new game, just let us know.

@Amaku: as Verity said, yes to both questions. You can show the exact HP by using "Be careful! Now you have %(my_stamina)d HP!".

If you aren't used to programming, writing the steps in plain English before you try to implement them will help. If you can't make it, you can post description and code and we can show where they don't match.
Keep your script in your Dropbox folder.
It allows you to share files with your team, keeps backups of previous versions, and is ridiculously easy to use.

Amaku
Regular
Posts: 50
Joined: Thu Jun 19, 2008 9:35 pm
Contact:

Re: How do you add first person fighting inside the game?

#9 Post by Amaku »

Ok here i go! Please very please, take a look and fix my code >_<. I will add you to the game credits :3

I have some questions please.
1.- Will the code loop when a phase is over?
2.- How can i configure the items?
3.- Can i change in-game my max stats? Like at first my stamina us at 5 but then i level up and my stamina is at 10. The same for my other stats.
4.- Also, if i have my stamina at max, i mean 5, and i use potion, will it rise up to 10? Or the limit is set, if is not, how can i set it? :3

Code: Select all

init python:
  my_stamina = 5
  my_magic = 5
  my_combo = 5
  their_stamina = 10
  my_partner = 0
And some things that might seem weird in the script XD

1.- I have a "Partner" sistem, so depending of your partner, the skills change. I think it can be like this?

Code: Select all

 "Who will you fight with?"
  menu:
    "Friend1" if friend_1 >= 10:
      $ my_partner = 1
    "Friend2" if friend_2 >= 10:
      $ my_partner = 2
    "Friend3" if friend_3 >= 10:
      $ my_partner = 3
    "Friend4" if friend_4 >= 10:
      $ my_partner = 4
    "Friend5" if friend_5 >= 10:
      $ my_partner = 5
2.- I am working with the DSE2 but i changed it a bit, instead of stats they are friend relations, for the combos and magic.

And here is my code

Code: Select all

while not (my_stamina == 0 or their_stamina == 0):
 
  # Here you can show your opponent getting ready for the next round of fisticuffs
  # You can even use their_stamina to show them more or less injured
 label menu:
  # Make your move
  menu:
    "Attack":
      $ my_move = 1
    "Magic":
      jump magic
    "Combo":
      jump combo
    "Item" if potions > 0 and/or if eter > 0 and/or if bread > 0 :
      jump item
 
 label magic:
  menu:
    "Fire" if partner == partner1 and while not my_magic ==0:
      $ my_move = 2
    "Water" if partner == parnter2 and while not my_magic ==0:
      $ my_move = 3
    "Wind" if partner == parnter3 and while not my_magic ==0:
      $ my_move = 4
    "Dark" if partner == partner4 and while not my_magic ==0:
      $ my_move = 5
    "Light" if partner == partner5 and while not my_magic ==0:
      $ my_move = 6
    "Return":
      jump menu
      
 label combo:
  menu:
    "Soul Refrain 10 CP" if :
      $ my_move = 7
    "Candenza 5 CP" while not my_combo <=5:
      $ my_move = 8
    "Total Darkness 15 CP" while not my_magic <=15:
      $ my_move = 9
    "Final Punch 20 CP" while not my_magic <=20:
      $ my_move = 10
    "Sacred Wings 3 CP" while not my_magic <=3:
      $ my_move = 11
    "Return":
      jump menu

 label item:
  menu:
    "Potion (show here how many potions are) : Restores Stamina " while not potion ==0:
      $ my_move = 12
    "Ether (show here how many etheres are) : Restores Magic " while not my_combo <=5:
¡      $ my_move = 13
    "Ether (show here how many bread are) : Restores Combo points" while not my_magic <=15:
      $ my_move = 14
    "Return":
      jump menu

  # Figure out the result of the round
  if my_move == 1:
    "You attacked fiercely!!!"
    $ their_stamina = their_stamina - 2
    "Now the enemys Stamina is in %(their_stamina)d Keep it up!"
  if my_move == 2:
    "Uh oh! It doesnt seem to work... the enemy blocked it..."
  if my_move == 3:
    "Whoa!! that was great! Its too effective"
    $ their_stamina = their_stamina - 6
    "Now the enemys Stamina is in %(their_stamina)d Keep it up!"
  if my_move == 4:
    "Thats ok...."
    $ their_stamina = their_stamina - 4
    "Now the enemys Stamina is in %(their_stamina)d Keep it up!"
  if my_move == 5:
    "BE CAREFULL!!! looks like the enemy reflects this element!"
    $ their_stamina = my_stamina - 4
    "Now your Stamina is in %(my_stamina)d"
  if my_move == 6:
    "Uh oh! It doesnt seem to work... the enemy blocked it..."
  if my_move == 7:
    "Ready for the action!"
    $ their_stamina = their_stamina - 8
    "Great team work! Now the enemys Stamina is in %(their_stamina)d Keep it up!"
  if my_move == 8:
    "Uh oh! It doesnt seem to work... the enemy blocked it..."
  if my_move == 9:
    "Take this!!"
    $ their_stamina = their_stamina - 6
    "Good!! Now the enemys Stamina is in %(their_stamina)d Keep it up!"
  if my_move == 10:
    "Feel my power!"
    $ their_stamina = their_stamina - 10
    " That was Super Effective! Now the enemys Stamina is in %(their_stamina)d Keep it up!"
  if my_move == 11:
    "Heaven help us!"
    $ my_stamina = my_stamina + 10
    "Now your Stamina is in %(my_stamina)d"
  if my_move == 12:
    "You recovered 10 points! Now your Stamina is in %(their_stamina)d"
    
    
  # They make theirs
  $ their_move = renpy.random.randint(1, 5)

  if their_move == 1:
    "The enemy is going to attack!!"
    $ my_stamina = my_stamina - 2
    "Now your Stamina is in %(their_stamina)d Keep your guard up!"
  if their_move == 2:
    "The enemy missed! Good work!"
  if their_move == 3:
    "Whoa!! that was great! Its too effective Take care!"
    $ my_stamina = my_stamina - 6
    "Now your Stamina is in %(my_stamina)d Keep it up!"
  if their_move == 4:
    "Take care!"
    $ my_stamina = my_stamina - 4
    "Now your Stamina is in %(my_stamina)d Keep it up!"
  if their_move == 5:
    "You reflected it!!"
    $ their_stamina = their_stamina - 10
    
# When you get here, the fight is over
if my_stamina == 0 and their_stamina == 0:
  "You couldnt survive"
elif my_stamina == 0:
  "Hey!! Are you alright?!? We will be sending backup soon!"
  jump game_over
elif their_stamina == 0:
  "Well done!! Keep up the good work!"

User avatar
Showsni
Miko-Class Veteran
Posts: 563
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: How do you add first person fighting inside the game?

#10 Post by Showsni »

Okay, I've made some improvements, though it still needs work. Put all this after the start label to see if it's what you want:

Code: Select all

    $ my_move = 0
    $ bread = 0
    $ potions = 0
    $ ether = 0
    $ friend_1 = 0
    $ friend_2 = 0
    $ friend_3 = 0
    $ friend_4 = 0
    $ friend_5 = 0
    $ my_stamina = 5
    $ my_magic = 5
    $ my_combo = 5
    $ their_stamina = 10
    $ my_partner = 0
    
    "For this test, please enter your stats."
    
    $ bread = int(renpy.input("Amount of bread"))
    $ potions = int(renpy.input("Amount of potions"))
    $ ether = int(renpy.input("Amount of ether"))
    $ friend_1 = int(renpy.input("Amount of points with friend 1"))
    $ friend_2 = int(renpy.input("Amount of points with friend 2"))
    $ friend_3 = int(renpy.input("Amount of points with friend 3"))
    $ friend_4 = int(renpy.input("Amount of points with friend 4"))
    $ friend_5 = int(renpy.input("Amount of points with friend 5"))
    $ my_stamina = int(renpy.input("Amount of stamina"))
    $ my_magic = int(renpy.input("Amount of magic"))
    $ my_combo = int(renpy.input("Amount of combo"))
    $ their_stamina = int(renpy.input("Amount of their stamina"))
        
    
    "Who will you fight alongside?"
    menu:
        "Friend1" if friend_1 >= 10:
            $ my_partner = 1
            jump friend
        "Friend2" if friend_2 >= 10:
            $ my_partner = 2
            jump friend
        "Friend3" if friend_3 >= 10:
            $ my_partner = 3
            jump friend
        "Friend4" if friend_4 >= 10:
            $ my_partner = 4
            jump friend
        "Friend5" if friend_5 >= 10:
            $ my_partner = 5
            jump friend
    "No one wants to be friends with you."
    
    label friend:
        pass

    
    
    while not (my_stamina <= 0 or their_stamina <= 0):
        label menu1:
            pass
        menu:
            "Attack":
                $ my_move = 1
                jump resolve
            "Magic":
                jump magic
            "Combo":
                jump combo
            "Item" if potions + ether + bread > 0:
                jump item

        label magic:
            menu:
                "Fire" if my_partner == 1 and not my_magic < 5:
                    $ my_move = 2
                "Water" if my_partner == 2 and not my_magic < 5:
                    $ my_move = 3
                "Wind" if my_partner == 3 and not my_magic < 5:
                    $ my_move = 4
                "Dark" if my_partner == 4 and not my_magic < 5:
                    $ my_move = 5
                "Light" if my_partner == 5 and not my_magic < 5:
                    $ my_move = 6
                "Return":
                    jump menu1
            jump resolve
            
        label combo:
            menu:
                "Soul Refrain 10 CP" if not my_combo < 10:
                    $ my_move = 7
                "Candenza 5 CP" if not my_combo < 5:
                    $ my_move = 8
                "Total Darkness 15 CP" if not my_combo < 15:
                    $ my_move = 9
                "Final Punch 20 CP" if not my_combo < 20:
                    $ my_move = 10
                "Sacred Wings 3 CP" if not my_combo < 3:
                    $ my_move = 11
                "Return":
                    jump menu1
            jump resolve
        
        label item:
            menu:
                "Potion (%(potions)s left): Restores Stamina " if not potions ==0:
                    $ my_move = 12
                "Ether (%(ether)s left): Restores Magic " if not ether == 0:
                    $ my_move = 13
                "Bread (%(bread)s left): Restores Combo points" if not bread == 0:
                    $ my_move = 14
                "Return":
                    jump menu1

  # Figure out the result of the round
        label resolve:
            pass
        if my_move == 1:
            "You attacked fiercely!!!"
            $ their_stamina -= 2
            "Now the enemy's stamina is %(their_stamina)s. Keep it up!"
        if my_move == 2:
            "You throw a fireball!"
            $ my_magic -=5
            "Uh oh! It doesn't seem to work... the enemy blocked it..."
            "You have %(my_magic)s magic points left."
        if my_move == 3:
            "You shoot a jet of water!"
            $ my_magic -=5
            "Whoa!! that was great! It's super effective!"
            $ their_stamina -= 6
            "Now the enemy's stamina is %(their_stamina)s! Keep it up!"
            "You have %(my_magic)s magic points left."
        if my_move == 4:
            "You summon a tornado!"
            $ my_magic -=5
            "That's ok...."
            $ their_stamina -= 4
            "Now the enemy's stamina is %(their_stamina)s! Keep it up!"
            "You have %(my_magic)s magic points left."
        if my_move == 5:
            "You unleash dark energy!"
            $ my_magic -=5
            "BE CAREFUL!!! It looks like the enemy can reflect this element!"
            $ my_stamina -= 4
            "Now your stamina is %(my_stamina)s!"
            "You have %(my_magic)s magic points left."
        if my_move == 6:
            "You blind them with a flash of light!"
            $ my_magic -=5
            "Uh oh! It doesn't seem to work... the enemy blocked it..."
            "You have %(my_magic)s magic points left."
        if my_move == 7:
            "You use Soul Refrain."
            "Ready for the action!"
            $ their_stamina -= 8
            $ my_combo -= 10
            "Great team work! Now the enemy's stamina is %(their_stamina)s! Keep it up!"
        if my_move == 8:
            "You use Candenza!"
            $ my_combo -=5
            "Uh oh! It doesn't seem to work... the enemy blocked it..."
        if my_move == 9:
            "You use Total Darkness!"
            $ my_combo -= 15
            "Take this!!"
            $ their_stamina -= 6
            "Good!! Now the enemy's stamina is %(their_stamina)s! Keep it up!"
        if my_move == 10:
            "You use Final Punch!"
            $ my_combo -=20
            "Feel my power!"
            $ their_stamina -= 10
            "That was super effective! Now the enemy's stamina is %(their_stamina)s! Keep it up!"
        if my_move == 11:
            "You use Sacred Wings!"
            $ my_combo -=3
            "Heaven help us!"
            $ my_stamina += 10
            "Now your stamina is %(my_stamina)s."
        if my_move == 12:
            "You quaff a potion."
            $ potions -=1
            $ my_stamina += 10
            "You recovered 10 stamina points! Now your stamina is %(my_stamina)s."
        if my_move == 13:
            "You use some ether."
            $ my_magic += 10
            $ ether -=1
            "You recovered 10 magic points! Now your magic is %(my_magic)s."
        if my_move == 14:
            "You bolt down a loaf of bread."
            $ my_combo += 10
            $ bread -= 1
            "You recovered 10 combo points! Now your combo power is %(my_combo)s."
    
    
  # They make theirs
        $ their_move = renpy.random.randint(1, 5)

        if their_move == 1:
            "The enemy is going to attack!!"
            $ my_stamina = my_stamina - 2
            "Now your stamina is %(my_stamina)s. Keep your guard up!"
        if their_move == 2:
            "The enemy missed! Good work!"
        if their_move == 3:
            "The enemy charges at you!"
            "Whoa!! That was powerful! It's super effective! Take care!"
            $ my_stamina = my_stamina - 6
            "Now your stamina is %(my_stamina)s. Keep it up!"
        if their_move == 4:
            "The enemy attacks you!"
            "Take care!"
            $ my_stamina = my_stamina - 4
            "Now your stamina is %(my_stamina)s. Keep it up!"
        if their_move == 5:
            "The enemy is attacking! But, you deflected it and countered! Nice move!"
            $ their_stamina = their_stamina - 10
            "Now the enemy's stamina is %(their_stamina)s! Keep it up!"
        if my_stamina < 0:
            $ my_stamina = 0
        if their_stamina < 0:
            $ their_stamina = 0
    
# When you get here, the fight is over
        if my_stamina == 0 and their_stamina == 0:
            "You couldn't survive."
        elif my_stamina == 0:
            "Hey!! Are you alright?!? We will be sending backup soon!"
            jump game_over
        elif their_stamina == 0:
            "The enemy is dead!"
            "Well done!! Keep up the good work!"

Amaku
Regular
Posts: 50
Joined: Thu Jun 19, 2008 9:35 pm
Contact:

Re: How do you add first person fighting inside the game?

#11 Post by Amaku »

Oh! Its fantastic!!! Thanks a lot, Showsni. its just what i need :3.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], IrisColt