DEV Community

Cover image for AI Generated Python RPG combat system.
Kurtiss Frost
Kurtiss Frost

Posted on • Updated on

AI Generated Python RPG combat system.

Edit 12/17/22:

The ai has been update and it FINALLY completed the second prompt. You can find it in the repo

So here recently, I have been messing around with the ChatGPT ai and I gotta say, it's been a lot of fun just messing around and asking it different prompts. You can find the code over on my GitHub page.

One thing I wanted to try out was asking it to write something in a coding language. I remember back to when I was working on Monster Killer and how at that time, I had no idea how I would go about writing a combat system because I didn't understand that much about Python. At this point in time, I still don't know much because I kind of put learning that on the back burner now so I could focus on learning JavaScript for front-end development.

So I decided to ask the ai the following prompt:

"use python. Create an RPG combat system where you are a knight that fights a Goblin."

This is what it gave me:



# define the Knight class
class Knight:
  def __init__(self, name, health, attack_power):
    self.name = name
    self.health = health
    self.attack_power = attack_power

  def attack(self, opponent):
    print(f"{self.name} attacks {opponent.name}!")
    opponent.health -= self.attack_power
    print(f"{opponent.name}'s health is now {opponent.health}.")

# define the Goblin class
class Goblin:
  def __init__(self, name, health, attack_power):
    self.name = name
    self.health = health
    self.attack_power = attack_power

  def attack(self, opponent):
    print(f"{self.name} attacks {opponent.name}!")
    opponent.health -= self.attack_power
    print(f"{opponent.name}'s health is now {opponent.health}.")

# create a Knight object
knight = Knight("Sir Arthur", 100, 10)

# create a Goblin object
goblin = Goblin("Goblin", 50, 5)

# have the Knight and Goblin fight until one of them dies
while True:
  knight.attack(goblin)
  if goblin.health <= 0:
    print(f"{goblin.name} has been defeated!")
    break
  goblin.attack(knight)
  if knight.health <= 0:
    print(f"{knight.name} has been defeated!")
    break


Enter fullscreen mode Exit fullscreen mode

Now, whenever I run that, it literally just has the Knight auto attack the Goblin and the Goblin doesn't retaliate. So, while it did do what I asked it, it's not exactly what I was looking for. So I decided to go back and ask it again and restructure my prompt as follows:

use python and make an RPG combat system where you can fight a goblin. The user controls the knight. You have three options: Attack which deals damage. Defend which reduces damage taken by 25%. and Run which has a 50% chance to end the combat. The goblin can only attack or defend and picks at random.

I wanted to see how complex you could make the prompt and if the ai would be able to execute it. This is what it turned out:



Did not save this code. Sorry 🙁


Enter fullscreen mode Exit fullscreen mode

It took it a minute on this one. I thought about it for a minute and then started spitting out code until it got to



knight_health -=


Enter fullscreen mode Exit fullscreen mode

it appeared to get hung up here and it thought about it for a minute before continuing. I sat there for about a minute and decided I would start a stopwatch and see how long the ai would be stcuk. The final time was 21 minutes! It never managed to complete the prompt. So I decided to reset it and start over. Sadly, I didn't copy that code before the refresh.

For the third and final test, I ran the same prompt but this time, I started the timer as soon as I hit enter on the prompt so I could see how long it would take for the ai to generate my prompt. In the middle of it, I had to leave to run some errands I decided to just let it do it's thing while I was gone. I was gone for around When I came back, this is what it had done:



import random

class Character:
  def __init__(self, name, health, attackPower):
    self.name = name
    self.health = health
    self.attackPower = attackPower

  def attack(self, enemy):
    print(f"{self.name} attacks {enemy.name}!")
    enemy.health -= self.attackPower
    print(f"{enemy.name}'s health is now {enemy.health}.")

  def defend(self):
    print(f"{self.name} defends against the enemy attack.")
    self.health += self.health * 0.25
    print(f"{self.name}'s health is now {self.health}.")

  def run(self):
    if random.random() <= 0.5:
      print(f"{self.name} successfully flees from the enemy!")
      return True
    else:
      print(f"{self.name} fails to flee from the enemy!")
      return False

# Create the player character
knight = Character("Knight", 100, 20)

# Create the enemy
goblin = Character("Goblin", 50, 10)

# Start the combat loop
while knight.health > 0 and goblin.health > 0:
  # Display the current health of each character
  print(f"{knight.name}'s health: {knight.health}")
  print(f"{goblin.name}'s health: {goblin.health}")

  # Display the options to the player
  print("What would you like to do?")
  print("1. Attack")
  print("2. Defend")
  print("3. Run")

  # Get the player's input
  choice = int(input("> "))

  # Handle the player's choice
  if choice == 1:
    knight.attack(goblin)
  elif choice == 2:
    knight.defend()
  elif choice == 3:
    if knight.run():
      break

  # The goblin attacks or defends at random
  if random.random() <= 0.5:
    goblin.attack(knight)
  else:
    goblin.defend()

# Print a message when the combat



Enter fullscreen mode Exit fullscreen mode

The prompt got hung up and didn't complete. I let it run for 2 and a half hours and it never managed to complete it.

I think I may have made the prompt a bit too complicated. One thing I have learned is that your prompts seem to have to be very direct but not overly complicated (or at least in my experiments). If I make the prompt too complicated, the ai gets hung up and doesn't complete the prompt. If I make it too simple, it seems to generate wonky code that either doesn't work or doesn't follow the original prompt.

Overall, I think the ChatGPT ai is cool and I want to keep messing around with it. It has some super cool functionality and it's pretty neat to mess around on. The downside is that when you are inputting your prompts, you almost have to be a wordsmith. Too many words seem to confuse it, and not enough seem to make it not always give you what you're looking for.

Overall though, I think it is a super neat ai and I'm going to keep experimenting with it. Eventually I would like to find a way to work it into a project somehow but, that is something for a WAY later date. I have so much to study now for my Udemy course and for web development that I have put learning Python on the back burner for now. For now, it is something I mess around with when I have a little free time. If you are interested, I have uploaded the files over on my GitHub page.

Thanks so much for reading and have a wonderful day!

signature

Top comments (0)