I want to make a simple game on python, all text based. Sort of an interactive story with puzzles etc. but it is likely to be long. I want to be able to it possible for poeple to sort of, 'Save Progress'. Perhaps assign them selves a name etc? I am new to Python and am wondering if it is possible to set up a bank containing details of game play that can be accessed by entering a username and so will load the correct area of the code to allow the player to pick up where they left of.
Say they decided to quit the game just after... say, finding a clue to a murder. The computer would store the line of code they were on and get them to enter their name. Next time they could select 'Load' from the start up menu and then enter their name. The computer would then search for their name and pull up the peice of code they were at and continue as normal from there.
I am new at this so please try and explain simply. I can make a menu etc. no problems with the if's etc and make the story it's self. It is just the loading I want to add.
I hope this isn't tooooo complicated!? Please help me!
You should check out the pickle module. It allows you to store/retrieve any python object. One way to do this would be to have your game take place in a class, where all of the states/etc were members of that class (or derived classes). When they wanted to save you would just pickle your class!
You are pretty new to python, I would recommend reading the documentation provided here: http://docs.python.org/tutorial/ .
Python tries to be both easy to learn and comprehensive. Most questions answer themselves!
Depends on how you create your game...
Do you follow the tree of decisions that the user took in the game? If so you save the decisions in a file, that you save to a location (for example "saves/{username}/{name_of_save}.sav or just "saves/{username.sav} if every username should have only one save, but this is just a example it all depends on you). You could then allow them to select their save file and then simulate the actions from the file till you reach the point where the last decision took place.
If you would like to easily save the data to the file(s) you could also use a format like JSON, which python has built-in support for - read more at http://docs.python.org/library/json.html . This will allow you to easily create objects that you want to save/read from the file and use for processing.
Hope that helps...
Just a Idea, I have not actually tried anything like this
Take a look at http://www.sqlite.org/ or use plain text files for the beginning:) you could use the username:bitfield to encode you progress. Or use the ConfigParser module for Python and create a scetion for each user:) have fun learning:)
Related
I currently have two files: control.py and list.py. list.py contains a list of objects that I want to access and edit in control.py. In the project I'm working on, I should be able to open multiple instances of control.py (essentially, open x number of terminals each running its own instance of control.py). I want to able to access the list in list.py from each instance of control.py. Each instance should be able to edit the list such that after editing, all the other instances should be able to see the edited list.
I've looked into the shelve module but it turns out that shelve doesn't support concurrent modification, so that won't work because i need each instance to simultaneously be able to read and edit the list.
The usual solution to this problem is known as "pubsub"; i.e., publish/subscribe. See https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern for a primer on the topic. There is no way to do this using just the capabilities provided by the core Python libraries; at least not without writing a pubsub implementation using those capabilities.
However, I get the sense you are doing the programming equivalent of trying to use a hammer to drive a screw. That is, because a hammer is the only tool you know how to use every problem looks like a nail. Reframing the problem is likely to help you, and us, provide better solutions.
I'm looking for an efficient way to save my game coded in python. I've looked at pickle, but I'm not sure it will suit my needs as there is a lot of info that needs to be saved.
The way I understand pickle is it will take whatever I give it and dump it into a file, which sounds great. But not only do I have the player character to save, theres the rooms that the character has already been, the state of the rooms, if the user opened up treasure chests, etc. And if I understand pickle correctly, I would have to input each room manually for that to work.
Is there a better way to do this? Would I be better off creating a database of some kind? Or am I just not using pickle correctly?
Perhaps I'm missing something, but why not put all of the data you mention into a dictionary. It may even be a multi-level dictionary. Something like:
{
"player_info":{"name":"Bart", ..},
"current_state_of_game":{"cur_location":"library", "prior_room":"kitchen", ..}
}
Then pickle that?
Working from the command line I wrote a function called go(). When called it receives input asking the user for a directory address in the format drive:\directory. No need for extra slashes or quotes or r literal qualifiers or what have you. Once you've provided a directory, it lists all the non-hidden files and directories under it.
I want to update the function now with a statement that stores this location in a variable, so that I can start browsing my hierarchy without specifying the full address every time.
Unfortunately I don't remember what statements I put in the function in the first place to make it work as it does. I know it's simple and I could just look it up and rebuild it from scratch with not too much effort, but that isn't the point.
As someone who is trying to learn the language, I try to stay at the command line as much as possible, only visiting the browser when I need to learn something NEW. Having to refer to obscure findings attached to vaguely related questions to rediscover how to do things I've already done is very cumbersome.
So my question is, can I see the contents of functions I have written, and how?
Unfortunately no. Python does not have this level of introspection. Best you can do is see the compiled byte code.
The inspect module details what information is available at runtime: https://docs.python.org/3.5/library/inspect.html
I understand that this question has, in essence, already been asked, but that question did not have an unequivocal answer, so please bear with me.
Background: In my company, we use Perforce submission numbers as part of our versioning. Regardless of whether this is a correct method or not, that is how things are. Currently, many developers do separate submissions for code and documentation: first the code and then the documentation to update the client-facing docs with what the new version numbers should be. I would like to streamline this process.
My thoughts are as follows: create a Perforce trigger (which runs on the server side) which scans the submitted documentation files (such as .txt) for a unique term (such as #####PERFORCE##CHANGELIST##NUMBER###ROFL###LOL###WHATEVER#####) and then replaces it with the value of what the change list would be when submitted. I already know how to determine this value. What I cannot figure out, is how or where to update the files.
I have already determined that using the change-content trigger (whether possible or not), which
"fire[s] after changelist creation and file transfer, but prior to committing the submit to the database",
is the way to go. At this point the files need to exist somewhere on the server. How do I determine the (temporary?) location of these files from within, say, a Python script so that I can update or sed to replace the placeholder value with the intended value? The online documentation for Perforce which I have found so far have not been very explicit on whether this is possible or how the mechanics of a submission at this stage would work.
EDIT
Basically what I am looking for is RCS-like functionality, but without the unsightly special character sequences which accompany it. After more digging, what I am asking is the same as this question. However I believe that this must be possible, because the trigger is running on the server side and the files had already been transferred to the server. They must therefore be accessible by the script.
EXAMPLE
Consider the following snippet from a release notes document:
[#####PERFORCE##CHANGELIST##NUMBER###ROFL###LOL###WHATEVER#####] Added a cool new feature. Early retirement is in sight.
[52702] Fixed a really annoying bug. Many lives saved.
[52686] Fixed an annoying bug.
This is what the user submits. I then want the trigger to intercept this file during the submission process (as mentioned, at the change-content stage) and alter it so that what is eventually stored within Perforce looks like this:
[52738] Added a cool new feature. Early retirement is in sight.
[52702] Fixed a really annoying bug. Many lives saved.
[52686] Fixed an annoying bug.
Where 52738 is the final change list number of what the user submitted. (As mentioned, I can already determine this number, so please do dwell on this point.) I.e., what the user sees on the Perforce client console is.
Changelist 52733 renamed 52738.
Submitted change 52738.
Are you trying to replace the content of pending changelist files that were edited on a different client workspace (and different user)?
What type of information are you trying to replace in the documentation files? For example,
is it a date, username like with RCS keyword expansion? http://www.perforce.com/perforce/doc.current/manuals/p4guide/appendix.filetypes.html#DB5-18921
I want to get better clarification on what you are trying to accomplish in case there is another way to do what you want.
Depending on what you are trying to do, you may want to consider shelving ( http://www.perforce.com/perforce/doc.current/manuals/p4guide/chapter.files.html#d0e5537 )
Also, there is an existing Perforce enhancement request I can add your information to,
regarding client side triggers to modify files on the client side prior to submit. If it becomes implemented, you will be notified by email.
99w,
I have also added you to an existing enhancement request for Customizable RCS keywords, along
with the example you provided.
Short of using a post-command trigger to edit the archive content directly and then update the checksum in the database, there is currently not a way to update the file content with the custom-edited final changelist number.
One of the things I learned very early on in programming was to keep out of interrupt level as much as possible, and especially don't do stuff in interrupt that requires resources that can hang the system. I totally get that you want to resolve the internal labeling in sequence, but a better way to do it may be to just set up the edit during the trigger so that a post trigger tool can perform the file modification.
Correct me if I'm looking at this wrong, but there seems a bit of irony, or perhaps recursion, if you are trying to make a file change during the course of submitting a file change. It might be better to have a second change list that is reserved for the log. You always know where that file is, in your local file space. That said, ktext files and $ fields may be able to help.
I learned how to create a playlist in a previous question, but now I can't figure out how to add tracks to it. Right now I have:
tracks.sort(key=lambda tup: tup[0])
i = 0
for trackList in generatePlaylists(tracks,10):
i += 1
playlistname = str(i)
p = {'name': playlistname}
playlist = iTunes.classForScriptingClass_("playlist").alloc().initWithProperties_(p)
iTunes.sources()[0].playlists().insertObject_atIndex_(playlist, 0)
# Find the playlist I just made
for playlist in iTunes.sources()[0].playlists():
if playlist.name() == playlistname:
newPlaylist = playlist
# Add the tracks to it
for track in trackList:
print track[1].name()
iTunes.add_to_(track[1],newPlaylist)
My tracks are in a list of tuples tracks, where the first element of the tuple is a score and the second is the actual track object. generatePlaylists is an iterator which splits all library tracks into 10 lists.
The above code runs without error, but in iTunes the playlists are empty.
First, here's the short answer:
track.duplicateTo_(newPlaylist)
The problem is that iTunes.add_to_ sends the add command, which takes a file (alias) and imports it into a playlist; you want to send the duplicate command, which takes any object and makes another copy of the object. You don't have a file, you have a track. (You could get a file via track.location(), but you don't want to re-import the file, just copy the track over.)
Also, in this case, you need to call the method on the track, rather than calling it on the app and passing it the track.
The first half of this is hard to explain without a solid understanding of the iTunes object model (and the AE model underneath it). But you don't really need to understand it. In most cases, by looking over the iTunes scripting dictionary (in AppleScript Editor) and trial and error (in AppleScript Editor or with py-appscript) you can figure it out what you want. (Just make sure you're working on a scrap library, or have a backup…) In this case, the only commands it could possibly be are add, copy, duplicate, or move, so just try them all and see what they do. Or, alternatively, go to dougscripts and download a bunch of samples and find one that does what you want.
The second half of this, figuring out how to translate to ScriptingBridge… well, I can't explain it without going into a long rant on SB (which hhas does much better than me, if you want to read one). But the basics are this: As far as iTunes is concerned, duplicate is a command. If you give it a direct object (tell application "iTunes" to duplicate theTrack to thePlaylist) it'll use that; if not, you're asking the subject to duplicate itself (tell theTrack to duplicate to thePlaylist). It works exactly like English. But SB insists on an object-oriented model, where duplicate is a method on some object. So, only one of those two forms is going to work. In general, you can figure out which by just looking at dir(iTunes) and dir(track) to see which one has a method that looks like the command you want.
As you can tell from the above, you've got a lot of trial and error ahead of you if you're trying to do anything complicated. Good luck, and keep asking.
PS, I have no idea why your code fails silently. The obvious way the add_to_ method should translate into a command should raise a -1708 error (as appscript iTunes.add(track, to=newPlaylist) or AppleScript add theTrack to newPlaylist both do…).