python 3.x - text based adventure game, save game function
By : Abd-Elmlik Abd-Elsam
Date : March 29 2020, 07:55 AM
I wish this help you You seem to have some things that define state like : self.__player_health, self.__player_name, self.__health_potion, self.__big_wrench, self.__blue_keycard. I need to read more carefully, but couldn't find a state that defines progression. With everything that defines the state you can save and read from a text file - if you are aiming at simplicity. One way to go would be having a dict structure to store text, time to wait and function (if available, like the fighting ), like pages. Then using an array to store these pages, which would be your book. If you have a loop to keep reading those pages - reading would be another function. Then since the story is indexed, you now know where you are, so just save the states and position. code :
import sys, traceback
import json
userQuited = False
def _quit(dummy):
global userQuited
userQuited = True
def _save(dummy):
f=open("game.sav",'w+')
json.dump(states, f)
f.close
def _continue(dummy):
f=open("game.sav",'r+')
states = json.load(f)
f.close
def user(name):
states["player_name"] = name
states = {
"player_health" : 100,
"player_name" : "",
"health_potion" : 0,
"big_wrench" : 0,
"blue_keycard" : 0,
"current_page" : "page1"
}
book = {
"page1": {
"text": """You are in an underwater research facility that has been
attacked by unknown assailants. As the assistant to the head
researcher, you were hired in to assist Dr. Weathers with
the Lightning Sword project. Project Lightning Sword still
remains in large a mystery to you. Although you worked closely
with the doctor you were never allowed to see the big picture.
Right now that's the least of your worries, as chaos ensues around you.
Alarms were triggered a while ago. As you sit in your room,
you hear short bursts of automatic rifle fire and only one
thing is for certain, you have to survive.\n """,
"inputText": "What would you like your player to be named?",
"function": user,
"next": "page2"
},
"page2": {
"text": "Welcome, %(player_name)s please choose what you would like to do",
"inputText": "0 = Quit, 1 = Start New Game, 2 = Continue",
"choices": {
"0" : "Quit",
"1" : "page3",
"2" : "Continue"
}
},
"page3" : {
"text" : """You are standing in your room, door is locked and you're wondering
if you will get out of this alive. The gunfire keeps getting closer and you know that you
can't stay here, you have to make a move and try and get out. Otherwise it's just a matter
of time before they find you and that option doesn't look very promising.\n\n
You are ready to try your escape but first you should try and get any useful
things from your room to use along the way. Your room-office is a bit bigger than a walk in closet,
textile flooring and gray walls encompas your view and the only things in the room are the bed you
are sitting on (not very comfortable, looks more like a prisoners bed), a framed picture on the wall
and your work desk which has barely enough space for your equipment and computer.""",
"inputText": "1 = Walk over to the picture frame, 2 = Walk over to the desk, 3 = Exit the door",
"choices": {
"1" : "page4",
"2" : "page5",
"3" : "page7"
}
},
"Quit" : {
"text": "goodbye!",
"inputText": "",
"function" : _quit
},
"Continue" : {
"text": "welcome back!",
"inputText": "",
"function" : _continue
}
}
def processPage(page):
answer = ""
print(page["text"] % states)
if len(page["inputText"]) > 1 :
answer = raw_input(page["inputText"] % states)
if "function" in page:
page["function"](answer)
if "next" in page:
return page["next"]
if "choices" in page:
for choice in page["choices"]:
if choice == answer:
print page["choices"][choice]
return page["choices"][choice]
return ""
def main():
global userQuited
while(userQuited==False):
states["current_page"] = processPage(book[states["current_page"] ])
if (states["current_page"] != "Quit" and states["current_page"] != "Continue"):
_save("")
main()
|
Having issues with State changes in a Text Adventure in Unity 4.6
By : coolfun
Date : November 04 2020, 04:05 PM
This might help you You should create two states for Cell, first one listens to C and changes the state to second, second one listens to B and changes the state to Bed. The reason is that state machines do not follow after where they left in the middle of the code inside one state. You need to be more specific when you are defining your states. code :
bool c_pressed = false;
void state_Cell () {
if (c_pressed || Input.GetKeyDown (KeyCode.C)) {
c_pressed = true;
if (Input.GetKeyDown (KeyCode.B)) {
c_pressed = false;
MyState = States.Bed;
}
}
else
c_pressed = false;
}
}
|
Item/Ability system using scriptableobjects in Unity
By : Sidharth Mishra
Date : March 29 2020, 07:55 AM
help you fix your problem If your question is "how do I create an instance of an Item in my project", you do that by right clicking in your Project view, then selecting Create -> Item. Then drag the Item asset that you created from your Project view into the ItemDisplay scene object's item field in the Inspector. Regarding how to pick up the item, you would pass the item to your Player script from your ItemDisplay script. You've got most of that already wired up. code :
public class ItemDisplay : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
var playerPickUp = other.gameObject;
var playerScript = playerPickUp.GetComponent<Player>();
var playerItem = playerScript.playerItem;
// This is where you would pass the item to the Player script
playerScript.pickUpItem(this.item);
bool isActive = true;
Object.Destroy(gameObject);
}
}
}
public class Player : MonoBehaviour
{
public void PickUpItem(Item item)
{
this.playerItem = true;
this.itemInHand = item;
Debug.Log(string.Format("You picked up a {0}.", item.name));
}
public void UseItem()
{
if (this.itemInHand != null)
{
Debug.Log(string.Format("You used a {0}.", this.itemInHand.name));
}
}
}
public abstract class Item : ScriptableObject
{
// Properties that are common for all types of items
public int weight;
public Sprite sprite;
public virtual void UseItem(Player player)
{
throw new NotImplementedException();
}
}
[CreateAssetMenu(menuName = "Items/Create Potion")]
public class Potion : Item
{
public int healingPower;
public int manaPower;
public override void UseItem(Player player)
{
Debug.Log(string.Format("You drank a {0}.", this.name));
player.health += this.healingPower;
player.mana += this.manaPower;
}
}
[CreateAssetMenu(menuName = "Items/Create Summon Orb")]
public class SummonOrb : Item
{
public GameObject summonedCreaturePrefab;
public override void UseItem(Player player)
{
Debug.Log(string.Format("You summoned a {0}", this.summonedCreaturePrefab.name));
Instantiate(this.summonedCreaturePrefab);
}
}
public class Player : MonoBehaviour
{
public void UseItem()
{
if (this.itemInHand != null)
{
this.itemInHand.UseItem(this);
}
}
}
|
Text-based adventure game, attacking causes game to crash
By : user3102696
Date : March 29 2020, 07:55 AM
should help you out It would be better if you added the get_available_actions(arg1, arg2) function. It appears that this function does not return a value or returns None (which is the same this). If you can add more of your code we can analyze this error further. Otherwise, you should try to change the return to something can use the method .get(arg1, arg2). code :
def get_available_actions(room, player):
actions = OrderedDict()
print("Choose an action: ")
if player.inventory:
action_adder(actions, 'i', player.print_inventory, "Print inventory")
if isinstance(room, world.EnemyTile) and room.enemy.is_alive():
action_adder(actions, 'a', player.attack, "Attack")
else:
if world.tile_at(room.x, room.y - 1):
action_adder(actions, 'n', player.move_north, "Go north")
if world.tile_at(room.x, room.y + 1):
action_adder(actions, 's', player.move_south, "Go south")
if world.tile_at(room.x + 1, room.y):
action_adder(actions, 'e', player.move_east, "Go east")
if world.tile_at(room.x - 1, room.y):
action_adder(actions, 'w', player.move_west, "Go west")
if player.hp < 100:
action_adder(actions, 'h', player.heal, "Heal")
return actions
|
Text Based Adventure Game
By : Cosmin Gabriell
Date : March 29 2020, 07:55 AM
Hope this helps I'll try to help as best I can a piece at a time. My line numbers might be slightly different from yours, so feel free to look around a bit. In:
|