Just A Small Guideline

Feel free to browse the site. Don't be afraid to leave comments, they're supportive.
Please follow me on Twitter @ehldmnt and retweet my posts.

Tuesday, March 26, 2013

Buliding A Simple Game In Python3

In order to have a better understanding of the program that I am writing, I've decided that I would write a dead simple tutorial series on how to make a small game in Python. Links to all of my places of reference will take place as I find them.

Step 1: Setting up the script

There are two ways we can write this script.
-- As as executable. [Optional]
-- As a script to import into the interpreter so that we can update it as we go along. [Recommended, but also optional]

The first way is as such:

#!/usr/bin/python3

def function():
...


The second way:



def function():
...


And then simply execute it in the shell like so:

[~]> python3 script.py
Or Import it into the Python3 interpreter like so:
[~]> python3
>>>> import script
>>>>
Now, onto the actual coding..

#-- Say Game Title --#
print("---------------------------------------")
print("\nWelcome to \"Dineros\"\n")
print("---------------------------------------")
#-- Defining Global Variables --#
x_pos = 0
y_pos = 0

#-- Event Functions --#

#N/A

#-- Shitty Functions that don't really work --#

#def wall():
#    global x_pos, y_pos
#    if x_pos >= 10 and y_pos >= 10:
#        print("You hit a wall.")
#        x_pos = 10 and y_pos = 10
#    if x_pos <= -10 and y_pos <= -10:
#        print("You hit a wall.")
#        x_pos = -10 and y_pos = -10


#-- Main Game Function --#

# Define the function "move"
def move():
    # Refer to the variables we made earlier, not ones made locally.
    global x_pos, y_pos
    # Create an array of words that will be later used.
    where = ("\nWhere would you like to go?",
    "\nYou can go\n(N)orth, (S)outh, \n(E)ast, or (W)est.\n",
    "Or you can find out (wh)ere you are.\n")
    # Print objects from that array in the order as we need it to. We'll improve this later.
    print(where[0])
    print(where[1])
    print(where[2])
    # Create a prompt by which people know they are to type. 
    dir = input('> ')
    # If you put in capital or lower case n
    if dir == "N" or dir == "n":
        # Then the global y_pos will have 1 added to it.
        y_pos += y_pos + 1
    elif dir == "S" or dir == "s":
        y_pos += y_pos - 1
    elif dir == "E" or dir == "E":
        x_pos += x_pos + 1
    elif dir == "W" or dir == "W":
        x_pos += x_pos - 1
    elif dir == "wh" or dir == "WH" or dir == "Wh" or dir == "wH":
        print("You are at %d x, and %d y,\n" % (x_pos, y_pos))
    #If you didn't type any of the things above, then it will ask you to choose a direction.
    else:
        print("\nChoose a Direction.")
        exit
        return(dir)

#-- Conclusion Var. --#

# The variable that defines how it all ends.
ending = 1

#-- Game Running --#
# The condition for as long as this game is running
while ending == 1:
# The function we defined earlier
    move()
# The places to be when this is all complete [temporary]
    if x_pos == 1 and y_pos == 1:
# The thing that stops the loop and ends the game.
        ending += ending + 1 


The program is far from complete, but this is a good start to our character being able to move. Load it up and try it for yourself!

Initialization Method 1:

[~]> ./script.py


Initialization Method 2:

[~]> python3 script.py


Initialization Method 3 [OPTIONAL]:

[~]> python3
>>>import script

No comments: