Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
questions about storage
#1
I'm gearing up to make a CCRPG in Python. The characters will be point bought. The starting points for a level 1 character is 100 points. Each level up gives them another 20 points. This will be super heroic in genre and the abilities reflect such. I'm working with a 2d plane so Flight will do things like add defense as well as immunity to pits and floor traps.

The abilities will all be 20 points at first level and 10 points at every other level of purchase. The levels of purchase can change multiple things. Let's say you bought level 1 Fire Powers the initial purchase costs 20 points (so the equivalent of a level up), each subsequent level costs 10. When you invest that 20 in Fire Powers, you gain an attack that does 5 damage (example for sake of discussion). When you put in the 10 points you have the choice of increasing the base attack's damage by 5, making an area of effect attack that does 5, or put down a hazard that persists for a duration called Fire Rain that at base lasts 3 turns doing 5 damage to everything in it a turn. Let's say you increased the base attack's damage by 5, so it does 10. All the other aspects of Fire Powers that your bought are still doing 5 damage. Let's say you put in another 10 points you have the choice of increasing the base attack's damage further, buying an aspect, or increasing an aspect that you bought already's damage by 5 (or increasing it's duration in case of the Fire Rain).

With such a widely varying character creation and leveling system, which data structure is best to save the characters in? I'll have a party of these characters, would it be best to have a data structure for each individual character?
Reply
#2
I'm only just getting back into coding after an absence, but I remember that the answer to most questions like this is "Dictionaries".

You can access and store all the numbers you described in python dictionaries. And even better, with "nested dictionaries" which are dictionaries inside dictionaries. You can also write game saves as dictionaries to text files.

save_game = {"game_data" : {"players" : [character1_data, character2_data],
                                           "map data" : map_data,
                                           "story data" : story_data}}
It took me a long time for me to understand them, but now I can't run out of uses.

Here is a program I wrote that used chatgpt to make original text adventures. As you can see, a dictionary is basically the whole game.
import json
import openai
import os


def json_dict(string):
    return json.loads(string)

def gpt3(stext):
    openai.api_key = "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"

    response = openai.Completion.create(
      model="text-davinci-003",
      prompt= stext,
      temperature=0.7,
      max_tokens=4000,
      top_p=1,
      frequency_penalty=0,
      presence_penalty=0
    )
    content = response.choices[0].text.split('.')
    
    return response.choices[0].text
genre = "sci-fi"
string = gpt3(f'write a CYOA script using JSON string to be turned into a python dictionary using json.loads().The keys are scene names. Values are lists containing [description, choices, list of scenes those choices lead to]. do not name the dictionary. Base story on poplular sci-fi movie.')
scenes = json_dict(string[string.find('{'):])
current_scene = list(scenes)[0]

while True:

    os.system('clear')
    print(scenes[current_scene][0])
    print()
    print("What will you do?")
    print()

    # Print out the available choices
    for i, choice in enumerate(scenes[current_scene][1]):
        print(str(i + 1) + ") " + choice)

    # Get the player's choice
    choice = input("Enter your choice: ")

    # Check to make sure the choice is valid
    if int(choice) > 0 and int(choice) <= len(scenes[current_scene][1]):
        current_scene = scenes[current_scene][2][int(choice) - 1]
    else:
        print("Invalid choice!")
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020