Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3 4 ... 6

Author Topic: Astronaut text based game (Python)  (Read 8559 times)

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #15 on: January 17, 2011, 03:38:02 am »

It is, but i'm a history degree student and am doing programming as a little hobby project. I'm way out of my league  8)
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Astronaut text based game (Python)
« Reply #16 on: January 17, 2011, 08:22:22 am »

Ok ive got my head around lists again (fairly easy). Next ill impliment the roster of Mercury 7 Astronauts and Vostok Cosmonauts!
One of the awesome things about python is that it has some very good list-related functions. Say you got a list of all mercury missions and you want to get a list with a detail of each mission (for example, the mission date). You could make a function that takes a mission and returns the date that mission took place, called for example getMissionDate(mission).
Then you can use map(getMissionDate, missionList) et presto, you've got a list with all the dates in which the missions in the list took place.
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #17 on: January 17, 2011, 08:36:09 am »

Thanks! looking into functions is my next objective, i've just beefed out the menu a bit more, added in practicing and the original Mercury/Vostok crew rosters and their starting skill levels, these need modifying greatly. I imported them directly from BARIS and the system i'm using uses higher numbers. I'll probably just double everyones base skills.
Next I will take a long look at functions and try to use them (will be testing the things people suggested).

Spoiler (click to show/hide)
« Last Edit: January 17, 2011, 08:38:47 am by Deadmeat1471 »
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #18 on: January 17, 2011, 08:46:25 am »

Ok ive got my head around lists again (fairly easy). Next ill impliment the roster of Mercury 7 Astronauts and Vostok Cosmonauts!
One of the awesome things about python is that it has some very good list-related functions. Say you got a list of all mercury missions and you want to get a list with a detail of each mission (for example, the mission date). You could make a function that takes a mission and returns the date that mission took place, called for example getMissionDate(mission).
Then you can use map(getMissionDate, missionList) et presto, you've got a list with all the dates in which the missions in the list took place.

Could you elaborate slightly on this, it looks really useful!


How would I put that into the mission function?

I know this isnt correct... hmm just attempting to recapping functions.
Code: [Select]
def getMissionDate(mission):
    print mission + str(MissionDate)


getMissionDate(Sputnik4)

* I feel like im getting closer to what I need... functions always made my brain hurt. I'm sure once I crack it and understand it will be incredibly easy, but until then, brain hurt.
« Last Edit: January 17, 2011, 09:14:46 am by Deadmeat1471 »
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #19 on: January 17, 2011, 09:22:14 am »

Ahhah!

Code: [Select]
Sputnik4date = 1960
Sputnik5date = 1961



def getMissionDate(mission):
    print 'Mission date was: ' + str(mission)


getMissionDate(Sputnik4date)
getMissionDate(Sputnik5date)

Becomes:
Code: [Select]
Mission date was: 1960
Mission date was: 1961

Success. Somewhat. Ill have to think more on how to use this further.
« Last Edit: January 17, 2011, 09:26:27 am by Deadmeat1471 »
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Astronaut text based game (Python)
« Reply #20 on: January 17, 2011, 09:37:32 am »

A function is like a Recipe. In short it contains a description of what the program has to do when you call the function. Let's have a look at the anatomy of a function:
Code: [Select]
def functionName(paramter1, parameter2)   #Creates a function with the name functionName, that takes 2 arguments.
    paramter3 = parameter1 + parameter2   #Binds a new variable called parameter3 to the value of paramter1 + parameter2
    return parameter3                     #When the function hits a return statement it'll stop and return the value


sum = functionName(1, 2)                  #Calls functionName. The program will jump to the code above, bind parameter1 to 1 and parameter2 to 2.
                                          #then it'll execute the code in the function. The function returns 3, so sum is set to 3.
As you see, a function can only do something, in itself it will not store anything. This means that for what you want to do, you'll have to use something that can store information for you. The easiest way to think about your mission is as an object. An object, in its simpelest form is a description of something (abstract or tangible) that exists within your "code world". Say in your game you've got to deal with chairs for some reason. A chair has an amount of legs, a height, a color and a material it's made of. We could of course define a list as a chair, store the amount of legs in the first cell, the height in the second et cetera. But that'll get confuzing fast because we constantly need to make the leap: first cell is legs, second is height.
To combat that problem, python lets you make objects from a class description. A class is an abstract definition of your object. For example, a chair would look like this:
Code: [Select]
class chair
    def __init__(self, legs, height, color, material)
        self.legs = legs
        self.height = height
        self.color = color
        self.material = material
This defines an abstract definition for your class. The __init__ function always gets called when you make a new instance of the class (it's what many languages refer to as the constructor). In this case the __init__ function takes 4 arguments (ignore self for the moment) and binds some properties of the chair class to the 4 supplied values.
You can now make a new chair as following:
Code: [Select]
chair1 = chair(4, 80, "green", "wood")
Which will create a green, 4 legged chair made from wood, with a height of 80 arbitrary-chair-height-units. We can then get some information about the class with the following code:
Code: [Select]
chair1Height= chair1.height
which will bind chair1Height to the height of chair 1 (currently 80 achu's)


I hope this clears some stuff up?
« Last Edit: January 17, 2011, 09:48:36 am by Virex »
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #21 on: January 17, 2011, 09:44:39 am »

Yeah that helps alot! I've got some experimenting to do  :D
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #22 on: January 17, 2011, 10:09:24 am »

Code: [Select]
class mission:
    def __init__(program, number, date):
        self.program = program
        self.number = number
        self.date = date

missionsputnik4 = mission('Sputnik', 4, 1960)
missionsputnik5 = mission('Sputnik', 5, 1961)

missionsputnik4date = missionsputnik4.date

print missionsputnik4date

This has an error :( cant figure it out.

It says:

missionsputnik4 = mission('Sputnik', 4, 1960)
TypeError: __init__() takes exactly 3 arguments (4 given)


But it's clearly 3 not 4. Anyone know what the problem is?
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Astronaut text based game (Python)
« Reply #23 on: January 17, 2011, 10:10:43 am »

Code: [Select]
class mission:
    def __init__(program, number, date):
        self.program = program
        self.number = number
        self.date = date

missionsputnik4 = mission('Sputnik', 4, 1960)
missionsputnik5 = mission('Sputnik', 5, 1961)

missionsputnik4date = missionsputnik4.date

print missionsputnik4date

This has an error :( cant figure it out.

It says:

missionsputnik4 = mission('Sputnik', 4, 1960)
TypeError: __init__() takes exactly 3 arguments (4 given)


But it's clearly 3 not 4. Anyone know what the problem is?
No no, it should be
Code: [Select]
print missionsputnik4.date
with a period.
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #24 on: January 17, 2011, 10:14:25 am »

oh i forgot self, im retarded.
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Astronaut text based game (Python)
« Reply #25 on: January 17, 2011, 10:15:24 am »

Code: [Select]
class mission:
    def __init__(program, number, date):
        self.program = program
        self.number = number
        self.date = date

missionsputnik4 = mission('Sputnik', 4, 1960)
missionsputnik5 = mission('Sputnik', 5, 1961)

missionsputnik4date = missionsputnik4.date

print missionsputnik4date

This has an error :( cant figure it out.

It says:

missionsputnik4 = mission('Sputnik', 4, 1960)
TypeError: __init__() takes exactly 3 arguments (4 given)


But it's clearly 3 not 4. Anyone know what the problem is?
__Init__'s first argument should be self. It won't work if you omit it. The self argument holds the object you've just created so the program can actually refer to the different parts of the object. It's passed implicitly, so you don't have to care about it when creating a new object instance, but it needs to be there when defining the constructor of a class.
« Last Edit: January 17, 2011, 10:17:37 am by Virex »
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #26 on: January 17, 2011, 10:16:17 am »

Code: [Select]
class mission:
    def __init__(self, program, number, date):
        self.program = program
        self.number = number
        self.date = date

missionsputnik4 = mission('Sputnik', 4, 1960)
missionsputnik5 = mission('Sputnik', 5, 1961)

s4date = missionsputnik4.date

print s4date

Works fine now, think I confused Darvi with my retarded variable names :D
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Astronaut text based game (Python)
« Reply #27 on: January 17, 2011, 10:18:30 am »

Code: [Select]
class mission:
    def __init__(self, program, number, date):
        self.program = program
        self.number = number
        self.date = date

missionsputnik4 = mission('Sputnik', 4, 1960)
missionsputnik5 = mission('Sputnik', 5, 1961)

s4date = missionsputnik4.date

print s4date

Works fine now, think I confused Darvi with my retarded variable names :D
Nope, confusion is the status quo in my case.
Logged

Deadmeat1471

  • Bay Watcher
    • View Profile
Re: Astronaut text based game (Python)
« Reply #28 on: January 17, 2011, 10:33:28 am »

Do you think it might be a good idea to put in the class EVERYTHING to do with the mission, for example it seems a nice place to put all the mission info like -

    def __init__(self, program, number, date, OIB, TLI, MCCB, LOIB)

to which the answer to the last four would be yes or no (skipping or running IF statements in mission resolution phase).

something like

while missionsputnik4.OIB = true:
        print 'performing Orbital Insertion burn'
        if random.randint(0, 100) > rocketsafety + c #capsule skill#
               print 'step failure, orbital insertion burn fails'
               if random.randint(0, 100) > 50:
                    print 'catastrophic failure'
                    if random.randint(0, 100) > 50:
                        print 'you are killed!'   
                        alive = 0
                    else: you are wounded
               else:
                    print ''Orbital insertion burn successful



Acronyms are Orbital Insertion Burn, Trans-Lunar Injection, Mid Course Correction Burn, Lunar Orbital Insertion Burn if anyones wondering.
« Last Edit: January 17, 2011, 10:43:35 am by Deadmeat1471 »
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: Astronaut text based game (Python)
« Reply #29 on: January 17, 2011, 10:35:46 am »

You mean true and false right?

So it would be
Code: [Select]
while missionsputnik4.OIB == true:
And yeah, don't see there being a problem as long as every mission has those same variables.
« Last Edit: January 17, 2011, 10:38:14 am by Darvi »
Logged
Pages: 1 [2] 3 4 ... 6